Solved

GetVolumeInformation API call

Posted on 1998-06-19
13
658 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:tward
ID: 1463783
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
ID: 1463784
Please tell me how to transfer the information of your procedure to separate textboxes.
0
 

Author Comment

by:Zorro032798
ID: 1463785
Since you're not answering, I'll have to reopen the question to other people.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 7

Accepted Solution

by:
tward earned 80 total points
ID: 1463786
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
ID: 1463787
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
ID: 1463788
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
 

Author Comment

by:Zorro032798
ID: 1463789
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
ID: 1463790
Try:

VolInfo = msiGetVolumeInformation("C:\")
0
 

Author Comment

by:Zorro032798
ID: 1463791
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
ID: 1463792
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
ID: 1463793
Thanks, you've been really helpful.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1463794
Bought This Question.
0
 

Author Comment

by:Zorro032798
ID: 1463795
Good idea!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

615 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