您的位置:程序门 -> .net技术 -> vb.net



请求帮助:vb.net(2003)读硬盘序列号的原代码....


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请求帮助:vb.net(2003)读硬盘序列号的原代码....
发表于:2008-02-18 10:52:04 楼主
请求帮助:vb.net(2003)读硬盘序列号的原代码....我是菜鸟,正在拜师学习,望老师,大侠指点..
发表于:2008-02-18 10:53:571楼 得分:0
up
发表于:2008-02-18 12:31:532楼 得分:0
    '需要引用   imports   scripting
                listbox1.items.clear()
                try
                        dim   cmicwmi   as   new   system.management.managementobjectsearcher("select   *   from   win32_diskdrive")
                        dim   uint32   as   long   =   0
                        for   each   cmicwmiobj   as   management.managementobject   in   cmicwmi.get
                                uint32   =   cmicwmiobj("signature")                                 '必须要用   signature
                                listbox1.items.add(uint32.tostring)
                        next
                        textbox1.text   =   uint32.tostring
                catch   ex   as   exception
                        msgbox(ex.tostring)
                end   try

'2005下,成功
发表于:2008-02-18 12:49:243楼 得分:0
先谢hzybc!!!
可否告我:怎么引用       imports       scripting     及       signature  
        在vs   2003中可实现吗?

在次谢了!!!
                           
           
发表于:2008-02-20 19:42:034楼 得分:0
这是引用硬盘的签名
发表于:2008-02-22 09:37:275楼 得分:0
获取硬盘物理序列号的类。

