Question

Listing HardDrives and Volumes Associated (not Using WMI)

Asked by: Delphian

Dear Experts,

I come to ask your wisdom, to give some directions to what API functions, libraries, etc I could use to list all storage devices and the volumes associated (how to map to a logical drive such as C: I leave for another question). More specifically, I would like to like the hard drives and after listing their associated volumes...
I looked and googled around a lot, and could not get a good path to that info....

Thanks in advance for any answers

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-09-03 at 17:36:43ID23701240
Tags

Delphi, VB, C++

Topics

Pascal Programming Language

,

C++ Programming Language

,

Visual Basic Programming

Participating Experts
5
Points
500
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Mute Volume with API
    I need the exact code to mute the Main volume and other Devices with an API within VB using windows 98.
  2. Windows Volume Management - API/command Line
    I am looking for API that can do Windows volume management such as Partitioning and formatting of volumes (space). Also is there a commandline utility to grow exisitng volume on windows? Thanks.
  3. Relationship / difference between SNMP and WMI
    Dear all experts, I'm new on SNMP and WMI and I really want to know is there any relationship between SNMP and WMI, are they the same? When I need to use SNMP and when to use WMI? Moreover, is there any tools about this? I would like to monitor my company network and ...
  4. WMI
    How can I convert the following to search other partitions? strComputer = Inputbox ("Server Name?") Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set ...
  5. read a harddrive label?
    Is there a component or a function that would read the label off of a harddrive or cdrom? i found hltools.pas but that doesnt compile... Tom

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: JosephGloszPosted on 2008-09-03 at 18:23:23ID: 22383589

well, for starters, you could use two native Delphi routines: DiskFree and DiskSize. You can look in help for their syntax, i.e.:
function DiskSize(Drive: Byte): Int64;
DiskSize returns the size in bytes of the specified drive, where 0 = Current, 1 = A, 2 = B, etc. DiskSize returns -1 if the drive number is invalid.

so something like this would work:

type
  TDriveChars = 'A'..'Z';
var
  Drive: char;
  iDrive: Byte;
  DiskInfo: array[TDriveChars] of Int64;
begin
for Drive := 'A' to 'Z' do
  begin
  iDrive := ord(Drive) - ord('A') + 1;    //maps 'A' to 1, 'B' to 2 etc...
  DiskInfo[Drive] := DiskSize(iDrive);
  end;

So any array entry that isn't "-1" is a valid drive.

Then, once you have all the valid drives you can continue with
  GetVolumeInformation(PChar(drv), pVolName, MAX_PATH, nVNameSer,maxCmpLen, FSSysFlags, pFSBuf, MAX_PATH);

  GetDiskFreeSpaceEx(PChar(drv), AvailToCaller, TotalBytes, TotalFreeBytes);

our code makes those exact calls and it works beautifully.

Here is the complete code snippet (actually adapted from somewhere else). Some of the fields are TComputerInfo properties. Adapt this as you see fit....
procedure TComputerInfo.GetHDInfo;
var
  drv         : String;
  pVolName    : PChar;
  nVNameSer   : PDWORD;
  maxCmpLen   : DWord;
  FSSysFlags  : DWord;
  pFSBuf      : PChar;

  AvailToCaller, TotalBytes: Int64;
  TotalFreeBytes: PLargeInteger;
begin
  GetMem(pVolName, MAX_PATH);
  GetMem(pFSBuf, MAX_PATH);
  GetMem(nVNameSer, MAX_PATH);
  GetMem(TotalFreeBytes, MAX_PATH);

  // Get OS information
  bNT := GetNT;
  sOS := GetWinVersion;
  // Get drive letter
  drv := ExtractFileDrive(Application.ExeName);
  //Get the volume information
  GetVolumeInformation(PChar(drv), pVolName, MAX_PATH, nVNameSer,
                       maxCmpLen, FSSysFlags, pFSBuf, MAX_PATH);
  sLetter := drv;

  // Get Free Space.  GetDiskFreeSpaceEx works on 98 or greater.
  if ((bNT = false) and (Version = '4.0')) then begin
    raise Exception.Create('Hard drive capacity and free space can not be determined in Windows 95.');
    iCapacity := 0;
    iFreeSpace := 0;
  end
  else begin
    GetDiskFreeSpaceEx(PChar(drv), AvailToCaller, TotalBytes, TotalFreeBytes);
    iCapacity := (TotalBytes div 1048576);
    iFreeSpace := (TotalFreeBytes^ div 1048576);
  end;

 

