Drive/Volume Label

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Drive/Volume Label

Post by SFSxOI »

In windows 7 you can also get the volume lables from the registry at : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Search\VolumeInfoCache
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Drive/Volume Label

Post by Crusiatus Black »

SFSxOI wrote:In windows 7 you can also get the volume lables from the registry at : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Search\VolumeInfoCache
Thanks :)

In Windows XP, I'm able to obtain the label through a GUID path, could you confirm if this path
exists in Windows 7?

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\
This contains a list of drive GUID's currently or once connected to the system.

*Edit, and if in some of the GUID Keys there is a Reg_SZ value named _LabelFromReg or a subkey named _Autorun?
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Drive/Volume Label

Post by Crusiatus Black »

If that key exists in windows 7 (and I believe it does, because kernel32.dll > GetVolumeNameForVolumeMountPoint exists in Win7 aswell)
then obtaining the label will be easy from registry, here's a function to get the GUID or VolumePath for a specific drive.

A little by the way, over here a DRIVE_REMOTE does not have a GUID nor VolumePath.

Code: Select all

Procedure.s GetDriveGUIDPath(szDrive.s, boolGUIDOnly = #False)
  szDrive = Left(szDrive,1) + ":\"
  Protected szVolumeGUIDPath.s = Space(100)
  Protected dwVolumeGUIDSize.l = 100
  Protected iDrive_Res.s = "", iDrive_CFR
  Protected GuidStart, GuidEnd, szGUID.s
  Protected hLib = OpenLibrary(#PB_Any,"kernel32.dll")
  If hLib
    CompilerIf #PB_Compiler_Unicode
      iDrive_CFR = CallFunction(hLib, "GetVolumeNameForVolumeMountPointW", @szDrive, @szVolumeGUIDPath, @dwVolumeGUIDSize)
    CompilerElse
      iDrive_CFR = CallFunction(hLib, "GetVolumeNameForVolumeMountPointA", @szDrive, @szVolumeGUIDPath, @dwVolumeGUIDSize)
    CompilerEndIf 
    If iDrive_CFR <> 0
      iDrive_Res = szVolumeGUIDPath
      If boolGUIDOnly
        GuidStart  = FindString(iDrive_Res, "{", 1)
        GuidEnd    = FindString(iDrive_Res, "}", GuidStart)
        iDrive_Res = Mid(iDrive_Res, GuidStart, Len(iDrive_Res)-GuidStart)
      EndIf 
    EndIf 
    CloseLibrary(hLib)
  EndIf 
  ProcedureReturn iDrive_Res
EndProcedure  
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Drive/Volume Label

Post by SFSxOI »

Yes, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2 does exist in Windows 7, however, it does not contain any volume lables.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Drive/Volume Label

Post by Crusiatus Black »

SFSxOI wrote:Yes, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2 does exist in Windows 7, however, it does not contain any volume lables.
Thanks,


Correct, it contains GUID's, for example: {0b2d57c1-5a99-11dd-b560-806d6172696f} and if that drive (because that GUID represents a drive)
has a custom label specified through an autorun.inf/ini it contains the subkey \_Autorun\DefaultLabel or through renaming it in My Computer it contains REG_SZ Key _LabelFromReg.

MountPoints2 is the registry entry for mounting drives I suppose, so the labels and default icons are specified there.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Drive/Volume Label

Post by Crusiatus Black »

Here is some code to get the a certain drive's label if it exists.

First it checks for _LabelFromReg, the label edited through windows.
Then it checks for _Autorun\DefaultLabel, if this value is not empty,
it will overwrite the previous found label, pretty much the same what Explorer.exe does.

Code: Select all

Procedure.s GetDriveGUIDPath(szDrive.s, boolGUIDOnly = #False)
  szDrive = Left(szDrive,1) + ":\"
  Protected szVolumeGUIDPath.s = Space(100)
  Protected dwVolumeGUIDSize.l = 100
  Protected iDrive_Res.s = "", iDrive_CFR
  Protected GuidStart, GuidEnd, szGUID.s
  Protected hLib = OpenLibrary(#PB_Any,"kernel32.dll")
  If hLib
    CompilerIf #PB_Compiler_Unicode
      iDrive_CFR = CallFunction(hLib, "GetVolumeNameForVolumeMountPointW", @szDrive, @szVolumeGUIDPath, @dwVolumeGUIDSize)
    CompilerElse
      iDrive_CFR = CallFunction(hLib, "GetVolumeNameForVolumeMountPointA", @szDrive, @szVolumeGUIDPath, @dwVolumeGUIDSize)
    CompilerEndIf 
    If iDrive_CFR <> 0
      iDrive_Res = szVolumeGUIDPath
      If boolGUIDOnly
        GuidStart  = FindString(iDrive_Res, "{", 1)
        GuidEnd    = FindString(iDrive_Res, "}", GuidStart)
        iDrive_Res = Mid(iDrive_Res, GuidStart, Len(iDrive_Res)-GuidStart)
      EndIf 
    EndIf 
    CloseLibrary(hLib)
  EndIf 
  ProcedureReturn iDrive_Res
EndProcedure  
Procedure.s GetLabelFromGUID(szGUID.s)
  Protected szLabel.s = Space(1024)
  Protected szRetVal.s = ""
  Protected lpLabel.l = 0
  Protected lnLabel.l = 0
  Protected lnType.l = 0
  Protected Key
  Protected qRes = 0
  If RegOpenKeyEx_(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\"+szGUID, 0, #KEY_ALL_ACCESS, @Key) = #ERROR_SUCCESS
    If RegQueryValueEx_(Key, "_LabelFromReg", 0, @lnType, #NUL, @lnLabel) = #ERROR_SUCCESS 
      szLabel = Space(lnLabel+1)
      If RegQueryValueEx_(Key, "_LabelFromReg", 0, @lnType, @szLabel, @lnLabel) = #ERROR_SUCCESS 
        szRetVal = szLabel
      EndIf 
    EndIf 
    RegCloseKey_(Key)
  EndIf 
  ;If Not Trim(szRetVal)
    szLabel.s = Space(1024)
    lpLabel.l = 0
    lnLabel.l = 0
    lnType.l = 0
    If RegOpenKeyEx_(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\"+szGUID+"\_AutoRun\DefaultLabel", 0, #KEY_ALL_ACCESS, @Key) = #ERROR_SUCCESS
      If RegQueryValueEx_(Key, "", 0, @lnType, #NUL, @lnLabel) = #ERROR_SUCCESS 
        szLabel = Space(lnLabel+1)
        If RegQueryValueEx_(Key, "", 0, @lnType, @szLabel, @lnLabel) = #ERROR_SUCCESS 
          If Trim(szLabel)
            szRetVal = szLabel
          EndIf 
        EndIf 
      EndIf 
      RegCloseKey_(Key)
    EndIf 
  ;EndIf 
  ProcedureReturn szRetVal
EndProcedure 
You should decomment the next two lines if you do not wish to overwrite:

Code: Select all

;If Not Trim(szRetVal)
and

Code: Select all

;EndIf
Now to obtain a drive's label, use the next code:

Code: Select all

szDriveLabel.s = GetLabelFromGUID(GetDriveGUIDPath("C:\", #True))
if the label is present, it will be stored in szDriveLabel :D

I found out that it doesn't work on DRIVE_REMOTE drives, because windows doesn't return the VolumePath of these drives.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Post Reply