vb.net code
imports system imports system.collections.generic imports system.text imports system.runtime.interopservices public class getdiskseries <structlayout(layoutkind.sequential)> _ friend structure driverstatus public bdrivererror as byte public bidestatus as byte <marshalas(unmanagedtype.byvalarray, sizeconst:=2)> _ public breserved() as byte <marshalas(unmanagedtype.byvalarray, sizeconst:=2)> _ public dwreserved() as int32 end structure <structlayout(layoutkind.sequential)> _ friend structure sendcmdoutparams public cbuffersize as int32 public driverstatus as driverstatus <marshalas(unmanagedtype.byvalarray, sizeconst:=513)> _ public bbuffer() as byte end structure <structlayout(layoutkind.sequential)> _ friend structure ideregs public bfeaturesreg as byte public bsectorcountreg as byte public bsectornumberreg as byte public bcyllowreg as byte public bcylhighreg as byte public bdriveheadreg as byte public bcommandreg as byte public breserved as byte end structure <structlayout(layoutkind.sequential)> _ friend structure sendcmdinparams public cbuffersize as int32 public irdriveregs as ideregs public bdrivenumber as byte <marshalas(unmanagedtype.byvalarray, sizeconst:=3)> _ public breserved() as byte <marshalas(unmanagedtype.byvalarray, sizeconst:=4)> _ public dwreserved() as int32 public bbuffer as byte end structure <structlayout(layoutkind.sequential)> _ friend structure getversionoutparams public bversion as byte public brevision as byte public breserved as byte public bidedevicemap as byte public fcapabilities as int32 <marshalas(unmanagedtype.byvalarray, sizeconst:=4)> _ public dwreserved() as int32 end structure <dllimport("kernel32.dll", setlasterror:=true)> _ private shared function closehandle( _ byval hobject as int32 _ ) as integer end function <dllimport("kernel32.dll")> _ private overloads shared function deviceiocontrol( _ byval hdevice as int32, _ byval dwiocontrolcode as int32, _ byref lpinbuffer as sendcmdinparams, _ byval ninbuffersize as integer, _ byref lpoutbuffer as sendcmdoutparams, _ byval noutbuffersize as integer, _ byref lpbytesreturned as int32, _ byval lpoverlapped as integer _ ) as integer end function <dllimport("kernel32.dll")> _ private overloads shared function deviceiocontrol( _ byval hdevice as int32, _ byval dwiocontrolcode as int32, _ byval lpinbuffer as integer, _ byval ninbuffersize as integer, _ byref lpoutbuffer as getversionoutparams, _ byval noutbuffersize as integer, _ byref lpbytesreturned as int32, _ byval lpoverlapped as integer _ ) as integer end function <dllimport("kernel32.dll")> _ private shared function createfile( _ byval lpfilename as string, _ byval dwdesiredaccess as int32, _ byval dwsharemode as int32, _ byval lpsecurityattributes as integer, _ byval dwcreationdisposition as int32, _ byval dwflagsandattributes as int32, _ byval htemplatefile as integer _ ) as int32 end function private const generic_read as int32 = &h80000000 private const generic_write as int32 = &h40000000 private const file_share_read as int32 = &h1 private const file_share_write as int32 = &h2 private const open_existing as int32 = 3 private const invalid_handle_value as int32 = &hffffffff private const dfp_get_version as int32 = &h74080 private const ide_atapi_identify as integer = &ha1 private const ide_ata_identify as integer = &hec private const identify_buffer_size as integer = 512 private const dfp_receive_drive_data as int32 = &h7c088 ''' <summary> ''' 获取物理序列号 ''' </summary> ''' <param name="id">系统硬盘序号,0-255</param> public shared function getserialnumber(byval id as byte) as string dim os as operatingsystem = environment.osversion if os.platform <> platformid.win32nt then throw new notsupportedexception("仅支持2000/xp/2003") end if if os.version.major < 5 then throw new notsupportedexception("仅支持2000/xp/2003") end if return read(id) end function private shared function read(byval drive as byte) as string dim drivename as string = string.concat("\\.\physicaldrive", drive.tostring()) dim device as int32 = createfile( _ drivename, _ generic_read or generic_write, _ file_share_read or file_share_write, _ 0, _ open_existing, _ 0, _ 0) if device = invalid_handle_value then return "" end if dim verpara as new getversionoutparams() dim bytrv as int32 = 0 if deviceiocontrol( _ device, _ dfp_get_version, _ 0, _ 0, _ verpara, _ marshal.sizeof(verpara), _ bytrv, _ 0 _ ) <> 0 then if verpara.bidedevicemap > 0 then dim bidcmd as byte = cbyteiif((verpara.bidedevicemap >> drive and &h10) <> 0, ide_atapi_identify, ide_ata_identify)) dim scip as new sendcmdinparams() dim scop as new sendcmdoutparams() with scip .cbuffersize = identify_buffer_size .bdrivenumber = drive with .irdriveregs .bfeaturesreg = 0 .bsectorcountreg = 1 .bcyllowreg = 0 .bcylhighreg = 0 .bdriveheadreg = cbyte((&ha0 or ((drive and 1) << 4))) .bcommandreg = bidcmd end with end with if deviceiocontrol(device, dfp_receive_drive_data, scip, marshal.sizeof(scip), scop, marshal.sizeof(scop), bytrv, 0) <> 0 then dim s as new stringbuilder() dim i as integer for i = 20 to 40 - 2 step 2 s.append(chrw(scop.bbuffer((i + 1)))) s.append(chrw(scop.bbuffer(i))) next i closehandle(device) return s.tostring().trim() end if end if end if closehandle(device) return "" end function end class



调用:

vb.net code
dim diskseries as string = getdiskseries.getserialnumber(0) '获取第1块硬盘的物理序列号
发表于:2008-02-22 10:27:246楼 得分:0
imports   system.management

'获得硬盘序列号  
                        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
                        me.txthd_no.text   =   uint32.tostring    
发表于:2008-02-22 22:45:247楼 得分:0
msd88   的可以使用,适用用于win2k及以上系统,其它适用用于winxp及以上系统
这代码只适合ide和sata硬盘,sas及scsi读不出来。


快速检索

最新资讯
热门点击