by: DelphianPosted on 2008-09-03 at 18:34:12ID: 22383660

Nice, that's the solution of other border of the problem (corresponding drives and volumes),
which I was thinking in create another question.
But my problem reside in justly list and correspond the physical HD with it's volumes...

That's a good one...
Thank you anyway.


 

by: JosephGloszPosted on 2008-09-03 at 19:45:52ID: 22384011

I don't quite follow.  The above should be completely what you need.

A physical hard drive will have one or more volumes.  With one hard drive, it is typically the 'C' drive. All the above info will work.

If a physical hard drive is partitioned into two partitions (volumes) it will be say, a 'C' and a 'D' drive. (the letters are really arbitrary, they could be any letters).  In this case, the above info will also work just fine.

In what way will the following NOT do the job?
for Drive := 'A' to 'Z' do
  begin
  iDrive := ord(Drive) - ord('A') + 1;    //maps 'A' to 1, 'B' to 2 etc...
  DiskInfo[Drive] := DiskSize(iDrive);
  end;

 

by: v_shalchianPosted on 2008-09-04 at 00:25:55ID: 22384863

Maybe you want the volume label;
then you should use this :

function GetVolumeLabel(Drive: Char): Str11;
{ function returns volume label of a disk }
var
  SR: TSearchRec;
  DriveLetter: Char;
  SearchString: String[7];
  P: Byte;
begin
  SearchString := Drive + ':\*.*';

  { find vol label }
  if FindFirst(SearchString, faVolumeID, SR) = 0 then begin
    P := Pos('.', SR.Name);
    if P > 0 then begin                      { if it has a dot... }
      Result := '           ';               { pad spaces between name }
      Move(SR.Name[1], Result[1], P - 1);    { and extension }
      Move(SR.Name[P + 1], Result[9], 3);
    end
    else begin
      Result := SR.Name;                     { otherwise, pad to end }
      PadVolumeLabel(Result);

    end;
  end
  else
    Result := '';
end;

 

by: CSecurityPosted on 2008-09-04 at 04:47:37ID: 22386137

this C++ code list all drives and prints their type(cdrom ,removable , fixed...)
I compiled in Borland 5.02 but I think should be compiled in visual c++ 6.0 too.
this code works great ;)

#include <windows.h>
#include <iostream.h>
 
long int pow(int i)
{
	if(i)
	{
		int ret = 1;
		for(int j=1;j<=i;j++)
			ret*=2;
		return ret;
	}
	else
		return 1;
}
 
