api函数调用有误

sunmoonfly 2004-12-21 10:16:00
通过调用api来获得磁盘的序列号

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

........
.........
Dim SerialNum As Long
Dim Res As Long
Dim Temp1 As String
Dim Temp2 As String
'initialise the strings
Temp1 = space(255)
Temp2 = space(255)
'call the API function
Res = GetVolumeInformation(strDrive, Temp1, Len(Temp1), SerialNum, 0, 0, Temp2, Len(Temp2))
GetSerialNumber = SerialNum

上面的代码放在vb6里面serialNum是能够获得值的,但放到.net里serialNum的值为0,测试很长时间也找不出原因,那位仁兄用过,帮忙解决一下,谢谢


...全文
200 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmoonfly 2004-12-22
  • 打赏
  • 举报
回复
问题已经解决,谢谢楼上各位的热心帮忙
我整理了一下,已加密做成注册码了,下面详细解释一下
下面代码得出的是:volumn serival number

Public Function GetDriveSerial(ByVal DriveLetter As String) As String
'Returns the serial number of the specified drive
'ie, GetDriveSerial("c:")
Dim strSelectText As String = "Win32_logicaldisk='" & DriveLetter &"'"
Dim objMO As New System.Management.ManagementObject(strSelectText)
objMO.Get()
Return CType(objMO.Properties("VolumeSerialNumber").Value, String)
End Function

通过api得出的是磁盘序列号,两者结果不同,volumn serival number 在磁盘整理的时候可以看到
通过api得到的是不能看到的.

暂且应付形式,以后再研究

sunmoonfly 2004-12-21
  • 打赏
  • 举报
回复
谢谢楼上两位的热心帮忙:我在电脑上又测试了一下有些疑问:

to landlordh(software)

imports System.management
Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive ")
Dim share As ManagementObject
For Each share In searcher.Get()
Debug.WriteLine(share.GetText(TextFormat.Mof))
MsgBox(share("Caption"))
Next share
________________________________________________________
这只是反映磁盘的一些基本信息,容量,状况等等。
Uint32 = cmicWmiObj("signature") 这一行我在电脑上试,提示里面的signature不存在,报错,不知是什么原因,
顺便问问 磁盘参数比如 caption,signature 的参数资料哪里可以获得.


我的本意是获取磁盘(主要是c盘)的序列号来通过加密的手段做注册码,在vb里面通过上面是可以实现的, 返回的是数值。在.net里调用就不能得到正确的值。总是为0。


顺便介绍一下这个api
function name :GetVolumeInformation
required os: Windows NT 3.1 or later; Windows 95 or later
description: The GetVolumeInformation function returns information about a file system and volume whose root directory is specified.












haoztao 2004-12-21
  • 打赏
  • 举报
回复
'获得硬盘序列号

Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT
* FROM Win32_DiskDrive")



Dim Uint32 As UInt32

For Each cmicWmiObj As ManagementObject In cmicWmi.Get

Uint32 = cmicWmiObj("signature")

Next

TextBox1.Text = Uint32.ToString
landlordh 2004-12-21
  • 打赏
  • 举报
回复
Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")



Dim Uint32 As UInt32

For Each cmicWmiObj As ManagementObject In cmicWmi.Get

Uint32 = cmicWmiObj("signature")

Next

TextBox1.Text = Uint32.ToString

landlordh 2004-12-21
  • 打赏
  • 举报
回复
imports System.management
Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive ")
Dim share As ManagementObject
For Each share In searcher.Get()
Debug.WriteLine(share.GetText(TextFormat.Mof))
MsgBox(share("Caption"))
Next share
landlordh 2004-12-21
  • 打赏
  • 举报
回复
Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Integer, ByVal lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer

...
Dim SerialNumber As Long
Dim TempStr1 As New String(Chr(0), 255)
Dim TempStr2 As New String(Chr(0), 255)
Dim TempInt1, TempInt2 As Integer
GetVolumeInformation("C:\", TempStr1, 256, SerialNumber, TempInt1, TempInt2, TempStr2, 256)
MsgBox("C盘序列号:" & SerialNumber)
...
landlordh 2004-12-21
  • 打赏
  • 举报
回复
这个试下:
用了没有问题的

Public Function GetDriveSerial(ByVal DriveLetter As String) As String
'Returns the serial number of the specified drive
'ie, GetDriveSerial("c:")
Dim strSelectText As String = "Win32_logicaldisk='" & DriveLetter &"'"
Dim objMO As New System.Management.ManagementObject(strSelectText)
objMO.Get()
Return CType(objMO.Properties("VolumeSerialNumber").Value, String)
End Function
wangxt 2004-12-21
  • 打赏
  • 举报
回复
帮你顶
机器人 2004-12-21
  • 打赏
  • 举报
回复
hoho, win32 的Long 是32位的~~~~ 所以在.net 里要定义为 integer ~~~~~~~~~~

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