Solved

GetVolumeInformation API call

Posted on 1998-06-19
13
652 Views
Last Modified: 2012-06-27
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
Comment
Question by:Zorro032798
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:tward
Comment Utility
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
 

Author Comment

by:Zorro032798
Comment Utility
Please tell me how to transfer the information of your procedure to separate textboxes.
0
 

Author Comment

by:Zorro032798
Comment Utility
Since you're not answering, I'll have to reopen the question to other people.
0
 
LVL 7

Accepted Solution

by:
tward earned 80 total points
Comment Utility
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
 

Author Comment

by:Zorro032798
Comment Utility
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
 
LVL 7

Expert Comment

by:tward
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Zorro032798
Comment Utility
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
 
LVL 7

Expert Comment

by:tward
Comment Utility
Try:

VolInfo = msiGetVolumeInformation("C:\")
0
 

Author Comment

by:Zorro032798
Comment Utility
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
 
LVL 7

Expert Comment

by:tward
Comment Utility
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
 

Author Comment

by:Zorro032798
Comment Utility
Thanks, you've been really helpful.
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
Bought This Question.
0
 

Author Comment

by:Zorro032798
Comment Utility
Good idea!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now