• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

GetVolumeInformation API call

I'd like to use the GetVolumeInformation Win32 API call in VB5 to retrieve information about the hard disk and floppy drives. I especially want to have the serial number and the volume ID.

Can anyone write the code for me in VB5? All the info has to be returned in separate textboxes.
0
Zorro032798
Asked:
Zorro032798
  • 7
  • 5
1 Solution
 
twardCommented:
Here is an example of what I use to get the VolumeName of a drive, but I load everything into the VolumeInformation Structure.

Public Type msiVolumeInformation
     
      msiRootPathName As String
      msiVolumeNameBuffer As String
      msiVolumeSerialNumber As Long
      msiMaximumComponentLength As Long
      msiFileNameCaseIsPreserved As Boolean
      msiFileNamesCaseSensitive As Boolean
      msiUnicodeStoredOnDisk As Boolean
      msiPersistentACLS As Boolean
      msiSupportsCompression As Boolean
      msiVolumeCompressed As Boolean
      msiFileSystemNameBuffer As String

End Type

Public Const FILE_CASE_SENSITIVE_SEARCH = &H1
Public Const FILE_CASE_PRESERVED_NAMES = &H2
Public Const FILE_UNICODE_ON_DISK = &H4
Public Const FILE_PERSISTENT_ACLS = &H8
Public Const FILE_FILE_COMPRESSION = &H10
Public Const FILE_VOLUME_IS_COMPRESSED = &H80000

Public Const FS_CASE_IS_PRESERVED = FILE_CASE_PRESERVED_NAMES
Public Const FS_CASE_SENSITIVE = FILE_CASE_SENSITIVE_SEARCH
Public Const FS_PERSISTENT_ACLS = FILE_PERSISTENT_ACLS
Public Const FS_UNICODE_STORED_ON_DISK = FILE_UNICODE_ON_DISK
Public Const FS_FILE_COMPRESSION = FILE_FILE_COMPRESSION
Public Const FS_VOL_IS_COMPRESSED = FILE_VOLUME_IS_COMPRESSED

Declare Function msiGetVolumeInformationAPI Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Public Function msiGetVolumeInformation(ByVal RootPath As String) As msiVolumeInformation

  Dim ReturnValue As Long
 
  Dim ReturnInfo As msiVolumeInformation
 
  Dim lpVolumeNameBuffer As String
  Dim nVolumeNameSize As Long
  Dim lpVolumeSerialNumber As Long
  Dim lpMaximumComponentLength As Long
  Dim lpFileSystemFlags As Long
  Dim lpFileSystemNameBuffer As String
  Dim nFileSystemNameSize As Long
 
  ReturnInfo.msiRootPathName = RootPath
  ReturnInfo.msiFileNameCaseIsPreserved = False
  ReturnInfo.msiFileNamesCaseSensitive = False
  ReturnInfo.msiUnicodeStoredOnDisk = False
  ReturnInfo.msiPersistentACLS = False
  ReturnInfo.msiSupportsCompression = False
  ReturnInfo.msiVolumeCompressed = False

  lpVolumeNameBuffer = Space$(256)
  nVolumeNameSize = 256
 
  lpFileSystemNameBuffer = Space$(256)
  nFileSystemNameSize = 256
 
  ReturnValue = msiGetVolumeInformationAPI(RootPath, lpVolumeNameBuffer, nVolumeNameSize, lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags, lpFileSystemNameBuffer, nFileSystemNameSize)
 
  If ReturnValue = 0 Then
   
    ReturnInfo.msiVolumeNameBuffer = "ERROR"
    ReturnInfo.msiVolumeSerialNumber = -1
    ReturnInfo.msiMaximumComponentLength = -1
    ReturnInfo.msiFileSystemNameBuffer = "ERROR"
 
  Else
 
    ReturnInfo.msiVolumeNameBuffer = Left(lpVolumeNameBuffer, Len(lpVolumeNameBuffer) - 1)
    ReturnInfo.msiVolumeSerialNumber = lpVolumeSerialNumber
    ReturnInfo.msiMaximumComponentLength = lpMaximumComponentLength
    ReturnInfo.msiFileSystemNameBuffer = Left(lpFileSystemNameBuffer, Len(lpFileSystemNameBuffer) - 1)
 
    If lpFileSystemFlags And FS_CASE_IS_PRESERVED Then
   
      ReturnInfo.msiFileNameCaseIsPreserved = True
   
    End If
 
    If lpFileSystemFlags And FS_CASE_SENSITIVE Then
   
      ReturnInfo.msiFileNamesCaseSensitive = True
   
    End If
 
    If lpFileSystemFlags And FS_UNICODE_STORED_ON_DISK Then
     
      ReturnInfo.msiUnicodeStoredOnDisk = True
   
    End If

    If lpFileSystemFlags And FS_PERSISTENT_ACLS Then
 
      ReturnInfo.msiPersistentACLS = True
   
    End If
 
    If lpFileSystemFlags And FS_FILE_COMPRESSION Then
   
      ReturnInfo.msiSupportsCompression = True
   
    End If
 
    If lpFileSystemFlags And FS_VOL_IS_COMPRESSED Then
   
      ReturnInfo.msiVolumeCompressed = True
   
    End If
 
  End If
 
  msiGetVolumeInformation = ReturnInfo
 
