首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

获取磁盘序列号有关问题

2012-02-10 
获取磁盘序列号问题下面是我用的一种方法,可是2005报错。错误如下,请指教:对PInvoke函数“WindowsApplicatio

获取磁盘序列号问题
下面是我用的一种方法,可是2005报错。
错误如下,请指教:
              对   PInvoke   函数“WindowsApplication3!WindowsApplication1.Form1::GetVolumeInformation”的调用导致堆栈不对称。原因可能是托管的   PInvoke   签名与非托管的目标签名不匹配。请检查   PInvoke   签名的调用约定和参数与非托管的目标签名是否匹配。


Private   Declare   Function   GetVolumeInformation   Lib   "kernel32 "   Alias   "GetVolumeInformationA "   (ByVal   lpRootPathName   As   String,   ByVal   lpVolumeNameBuffer   As   String,   ByVal   nVolumeNameSize   As   Integer,   ByVal   ByReflpVolumeSerialNumber   As   Long,   ByVal   lpMaximumComponentLength   As   Integer,   ByVal   lpFileSystemFlags   As   Integer,   ByVal   lpFileSystemNameBuffer   As   String,   ByVal   nFileSystemNameSize   As   Integer)   As   Integer
        Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click
                Dim   SerialNumber   As   Long
                Dim   TempStr1   As   New   String(Chr(0),   255)
                Dim   TempStr2   As   New   String(Chr(0),   255)
                Dim   TempInt1,   TempInt2   As   long
                GetVolumeInformation( "C:\ ",   TempStr1,   256,   SerialNumber,   TempInt1,   TempInt2,   TempStr2,   256)
                MsgBox( "C盘序列号: "   &   SerialNumber)
        End   Sub

[解决办法]
你数据类型弄错了:在vb.net中vb的long应该转换成integer,也就是.net的int32数据类型,附代码:


Imports System.Runtime.InteropServices
Public Class Form1

<DllImport( "Kernel32.dll ", CharSet:=System.Runtime.InteropServices.CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetVolumeInformation(ByVal RootPathName As String, ByVal VolumeNameBuffer As System.Text.StringBuilder, ByVal VolumeNameSize As Int32, ByRef VolumeSerialNumber As Int32, ByRef MaximumComponentLength As Int32, ByRef FileSystemFlags As Int32, ByVal FileSystemNameBuffer As System.Text.StringBuilder, ByVal nFileSystemNameSize As Int32) As Boolean
End Function

Private Declare Function GetVolumeInformation2 Lib "kernel32 " Alias "GetVolumeInformationA " (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, ByRef ByReflpVolumeSerialNumber As Integer, ByRef lpMaximumComponentLength As Integer, ByRef lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim SerialNumber As Integer
Dim TempStr1 As New String(Chr(0), 255)
Dim TempStr2 As New String(Chr(0), 255)
Dim TempInt1, TempInt2 As Integer
GetVolumeInformation2( "c:\ ", TempStr1, 256, SerialNumber, TempInt1, TempInt2, TempStr2, 256)
MsgBox( "C盘序列号: " & SerialNumber.ToString( "X "))


MsgBox(GetVolumeSerial( "C "))


End Sub

Private Function GetVolumeSerial(ByVal DriveLetter As String) As String

'Check for valid drive letter argument.
Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
If ValidDriveLetters.IndexOf(DriveLetter) <> -1 Then
If DriveLetter.Length = 1 Then
Dim Disk As New System.Management.ManagementObject( "Win32_LogicalDisk.DeviceID= " " " & DriveLetter & ": " " ")
Dim DiskProperty As System.Management.PropertyData
For Each DiskProperty In Disk.Properties
If DiskProperty.Name = "VolumeSerialNumber " Then
Return DiskProperty.Value.ToString '.ToString 'Return the volume serial number.
End If
Next DiskProperty
End If
End If
Return Nothing 'Invalid drive letter.


End Function

End Class


参考:

http://www.pinvoke.net/default.aspx/kernel32.GetVolumeInformation

热点排行