windows x下的c编程,获取电脑硬件序列号(cpu 硬盘 bios 主版) (c 获取服务器cpu序列号)

1、取得磁盘序列号、卷标和文件系统类型

磁盘序列号在每次软盘或硬盘格式化后都重新生成,并且不回重复。许多程序员用瞎巧此加密。其衫衫实也可以修改该函数,可以得到磁盘卷标和文件系统类型信息。

声明:

Private Declare Function GetVolumeInformation Lib “kernel32.dll” Alias “GetVolumeInformationA”(ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

代码:

Function GetSerialNumber(sRoot As String) As Long

Dim lSerialNum As Long

Dim R As Long

Dim sTemp1 As String, sTemp2 As String

strLabel = String$(255, Chr$(0))

注释: 磁盘卷标

strType = String$(255, Chr$(0))

注释: 文件系统类型 一般为 FAT

R = GetVolumeInformation(sRoot, strLabel, Len(strLabel), lSerialNum, 0, 0, strType, Len(strType))

GetSerialNumber = lSerialNum

注磨塌键释:在 strLabel 中为 磁盘卷标

注释:在 strType 中为 文件系统类型

End Function

用法:

当驱动器不存在时,函数返回 0。如果是个非根目录,也将返回 0:

lSerial = GetSerialNumber(“c:\”)

2、 大家只需将以下代码拈贴到VB的工程中,并以类的模式保存即可使用。所有的代码我都已经测试过,并曾在多次开发项目中使用过。

使用方法:

dim oHard as new CGetHardInfo ‘您可将CGetHardInfo 替换成您保存的类

读取硬盘的序列号:

HDDSR=CGetHardInfo.HardDiskID ‘读取C盘的串号 (默认值)

如果获取其他盘符的序列号则需要指明盘符如下:

CGetHardInfo.HardDrive=”D”

HDDSR=CGetHardInfo.HardDiskID ‘读取D盘的串号

获得网卡的串号:

NICID=CGetHardInfo.NicID

获取本机的IP地址:

IPAddress=CGethardInfo.IPAddress

获取主机名称:

HostName=CGetHardInfo.HostName

如果产生错误(比如没有网卡等)可利用LASTERROR返回产生的最后一个错误 ERRORs为产生的错误

以下为获取硬件信息的完整代码

‘=================================================================

‘获取相关的硬件信息 (硬盘ID/网卡ID/主机名称/IP地址)

‘文件名称: GetHardInfo.cls

‘开发时间:.10.30

‘=================================================================

Option Explicit

Private Const NCBASTAT = &H33

Private Const NCBNAMSZ =

Private Const HEAP_ZERO_MEMORY = &H8

Private Const HEAP_GENERATE_EXCEPTIONS = &H4

Private Const NCBRESET = &H32

Private Type NCB

ncb_command As Byte

ncb_retcode As Byte

ncb_lsn As Byte

ncb_num As Byte

ncb_buffer As Long

ncb_length As Integer

ncb_callname As String * NCBNAMSZ

ncb_name As String * NCBNAMSZ

ncb_rto As Byte

ncb_sto As Byte

ncb_post As Long

ncb_lana_num As Byte

ncb_cmd_cplt As Byte

ncb_reserve(9) As Byte ‘ Reserved, must be

ncb_event As Long

End Type

Private Type ADAPTER_STATUS

adapter_address(5) As Byte ‘As String *

rev_major As Byte

reserved0 As Byte

adapter_type As Byte

rev_minor As Byte

duration As Integer

frmr_recv As Integer

frmr_xmit As Integer

iframe_recv_err As Integer

xmit_aborts As Integer

xmit_success As Long

recv_success As Long

iframe_xmit_err As Integer

recv_buff_unavail As Integer

t1_timeouts As Integer

ti_timeouts As Integer

Reserved1 As Long

free_ncbs As Integer

max_cfg_ncbs As Integer

max_ncbs As Integer

xmit_buf_unavail As Integer

max_dgram_size As Integer

pending_sess As Integer

max_cfg_sess As Integer

max_sess As Integer

max_sess_pkt_size As Integer

name_count As Integer

End Type

Private Type NAME_BUFFER

Name As String * NCBNAMSZ

name_num As Integer

name_flags As Integer

End Type

Private Type ASTAT

adapt As ADAPTER_STATUS

NameBuff(30) As NAME_BUFFER

End Type

Private Declare Function Netbios Lib “netapi32.dll” (pncb As NCB) As Byte

Private Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Private Declare Function GetProcessHeap Lib “kernel32” () As Long

Private Declare Function HeapAlloc Lib “kernel32” (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long

Private Declare Function HeapFree Lib “kernel32” (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long

3、磁盘序列号在每次软盘或硬盘格式化后都重新生成,并且不回重复。许多程序员用此加密。其实也可以修改该函数,可以得到磁盘卷标和文件系统类型信息。

声明:

Private Declare Function GetVolumeInformation Lib “kernel32.dll” Alias _

“GetVolumeInformationA” (ByVal lpRootPathName As String, ByVal _

lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, _

lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _

lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _

ByVal nFileSystemNameSize As Long) As Long

代码:

Function GetSerialNumber(sRoot As String) As Long

Dim lSerialNum As Long

Dim R As Long

Dim strLabel As String, strType As String

strLabel = String$(255, Chr$(0))

‘磁盘卷标

strType = String$(255, Chr$(0))

‘文件系统类型 一般为 FAT

R = GetVolumeInformation(sRoot, strLabel, Len(strLabel), _

lSerialNum, 0, 0, strType, Len(strType))

GetSerialNumber = lSerialNum

‘在 strLabel 中为 磁盘卷标

‘在 strType 中为 文件系统类型

End Function

用法:

当驱动器不存在时,函数返回 0。如果是个非根目录,也将返回 0:

lSerial = GetSerialNumber(“c:\”)


数据运维技术 » windows x下的c编程,获取电脑硬件序列号(cpu 硬盘 bios 主版) (c 获取服务器cpu序列号)