End Function

Public Function GetVolumeName(ByVal RootPath As String) As String
 
  Dim VolInfo As msiVolumeInformation
 
  VolInfo = msiGetVolumeInformation(RootPath)
 
  GetVolumeName = VolInfo.msiVolumeNameBuffer
 
End Function

0
 
Zorro032798Author Commented:
Please tell me how to transfer the information of your procedure to separate textboxes.
0
 
Zorro032798Author Commented:
Since you're not answering, I'll have to reopen the question to other people.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
twardCommented:
Public Function GetVolumeInfo(ByVal RootPath As String) As String
  
Dim VolInfo As msiVolumeInformation
  
VolInfo = msiGetVolumeInformation(RootPath)
  
Form1.Text1.Text = VolInfo.msiVolumeNameBuffer
Form1.Text2.Text = VolInfo.msiVolumeNameBuffer As String
Form1.Text3.Text = VolInfo.msiVolumeSerialNumber As Long
Form1.Text4.Text = VolInfo.msiMaximumComponentLength As Long
Form1.Text5.Text = VolInfo.msiFileNameCaseIsPreserved As Boolean
Form1.Text6.Text = VolInfo.msiFileNamesCaseSensitive As Boolean
Form1.Text7.Text = VolInfo.msiUnicodeStoredOnDisk As Boolean
Form1.Text8.Text = VolInfo.msiPersistentACLS As Boolean
Form1.Text9.Text = VolInfo.msiSupportsCompression As Boolean
Form1.Text10.Text = VolInfo.msiVolumeCompressed As Boolean
Form1.Text11.Text = VolInfo.msiFileSystemNameBuffer As String   
End Function
0
 
Zorro032798Author Commented:
This is not working, maybe I do something wrong. In the first 3 textboxes I get "Error", "Error" and "-1".

I do not need all the other information. Isn't it possible to write a simpler piece of code, so that I can easily test it?

Thanks for your help.
0
 
twardCommented:
Post the code you are using.  If it is getting Error then there is a problem in the call to the function.

If you want VolumeInformation then this is really the only way to do it that I know of, the API call returns a Volume Information Structure.
0
 
Zorro032798Author Commented:
I did a cut and paste of your code and then wrote similar lines to call the function.
Don't know what went wrong, but it isn't working.


Private Sub VolumeInfo_Click()

Dim VolInfo As msiVolumeInformation


    VolInfo = msiGetVolumeInformation("C")
   
    Text1.Text = VolInfo.      msiVolumeNameBuffer
    Text2.Text = VolInfo.      msiVolumeNameBuffer
    Text3.Text = VolInfo.      msiVolumeSerialNumber
   
   
   
End Sub

0
 
twardCommented:
Try:

VolInfo = msiGetVolumeInformation("C:\")
0
 
Zorro032798Author Commented:
How stupid of me! Now everything works fine. Thanks.

Still one more question (I'll raise your points to 80): is there a Win32 api function that can change the VolumeSerialNumber?
0
 
twardCommented:
Not that I can find, there is one to allow the setting of the Volume Label:

Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long

The SetVolumeLabel function sets the label of a file system volume.
BOOL SetVolumeLabel(
    LPCTSTR lpRootPathName,      // address of name of root directory for volume
    LPCTSTR lpVolumeName       // name for the volume
   );      
 
Parameters
lpRootPathName
Points to a null-terminated string specifying the root directory of a file system volume. This is the volume the function will label. If this parameter is NULL, the root of the current directory is used.
lpVolumeName
Points to a string specifying a name for the volume. If this parameter is NULL, the function deletes the name of the specified volume.
 
Return Values
If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
See Also
GetVolumeInformation

0
 
Zorro032798Author Commented:
Thanks, you've been really helpful.
0
 
MirkwoodCommented:
Bought This Question.
0
 
Zorro032798Author Commented:
Good idea!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now