int main()
{
long int LDs = GetLogicalDrives();
	char letter[2], drive_letter[4];
	BYTE index=0;
	for(unsigned int i=0;i<25;i++)
		if((LDs & pow(i)) != 0)
		{
			letter[0] = (char)(65 + i);
			letter[1] = '\0';
			strcpy(drive_letter,letter);
			strcat(drive_letter,":\\");
			switch(GetDriveType(drive_letter))
         {
         case 2:
         cout<<drive_letter<<"\t"<<"Removable"<<endl;
         break;
         case 3:
         cout<<drive_letter<<"\t"<<"Fixed Drive"<<endl;
         break;
         case 4:
         cout<<drive_letter<<"\t"<<"Remote"<<endl;
         break;
         case 5:
         cout<<drive_letter<<"\t"<<"CD-Rom"<<endl;
         break;
         case 6:
         cout<<drive_letter<<"\t"<<"Ram disk"<<endl;
         break;
         default:
         cout<<drive_letter<<"\t"<<"Unrecognized"<<endl;
         }
		}
   system("pause");
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

 

by: JosephGloszPosted on 2008-09-04 at 11:06:29ID: 22390604

Hmm, that iterates through 26 drive letters just like my code above. With the only difference being that it calls GetDriveType instead of GetSize.

 

by: DelphianPosted on 2008-09-05 at 12:33:49ID: 22402871

@ALL:
Oh, my goodness... Seems my question wasn't clear.

Let's take as imaginary machine.
CPU intel Pentium 4
HardDrive Sansung MP0804H
  Has drive C:\
HardDrive Seagate ST3230623A
 Has drive E:\
 Has drive F:\
<and a lot of other stuff>


I want a path to get that information:
 Sansung MP0804
    C:\ - Space total: 17.3Gb Space Free: 2.15Gb
 Seagate ST3230623A
    E:\ - <same info as above>

My trouble is relating the physical hardsdrives with it's correspondents volumes (and logical drives). I'm  completely clueless about it.

@ciuly:
The approach on:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20722391.html

Seems very promising... Hmmm

 

by: JosephGloszPosted on 2008-09-05 at 12:52:09ID: 22403055

I think my original post has all the info to do exactly that.  Here's a simplified version:

for Drive := 'A' to 'Z' do
  begin
  iDrive := ord(Drive) - ord('A') + 1;  
  if DiskSize(iDrive) > 0 then
    writeln(format('Disk %s Space Total: %d bytes;  Space Free: %d bytes',
        [Drive,DiskSize(iDrive),DiskFree(iDrive)]);
  end;

Using the other API calls you can also get other drive info like it's name, serial number, etc.

The above writes to standard output; if you want to write to say a memo box, change the line to
    S := format('Disk %s Space Total: %d bytes;  Space Free: %d bytes',
        [Drive,DiskSize(iDrive),DiskFree(iDrive)]);
and then write the S to the memo box.

and this also writes the full number of bytes. I'll leave it to you to convert that into nice round KB or MB or GB or TB.  

 

by: ciulyPosted on 2008-09-05 at 12:56:24ID: 22403113

I know. I would have suggested WMI, more specifically mitec components (he uses wmi AND winapi) but I don't remember to what extent is the WMI part in his component and you didn't mention why you didn't want WMI. you might want to give mitec compoent a shot: http://www.softpedia.com/get/System/System-Info/MiTeC-System-Information.shtml

 

by: DelphianPosted on 2008-09-08 at 15:10:17ID: 22422143

@ciuly

Why no WMI? I don't want to rely on a type library that doesn't
exist natively on Win9x.  If I use WMI, I'll have to create a ins-
taller - and I wanted to be zero-installation.


@JosephGlosz
Yeah, but what I'm searching is just the relation of physical HD
with the volumes.
Like I showed in my last message:
HardDrive Sansung MP0804H
 Has drive C:\

What I want is THAT relationship between the HD and logical
drives.


 

by: DelphianPosted on 2008-09-08 at 15:21:53ID: 22422203

@ciuly
Since Mitec SysInfo component suite  is discontinued (was sold to another company), it's not a option anymore...

 

by: ciulyPosted on 2008-09-09 at 05:12:32ID: 22426546

why not? you can still use that old version :) I do :P

 

by: DelphianPosted on 2008-09-12 at 16:17:30ID: 22464686

@ciuly and @Joseph

 Initially, I was focused to understand the code of getting the hd serial and use it to get other information about the storage devices. So I'll use ciuly links and Joseph approach (although using an API to get an bitmap with the logical drives in use instead of iterating through them).
 
But I changed plans.

 Actually, I'll use the MITec Suite because it provides MUCH more information than merely drive, physical devices and volumes - and is ABSURDLY easier to use.

 But I'll reward both ciuly and Joseph as follows:
Ciuly --> 400 points
Joseph --> 100 points

 Do you two agree with that?
 
(I'll read the replies only on Monday, since my home computer is screwed :-(     )

 @all

 Thank you all very much for your time and dedication to this question.
 
Best regards
Delphian

 

by: DelphianPosted on 2008-09-12 at 16:21:43ID: 22464716

Where it reads
> So i'll use ciuly links and Joseph appoach

read

>So I'd use ciuly' links and Joseph's approach


Sorry about that

 

by: JosephGloszPosted on 2008-09-12 at 16:33:11ID: 22464771

I should get more points. You can't even pronounce 'Ciuly' ...  You must detract points just for that alone  :)

 

by: ciulyPosted on 2008-09-13 at 02:10:17ID: 22466219

lol :)
joseph, try it like this: chiulee ;)

 

by: DelphianPosted on 2008-09-15 at 10:11:50ID: 31493007

Thank you two very much and see ya on EE.

 

by: JosephGloszPosted on 2008-09-15 at 10:58:52ID: 22481135

Sweet!  So just having a girl say your name makes it look like she is blowing you a kiss. Awesome. I love it. *I* should have thought of that!  I retract my previous comment, I say add extra points for you from now on!  

 

by: ciulyPosted on 2008-09-15 at 11:52:34ID: 22481737

I never thought of it like that :lol: I have to write that down :D

 

by: DelphianPosted on 2008-09-15 at 15:07:00ID: 22483371

ROFL!!!!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...