How to read the volume label of hard disk?

Just starting out? Need help? Post your questions and find answers here.
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

How to read the volume label of hard disk?

Post by AlanFoo »

Hope someone can help.

I need to read the name of the hard disk volume label using Purebasic. How to do it ?


thanks.

Alan
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to read the volume label of hard disk?

Post by ts-soft »

Code: Select all

Define lpRootPathName.s = "C:\"
Define lpVolumeNameBuffer.s = Space(#MAX_PATH)
Define nVolumeNameSize = #MAX_PATH

If GetVolumeInformation_(lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize, 0, 0, 0, 0, 0)
  Debug lpVolumeNameBuffer
EndIf
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

Re: How to read the volume label of hard disk?

Post by AlanFoo »

Thanks Thomas for responding so fast.


I copied and pasted to the Purebasic editor opening up a new page and tried it.


Define lpRootPathName.s = "C:\"
Define lpVolumeNameBuffer.s = Space(#MAX_PATH)
Define nVolumeNameSize = #MAX_PATH

If GetVolumeInformation_(lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize, 0, 0, 0, 0, 0)
Debug lpVolumeNameBuffer
Debug nVolumeNameSize
Debug lpRootPathName
EndIf

the lpVoumeNameBuffer seems to be blank

The result at the Debug output.

Blank SPACE
260
C:\

Any idea?

thanks
Alan
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to read the volume label of hard disk?

Post by ts-soft »

Your Drive have not a volume-name (is empty)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

Re: How to read the volume label of hard disk?

Post by AlanFoo »

Hi Thomas, ya .. you are right. Went to DOS and dir it - stated no Label :>)

How about reading the "Volume Serial Number"?

Thanks
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to read the volume label of hard disk?

Post by ts-soft »

AlanFoo wrote:How about reading the "Volume Serial Number"?
The same API, here with some beautification:

Code: Select all

Procedure.s GetDriveSerial(Drive.s)
  Protected lpVolumeNameBuffer.s, lpVolumeSerialNumber.l
  If Len(Drive) = 1 : Drive + ":\" : EndIf
  If Right(Drive, 1) <> "\" : Drive + "\" : EndIf
  lpVolumeNameBuffer.s = Space(#MAX_PATH +1)
  GetVolumeInformation_(@Drive, @lpVolumeNameBuffer, #MAX_PATH +1, @lpVolumeSerialNumber, 0,0,0,0)
  ProcedureReturn Hex(PeekW(@lpVolumeSerialNumber + 2) & $FFFF) + "-" + Hex(PeekW(@lpVolumeSerialNumber) & $FFFF)
EndProcedure
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

Re: How to read the volume label of hard disk?

Post by AlanFoo »

Dear Thomas,

Thanks a million....

Got it.

Warmest regards
Alan
mesozorn
Enthusiast
Enthusiast
Posts: 171
Joined: Fri Feb 20, 2009 2:23 am

Re: How to read the volume label of hard disk?

Post by mesozorn »

I wonder if I could resurrect this thread to amend it with a related question:

When using GetVolumeInformation on a local disk/drive, the volume label is returned correctly, no problem. But with a mapped network drive, the same API command will successfully return the "true" volume label as mirrored if one right-clicks on the drive in Windows Explorer and selects "Properties". However, I do not know of a way to get its "local" name that is shown in the Explorer tree right beside the drive letter.

Example, my drive letter tree looks like this:

C:\ (System)
D:\ (Applications)
H:\ (Storage) <----- This is a mapped network drive

Drive "H" shows with the name "Storage" in my drive tree, and if I like I can rename this to anything I want and it will be reflected on MY local drive tree, but if I right click on "H" and select "Properties", the volume label shows up as something else, for example "Data drive". And this stays constant no matter what renaming operations I perform locally.

When I use GetVolumeInformation, I am only ever able to retrieve that static unchanging "Data drive" label, presumably set by a network administrator somewhere. Is there any way I can retrieve the "local" drive label, either using WinAPI or a native PB command?

Thanks!
Post Reply