A. VC怎么获取硬件ID,CPU,硬件,网卡等
CStringGetMacAddress()
/*
*通过WMI取第一块活动网卡地址,事先需要调用过CoInitialize()初始化COM
*/
{
HRESULThRes;
hRes=CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
if(FAILED(hRes))
return_T("");
IWbemLocator*pLoc=NULL;
hRes=CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID*)&pLoc);
if(FAILED(hRes))
return_T("");
IWbemServices*pSvc=NULL;
hRes=pLoc->ConnectServer(_bstr_t(L"ROOT\CIMV2"),NULL,NULL,0,
NULL,0,0,&pSvc);
if(FAILED(hRes))
{
pLoc->Release();
return_T("");
}
hRes=CoSetProxyBlanket(pSvc,RPC_C_AUTHN_WINNT,RPC_C_AUTHZ_NONE,
NULL,RPC_C_AUTHN_LEVEL_CALL,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_DEFAULT);
if(FAILED(hRes))
{
pSvc->Release();
pLoc->Release();
return_T("");
}
IEnumWbemClassObject*pEnumerator=NULL;
hRes=pSvc->ExecQuery(_bstr_t(L"WQL"),
_bstr_t(L"SELECT*FROMWin32_=TRUE"),
WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,&pEnumerator);
if(FAILED(hRes))
{
pSvc->Release();
pLoc->Release();
return_T("");
}
CStringszMacAddress=_T("");
while(true)
{
IWbemClassObject*pclsObj=NULL;
ULONGuReturn=0;
hRes=pEnumerator->Next(WBEM_INFINITE,1,&pclsObj,&uReturn);
if(FAILED(hRes)||!uReturn)
break;
VARIANTvtProp;
VariantInit(&vtProp);
if(SUCCEEDED(pclsObj->Get(L"MACAddress",0,&vtProp,0,0)))
{
if(V_VT(&vtProp)==VT_BSTR)
{
szMacAddress=vtProp.bstrVal;
VariantClear(&vtProp);
pclsObj->Release();
break;
}
VariantClear(&vtProp);
}
pclsObj->Release();
}
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
returnszMacAddress;
}
------解决方案--------------------奥,那个DLL大概在02年左右整理的好像,时间太久了,应该是不支持vsita的,楼主看看这段代码是否有用?我是拷贝来的:/************************************************************//函数名称:GetDriverInfomation//函数功能:得到驱动器的信息//参数1:bAlpha-BYTE型,驱动器的代号A-Z(a-z)//参数2:iGTI_TYPE-int,将要获取的驱动器信息类型//GDI_VOLUMENAME得到驱动器名字//GDI_VOLUMESERIALNUMBER得到驱动器序列号//GDI_VOLUMEFILESYSTEM得到驱动器文件系统//GDI_VOLUMETYPE得到驱动器类型//GDI_VOLUMESIZE得到驱动器总大小//GDI_VOLUMEFREESIZE得到驱动器剩余大小//返回值:CString型,所要得到的驱动器信息的字符串表达//例子:得到C盘的名字GetDriverInfomation('C',GDI_VOLUMENAME)************************************************************/#defineGDI_VOLUMENAME0#defineGDI_VOLUMESERIALNUMBER1#defineGDI_VOLUMEFILESYSTEM2#defineGDI_VOLUMETYPE3#defineGDI_VOLUMESIZE4#defineGDI_(BYTEbAlpha,intiGTI_TYPE){CStringstrResult=_T("");
B. 用C语言怎么得到电脑的CPU序列号,硬盘序列号等信息
获取CPU序列号要使用 汇编指令
比较麻烦
static DWORD g_eax; // 存储返回的eax
static DWORD g_ebx; // 存储返回的ebx
static DWORD g_ecx; // 存储返回的ecx
static DWORD g_edx; // 存储返回的edx
void Executecpuid(DWORD veax)
{
asm("cpuid"
:"=a"(g_eax),
"=b"(g_ebx),
"=c"(g_ecx),
"=d"(g_edx)
:"a"(g_eax));
}
int isSupport;
void GetSerialNumber(WORD nibble[6])
{
Executecpuid(1); // 执行cpuid,参数为 eax = 1
isSupport = g_edx & (1<<18); // edx是否为1代表CPU是否存在序列号
if (FALSE == isSupport) // 不支持,返回false
{
return ;
}
Executecpuid(3); // 执行cpuid,参数为 eax = 3
memcpy(&nibble[4], &g_eax, 4); // eax为最高位的两个WORD
memcpy(&nibble[0], &g_ecx, 8); // ecx 和 edx为低位的4个WORD
}
C. C++如何获取硬盘的ID求代码!
BOOL __fastcall DoIdentify( HANDLE hPhysicalDriveIOCTL,
PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP,
BYTE btIDCmd,
BYTE btDriveNum,
PDWORD pdwBytesReturned)
{
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
pSCIP->irDriveRegs.bFeaturesReg = 0;
pSCIP->irDriveRegs.bSectorCountReg = 1;
pSCIP->irDriveRegs.bSectorNumberReg = 1;
pSCIP->irDriveRegs.bCylLowReg = 0;
pSCIP->irDriveRegs.bCylHighReg = 0;
pSCIP->irDriveRegs.bDriveHeadReg = (btDriveNum & 1) ? 0xB0 : 0xA0;
pSCIP->irDriveRegs.bCommandReg = btIDCmd;
pSCIP->bDriveNumber = btDriveNum;
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
return DeviceIoControl( hPhysicalDriveIOCTL,
SMART_RCV_DRIVE_DATA,
(LPVOID)pSCIP,
sizeof(SENDCMDINPARAMS) - 1,
(LPVOID)pSCOP,
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
pdwBytesReturned, NULL);
}
char *__fastcall ConvertToString(DWORD dwDiskData[256], int nFirstIndex, int nLastIndex)
{
static char szResBuf[1024];
char ss[256];
int nIndex = 0;
int nPosition = 0;
for(nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex++)
{
ss[nPosition] = (char)(dwDiskData[nIndex] / 256);
nPosition++;
// Get low BYTE for 2nd character
ss[nPosition] = (char)(dwDiskData[nIndex] % 256);
nPosition++;
}
// End the string ss[nPosition] = '\0';
int i, index=0;
for(i=0; i<nPosition; i++)
{
if(ss[i]==0 || ss[i]==32) continue;
szResBuf[index]=ss[i];
index++;
}
szResBuf[index]=0;
return szResBuf;
}
int GetID() //主函数
{
//创建设备对象,得到设备句柄,设备为硬盘。
CString sFilePath;
sFilePath.Format("\\\\.\\PHYSICALDRIVE%d", driver);
HANDLE hFile=::CreateFile(sFilePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
0, NULL);
DWORD dwBytesReturned;
GETVERSIONINPARAMS gvopVersionParams;
DeviceIoControl(hFile, //向设备对象发送SMART_GET_VERSION设备请求,获取硬盘属性
SMART_GET_VERSION,
NULL,
0,
&gvopVersionParams,
sizeof(gvopVersionParams),
&dwBytesReturned, NULL);
if(gvopVersionParams.bIDEDeviceMap <= 0) return -2;
//发送SMART_RCV_DRIVE_DATA设备请求,获取硬盘详细信息。
// IDE or ATAPI IDENTIFY cmd
int btIDCmd = 0;
SENDCMDINPARAMS InParams;
int nDrive =0;
btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
// 输出参数
BYTE btIDOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
if(DoIdentify(hFile,
&InParams,
(PSENDCMDOUTPARAMS)btIDOutCmd,
(BYTE)btIDCmd,
(BYTE)nDrive, &dwBytesReturned) == FALSE) return -3;
::CloseHandle(hFile);
DWORD dwDiskData[256]; USHORT *pIDSector; // 对应结构IDSECTOR,见头文件
pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer; for(int i=0; i < 256; i++) dwDiskData[i] = pIDSector[i];
// 取系列号
ZeroMemory(szSerialNumber, sizeof(szSerialNumber));
strcpy(szSerialNumber, ConvertToString(dwDiskData, 10, 19));
// 取模型号
ZeroMemory(szModelNumber, sizeof(szModelNumber));
strcpy(szModelNumber, ConvertToString(dwDiskData, 27, 46));
return 0;}
D. VC++ MFC如何获取CPU ID及硬盘的序列号
// “获得Intel CPU ID”按钮消息处理函数
void CIntelCPUIDDlg::OnBtnCPUID()
{
unsigned long s1,s2;
unsigned char vendor_id[]="------------";//CPU提供商ID
CString str1,str2,str3;
// 以下为获得CPU ID的汇编语言指令
_asm // 得到CPU提供商信息
{
xor eax,eax // 将eax清0
cpuid // 获取CPUID的指令
mov dword ptr vendor_id,ebx
mov dword ptr vendor_id[+4],edx
mov dword ptr vendor_id[+8],ecx
}
str1.Format("%s",vendor_id);
_asm // 得到CPU ID的高32位
{
mov eax,01h
xor edx,edx
cpuid
mov s2,eax
}
str2.Format("%08X-",s2);
_asm // 得到CPU ID的低64位
{
mov eax,03h
xor ecx,ecx
xor edx,edx
cpuid
mov s1,edx
mov s2,ecx
}
str3.Format("%08X-%08X\n",s1,s2);
str2=str2+str3;
m_editVendor.SetWindowText(str1);
m_editCPUID.SetWindowText(str2);
}
// GetHDSerial.cpp: implementation of the CGetHDSerial class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GetHDSerial.h"
char m_buffer[256];
WORD m_serial[256];
DWORD m_OldInterruptAddress;
DWORDLONG m_IDTR;
// 等待硬盘空闲
static unsigned int WaitHardDiskIdle()
{
BYTE byTemp;
Waiting:
_asm
{
mov dx, 0x1f7
in al, dx
cmp al, 0x80
jb Endwaiting
jmp Waiting
}
Endwaiting:
_asm
{
mov byTemp, al
}
return byTemp;
}
//中断服务程序
void _declspec( naked )InterruptProcess(void)
{
int byTemp;
int i;
WORD temp;
//保存寄存器值
_asm
{
push eax
push ebx
push ecx
push edx
push esi
}
WaitHardDiskIdle();//等待硬盘空闲状态
_asm
{
mov dx, 0x1f6
mov al, 0xa0
out dx, al
}
byTemp = WaitHardDiskIdle(); //若直接在Ring3级执行等待命令,会进入死循环
if ((byTemp&0x50)!=0x50)
{
_asm // 恢复中断现场并退出中断服务程序
{
pop esi
pop edx
pop ecx
pop ebx
pop eax
iretd
}
}
_asm
{
mov dx, 0x1f6 //命令端口1f6,选择驱动器0
mov al, 0xa0
out dx, al
inc dx
mov al, 0xec
out dx, al //发送读驱动器参数命令
}
byTemp = WaitHardDiskIdle();
if ((byTemp&0x58)!=0x58)
{
_asm // 恢复中断现场并退出中断服务程序
{
pop esi
pop edx
pop ecx
pop ebx
pop eax
iretd
}
}
//读取硬盘控制器的全部信息
for (i=0;i<256;i++)
{
_asm
{
mov dx, 0x1f0
in ax, dx
mov temp, ax
}
m_serial[i] = temp;
}
_asm
{
pop esi
pop edx
pop ecx
pop ebx
pop eax
iretd
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGetHDSerial::CGetHDSerial()
{
}
CGetHDSerial::~CGetHDSerial()
{
}
// 读取硬盘序列号函数
char* CGetHDSerial::GetHDSerial()
{
m_buffer[0]='\n';
// 得到当前操作系统版本
OSVERSIONINFO OSVersionInfo;
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx( &OSVersionInfo);
if (OSVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
{
// Windows 9x/ME下读取硬盘序列号
WORD m_wWin9xHDSerial[256];
Win9xReadHDSerial(m_wWin9xHDSerial);
strcpy (m_buffer, WORDToChar (m_wWin9xHDSerial, 10, 19));
}
else
{
// Windows NT/2000/XP下读取硬盘序列号
DWORD m_wWinNTHDSerial[256];
// 判断是否有SCSI硬盘
if ( ! WinNTReadIDEHDSerial(m_wWinNTHDSerial))
WinNTReadSCSIHDSerial(m_wWinNTHDSerial);
strcpy (m_buffer, DWORDToChar (m_wWinNTHDSerial, 10, 19));
}
return m_buffer;
}
// Windows9X/ME系统下读取硬盘序列号
void _stdcall CGetHDSerial::Win9xReadHDSerial(WORD * buffer)
{
int i;
for(i=0;i<256;i++)
buffer[i]=0;
_asm
{
push eax
//获取修改的中断的中断描述符(中断门)地址
sidt m_IDTR
mov eax,dword ptr [m_IDTR+02h]
add eax,3*08h+04h
cli
//保存原先的中断入口地址
push ecx
mov ecx,dword ptr [eax]
mov cx,word ptr [eax-04h]
mov dword ptr m_OldInterruptAddress,ecx
pop ecx
//设置修改的中断入口地址为新的中断处理程序入口地址
push ebx
lea ebx,InterruptProcess
mov word ptr [eax-04h],bx
shr ebx,10h
mov word ptr [eax+02h],bx
pop ebx
//执行中断,转到Ring 0(类似CIH病毒原理)
int 3h
//恢复原先的中断入口地址
push ecx
mov ecx,dword ptr m_OldInterruptAddress
mov word ptr [eax-04h],cx
shr ecx,10h
mov word ptr [eax+02h],cx
pop ecx
sti
pop eax
}
for(i=0;i<256;i++)
buffer[i]=m_serial[i];
}
// Windows 9x/ME系统下,将字类型(WORD)的硬盘信息转换为字符类型(char)
char * CGetHDSerial::WORDToChar (WORD diskdata [256], int firstIndex, int lastIndex)
{
static char string [1024];
int index = 0;
int position = 0;
// 按照高字节在前,低字节在后的顺序将字数组diskdata 中内容存入到字符串string中
for (index = firstIndex; index <= lastIndex; index++)
{
// 存入字中的高字节
string [position] = (char) (diskdata [index] / 256);
position++;
// 存入字中的低字节
string [position] = (char) (diskdata [index] % 256);
position++;
}
// 添加字符串结束标志
string [position] = '\0';
// 删除字符串中空格
for (index = position - 1; index > 0 && ' ' == string [index]; index--)
string [index] = '\0';
return string;
}
// Windows NT/2000/XP系统下,将双字类型(DWORD)的硬盘信息转换为字符类型(char)
char* CGetHDSerial::DWORDToChar (DWORD diskdata [256], int firstIndex, int lastIndex)
{
static char string [1024];
int index = 0;
int position = 0;
// 按照高字节在前,低字节在后的顺序将双字中的低字存入到字符串string中
for (index = firstIndex; index <= lastIndex; index++)
{
// 存入低字中的高字节
string [position] = (char) (diskdata [index] / 256);
position++;
// 存入低字中的低字节
string [position] = (char) (diskdata [index] % 256);
position++;
}
// 添加字符串结束标志
string [position] = '\0';
// 删除字符串中空格
for (index = position - 1; index > 0 && ' ' == string [index]; index--)
string [index] = '\0';
return string;
}
// Windows NT/2000/XP下读取IDE硬盘序列号
BOOL CGetHDSerial::WinNTReadIDEHDSerial(DWORD * buffer)
{
BYTE IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
BOOL bFlag = FALSE;
int drive = 0;
char driveName [256];
HANDLE hPhysicalDriveIOCTL = 0;
sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);
// Windows NT/2000/XP下创建文件需要管理员权限
hPhysicalDriveIOCTL = CreateFile (driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
GETVERSIONOUTPARAMS VersionParams;
DWORD cbBytesReturned = 0;
// 得到驱动器的IO控制器版本
memset ((void*) &VersionParams, 0, sizeof(VersionParams));
if(DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_GET_VERSION,
NULL, 0, &VersionParams,
sizeof(VersionParams),
&cbBytesReturned, NULL) )
{
if (VersionParams.bIDEDeviceMap > 0)
{
BYTE bIDCmd = 0; // IDE或者ATAPI识别命令
SENDCMDINPARAMS scip;
// 如果驱动器是光驱,采用命令IDE_ATAPI_IDENTIFY, command,
// 否则采用命令IDE_ATA_IDENTIFY读取驱动器信息
bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10)?
IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
memset (&scip, 0, sizeof(scip));
memset (IdOutCmd, 0, sizeof(IdOutCmd));
// 获取驱动器信息
if (WinNTGetIDEHDInfo (hPhysicalDriveIOCTL,
&scip,
(PSENDCMDOUTPARAMS)&IdOutCmd,
(BYTE) bIDCmd,
(BYTE) drive,
&cbBytesReturned))
{
int m = 0;
USHORT *pIdSector = (USHORT *)
((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;
for (m = 0; m < 256; m++)
buffer[m] = pIdSector [m];
bFlag = TRUE; // 读取硬盘信息成功
}
}
}
CloseHandle (hPhysicalDriveIOCTL); // 关闭句柄
}
return bFlag;
}
// WindowsNT/2000/XP系统下读取SCSI硬盘序列号
BOOL CGetHDSerial::WinNTReadSCSIHDSerial (DWORD * buffer)
{
buffer[0]='\n';
int controller = 0;
HANDLE hScsiDriveIOCTL = 0;
char driveName [256];
sprintf (driveName, "\\\\.\\Scsi%d:", controller);
// Windows NT/2000/XP下任何权限都可以进行
hScsiDriveIOCTL = CreateFile (driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE)
{
int drive = 0;
DWORD mmy;
for (drive = 0; drive < 2; drive++)
{
char buffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];
SRB_IO_CONTROL *p = (SRB_IO_CONTROL *) buffer;
SENDCMDINPARAMS *pin =
(SENDCMDINPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));
// 准备参数
memset (buffer, 0, sizeof (buffer));
p -> HeaderLength = sizeof (SRB_IO_CONTROL);
p -> Timeout = 10000;
p -> Length = SENDIDLENGTH;
p -> ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;
strncpy ((char *) p -> Signature, "SCSIDISK", 8);
pin -> irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
pin -> bDriveNumber = drive;
// 得到SCSI硬盘信息
if (DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,
buffer,
sizeof (SRB_IO_CONTROL) +
sizeof (SENDCMDINPARAMS) - 1,
buffer,
sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,
&mmy, NULL))
{
SENDCMDOUTPARAMS *pOut =
(SENDCMDOUTPARAMS *) (buffer + sizeof (SRB_IO_CONTROL));
IDSECTOR *pId = (IDSECTOR *) (pOut -> bBuffer);
if (pId -> sModelNumber [0])
{
int n = 0;
USHORT *pIdSector = (USHORT *) pId;
for (n = 0; n < 256; n++)
buffer[n] =pIdSector [n];
return TRUE; // 读取成功
}
}
}
CloseHandle (hScsiDriveIOCTL); // 关闭句柄
}
return FALSE; // 读取失败
}
// Windows NT/2000/XP下读取IDE设备信息
BOOL CGetHDSerial::WinNTGetIDEHDInfo (HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,
PDWORD lpcbBytesReturned)
{
// 为读取设备信息准备参数
pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;
pSCIP -> irDriveRegs.bFeaturesReg = 0;
pSCIP -> irDriveRegs.bSectorCountReg = 1;
pSCIP -> irDriveRegs.bSectorNumberReg = 1;
pSCIP -> irDriveRegs.bCylLowReg = 0;
pSCIP -> irDriveRegs.bCylHighReg = 0;
// 计算驱动器位置
pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);
// 设置读取命令
pSCIP -> irDriveRegs.bCommandReg = bIDCmd;
pSCIP -> bDriveNumber = bDriveNum;
pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;
// 读取驱动器信息
return ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_GET_DRIVE_INFO,
(LPVOID) pSCIP,
sizeof(SENDCMDINPARAMS) - 1,
(LPVOID) pSCOP,
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
lpcbBytesReturned, NULL) );
}
E. linux下怎么用c获取硬盘物理序列号
1、在Linux系统中通过C语言获取硬盘序列号,可以借助于ioctl()函数,该函数原型如下:
intioctl(intfd,unsignedlongrequest,...);
ioctl的第一个参数是文件标识符,用open()函数打开设备时获取。
ioctl第二个参数为用于获得指定文件描述符的标志号,获取硬盘序列号,一般指明为HDIO_GET_IDENTITY。
ioctl的第三个参数为一些辅助参数,要获取硬盘序列号,需要借助于structhd_driveid结构体来保存硬盘信息,该结构体在Linux/hdreg.h中,structhd_driveid的声明如下
structhd_driveid{
unsignedshortconfig;/lotsofobsoletebitflags*/
unsignedshortcyls;/*Obsolete,"physical"cyls*/
unsignedshortreserved2;/*reserved(word2)*/
unsignedshortheads;/*Obsolete,"physical"heads*/
unsignedshorttrack_bytes;/*unformattedbytespertrack*/
unsignedshortsector_bytes;/*unformattedbytespersector*/
unsignedshortsectors;/*Obsolete,"physical"sectorspertrack*/
unsignedshortvendor0;/*vendorunique*/
unsignedshortvendor1;/*vendorunique*/
unsignedshortvendor2;/*Retiredvendorunique*/
unsignedcharserial_no[20];/*0=not_specified*/
unsignedshortbuf_type;/*Retired*/
unsignedshortbuf_size;/*Retired,512byteincrements
*0=not_specified
*/
……
};
2、源代码如下
#include<stdio.h>
//ioctl()的声明头文件
#include<sys/ioctl.h>
//硬盘参数头文件,hd_driveid结构声明头文件
#include<linux/hdreg.h>
//文件控制头文件
#include<sys/fcntl.h>
intmain()
{
//用于保存系统返回的硬盘数据信息
structhd_driveidid;
//这里以第一块硬盘为例,用户可自行修改
//用open函数打开获取文件标识符,类似于windows下的句柄
intfd=open("/dev/sda",O_RDONLY|O_NONBLOCK);
//失败返回
if(fd<0){
perror("/dev/sda");
return1;}
//调用ioctl()
if(!ioctl(fd,HDIO_GET_IDENTITY,&id))
{
printf("SerialNumber=%s ",id.serial_no);
}
return0;
}
编译完成后,执行效果如下:
F. VC++如何获取机器码硬盘序列号、CPU编号、BIOS编号等~
可以试试使用WMI(在msdn上有详细的信息):
硬盘序列号:
用Win32_PhysicalMedia
class.
CPU编号:
用Win32_Processor
class.
BIOS编号:
用Win32_BIOS
class.
下面例子取得硬盘的序列号,其他的用法也类似(msdn上的例子,把Win32_OperatingSystem改成了Win32_PhysicalMedia):
#define
_WIN32_DCOM
#include
<iostream>
using
namespace
std;
#include
<comdef.h>
#include
<Wbemidl.h>
#
pragma
comment(lib,
"wbemuuid.lib")
int
main(int
argc,
char
**argv)
{
HRESULT
hres;
//
Step
1:
--------------------------------------------------
//
Initialize
COM.
------------------------------------------
hres
=
CoInitializeEx(0,
COINIT_MULTITHREADED);
if
(FAILED(hres))
{
cout
<<
"Failed
to
initialize
COM
library.
Error
code
=
0x"
<<
hex
<<
hres
<<
endl;
return
1;
//
Program
has
failed.
}
//
Step
2:
--------------------------------------------------
//
Set
general
COM
security
levels
--------------------------
//
Note:
If
you
are
using
Windows
2000,
you
need
to
specify
-
//
the
default
authentication
credentials
for
a
user
by
using
//
a
SOLE_AUTHENTICATION_LIST
structure
in
the
pAuthList
----
//
parameter
of
CoInitializeSecurity
------------------------
hres
=
CoInitializeSecurity(
NULL,
-1,
//
COM
authentication
NULL,
//
Authentication
services
NULL,
//
Reserved
RPC_C_AUTHN_LEVEL_DEFAULT,
//
Default
authentication
RPC_C_IMP_LEVEL_IMPERSONATE,
//
Default
Impersonation
NULL,
//
Authentication
info
EOAC_NONE,
//
Additional
capabilities
NULL
//
Reserved
);
if
(FAILED(hres))
{
cout
<<
"Failed
to
initialize
security.
Error
code
=
0x"
<<
hex
<<
hres
<<
endl;
CoUninitialize();
return
1;
//
Program
has
failed.
}
//
Step
3:
---------------------------------------------------
//
Obtain
the
initial
locator
to
WMI
-------------------------
IWbemLocator
*pLoc
=
NULL;
hres
=
CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID
*)
&pLoc);
if
(FAILED(hres))
{
cout
<<
"Failed
to
create
IWbemLocator
object."
<<
"
Err
code
=
0x"
<<
hex
<<
hres
<<
endl;
CoUninitialize();
return
1;
//
Program
has
failed.
}
//
Step
4:
-----------------------------------------------------
//
Connect
to
WMI
through
the
IWbemLocator::ConnectServer
method
IWbemServices
*pSvc
=
NULL;
//
Connect
to
the
root\cimv2
namespace
with
//
the
current
user
and
obtain
pointer
pSvc
//
to
make
IWbemServices
calls.
hres
=
pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
//
Object
path
of
WMI
namespace
NULL,
//
User
name.
NULL
=
current
user
NULL,
//
User
password.
NULL
=
current
0,
//
Locale.
NULL
indicates
current
NULL,
//
Security
flags.
0,
//
Authority
(e.g.
Kerberos)
0,
//
Context
object
&pSvc
//
pointer
to
IWbemServices
proxy
);
if
(FAILED(hres))
{
cout
<<
"Could
not
connect.
Error
code
=
0x"
<<
hex
<<
hres
<<
endl;
pLoc->Release();
CoUninitialize();
return
1;
//
Program
has
failed.
}
cout
<<
"Connected
to
ROOT\\CIMV2
WMI
namespace"
<<
endl;
//
Step
5:
--------------------------------------------------
//
Set
security
levels
on
the
proxy
-------------------------
hres
=
CoSetProxyBlanket(
pSvc,
//
Indicates
the
proxy
to
set
RPC_C_AUTHN_WINNT,
//
RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE,
//
RPC_C_AUTHZ_xxx
NULL,
//
Server
principal
name
RPC_C_AUTHN_LEVEL_CALL,
//
RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE,
//
RPC_C_IMP_LEVEL_xxx
NULL,
//
client
identity
EOAC_NONE
//
proxy
capabilities
);
if
(FAILED(hres))
{
cout
<<
"Could
not
set
proxy
blanket.
Error
code
=
0x"
<<
hex
<<
hres
<<
endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return
1;
//
Program
has
failed.
}
//
Step
6:
--------------------------------------------------
//
Use
the
IWbemServices
pointer
to
make
requests
of
WMI
----
//
For
example,
get
the
name
of
the
operating
system
IEnumWbemClassObject*
pEnumerator
=
NULL;
hres
=
pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT
*
FROM
Win32_PhysicalMedia"),
WBEM_FLAG_FORWARD_ONLY
|
WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if
(FAILED(hres))
{
cout
<<
"Query
for
physical
media
failed."
<<
"
Error
code
=
0x"
<<
hex
<<
hres
<<
endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return
1;
//
Program
has
failed.
}
//
Step
7:
-------------------------------------------------
//
Get
the
data
from
the
query
in
step
6
-------------------
IWbemClassObject
*pclsObj;
ULONG
uReturn
=
0;
while
(pEnumerator)
{
HRESULT
hr
=
pEnumerator->Next(WBEM_INFINITE,
1,
&pclsObj,
&uReturn);
if(0
==
uReturn)
{
break;
}
VARIANT
vtProp;
//
Get
the
value
of
the
Name
property
hr
=
pclsObj->Get(L"SerialNumber",
0,
&vtProp,
0,
0);
wcout
<<
"Serial
Number
:
"
<<
vtProp.bstrVal
<<
endl;
VariantClear(&vtProp);
}
//
Cleanup
//
========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return
0;
//
Program
successfully
completed.
}
G. 请教:如何获得磁盘ID
1、首先点击电脑左下角的“开始”,选择“运行”,输入“cmd”,或者直接按快捷键“win+R”
2、在命令提示符中,输入“diskpart”;有些电脑会提示用户账户控制“是否允许以下程序对电脑进行控制?选择”是“即可!
3、接着再输入”list disk“,并按回车
4、执行结束后,再次输入select disk 0,按回车;
5、执行结束后,再输入detail disk,按回车,就能出现硬盘ID了,
H. C\C++如何获取硬盘的序列号和使用时间
可以试试使用WMI(在msdn上有详细的信息):
硬盘序列号: 用Win32_PhysicalMedia class.
CPU编号: 用Win32_Processor class.
BIOS编号: 用Win32_BIOS class.
下面例子取得硬盘的序列号,其他的用法也类似(msdn上的例子,把Win32_OperatingSystem改成了Win32_PhysicalMedia):
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
int main(int argc, char **argv)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_PhysicalMedia"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for physical media failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
wcout << "Serial Number : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
I. 如何用C#获取硬盘ID编号
同LS BS 4L &易语言LZ的问题应该是没有声明一个RegistryKey...RegistryKey regNew_GetDisk = Registry.LocalMachine.OpenSubKey(@\"\\HardWare\\Devicemap\\Scsi\\Scsi port 0\\Scsi Bus 0\\Target Id\\\\Logical Unit Id 0\", false);string Value = regNew_GetDisk.GetValue(\"Identifier\",\"none\").ToString ();MessageBox.Show(Value);regNew_GetDisk.Close();注意引用Microsoft.Win32 namespace.