GetVolumeInformation

bogdandaniel

Member
Joined
Mar 28, 2003
Messages
11
:(
How can I use the api GetVolumeInformation in VB.NET to get the hard disk serial number? If I use the same code like in VB 6 the serial is always 0!

Please help me

Thanks
 
Code:
Imports System.Runtime.InteropServices
Imports System.Text

Private Declare Ansi Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" ( _
    ByVal lpRootPathName As String, _
    ByVal lpVolumeNameBuffer As StringBuilder, _
    ByVal nVolumeNameSize As Int32, _
    [b]ByRef[/b] lpVolumeSerialNumber As Int32, _
    [b]ByRef[/b] lpMaximumComponentLength As Int32, _
    [b]ByRef[/b] lpFileSystemFlags As Int32, _
    ByVal lpFileSystemNameBuffer As StringBuilder, _
    ByVal nFileSystemNameSize As Int32) As Boolean

Dim r As Boolean
Dim iSerial As Int32
Dim sbVolumeName As New StringBuilder(256)
Dim sbFileSystemName As New StringBuilder(256)

r = GetVolumeInformation("D:\", sbVolumeName, sbVolumeName.Capacity, _
    iSerial, 0, 0, sbFileSystemName, sbFileSystemName.Capacity)

MessageBox.Show(r.ToString() & " " & iSerial.ToString, Application.ProductName)
 
Im just reading about WMI in a book I got. Heres a chunk of code I wrote just playing around with the info in the book. Its essentially non-API method. The only frustrating thing is that I dont know what all you can get information about. The only reason I knew the format of "win32_logicaldisk.deviceid" was that I saw it in the book. Theres no telling what other things you can grab as Ive not found a list of them anywhere.
Code:
Public Sub ShowDriveInfo()
       Dim eDrive As New Management.ManagementObject("win32_logicaldisk.deviceid=""c:""")
        Dim sbProperties As New StringBuilder()

        eDrive.Get()

        Dim en As IEnumerator
        en = eDrive.Properties.GetEnumerator()
        While en.MoveNext
            sbProperties.Append(en.Current.Name)
            sbProperties.Append(" = ")
            sbProperties.Append(en.Current.Value)
            sbProperties.Append(vbCrLf)
        End While
        MessageBox.Show(sbProperties.ToString())
End Sub
 
Thanks for your reply. This is the code that I used:

Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" _
(ByVal lpRootPathName As String, _
ByVal lpVolumeNameBuffer As String, _
ByVal nVolumeNameSize As Int32, _
ByRef lpVolumeSerialNumber As System.UInt32, _
ByRef lpMaximumComponentLength As Int32, _
ByRef lpFileSystemFlags As Int32, _
ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Int32) As Int32

Dim drvserial As System.UInt32
Dim mydrvlabel As String = Space$(200)
Dim myfilesys As String = Space$(200)
Dim i As Int32
Dim j As Int32
Dim x As Int32
Dim number As Int32
x = GetVolumeInformation("C:\", mydrvlabel, 200, drvserial, i, j, myfilesys, 200)
MsgBox(drvserial.ToString & " - " & mydrvlabel)
MsgBox(myfilesys)

About WMI I found this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_logicaldisk.asp
 
Back
Top