❶ 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.
}
❷ 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;
}
编译完成后,执行效果如下:
❸ 用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
}
❹ 如何获得硬盘序列号
1用硬盘序列号读取软件可以查到,
2
DOS命令行操作:
使用diskpart命令,Win+R键运行cmd,进入命令行界面:
1.diskpart
2.list disk 查看有几块硬盘
3.select disk 0 选择第一块硬盘
4.detail disk 显示选择的硬盘详细信息,显示的第一行是硬盘型号,第二行ID是序列号。
5.若有多块硬盘,返回第3步继续下面操作。
❺ 如何用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# 获取硬盘序列号
有两种方法可以获取:
1.采用WMI可以获取(获取部分硬盘),代码如下:
public static List<string> GetHDIds()
{
List<string> hdIds = new List<string>();
try
{
//ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementClass cimobject = new ManagementClass("Win32_PhysicalMedia");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
//String HDid = (string)mo.Properties["Model"].Value.ToString();//获取的是硬盘名称
String HDid = (string)mo.Properties["SerialNumber"].Value.ToString();
if (!string.IsNullOrEmpty(HDid))
{
hdIds.Add(HDid);
}
}
return hdIds;
}
catch (Exception r)
{
hdIds.Add("无法获得硬盘信息!");
MessageBox.Show("硬盘错误信息:" + r.Message);
return hdIds;
}
}
2:采用一个免费的DLL,DiskID32.dll(网上下载)可以获取大部分硬盘的序列号,和上一种方法结合应该可以获取绝大多数的硬盘序列号,dll可以直接放在Debug目录下,
//[DllImport("DiskID32.dll")]
//public static extern long DiskID32(ref byte DiskModel, ref byte DiskID);
//public static string GetDiskID()
//{
// byte[] DiskModel = new byte[31];
// byte[] DiskID = new byte[31];
// int i;
// string ID = "";
// if (DiskID32(ref DiskModel[0], ref DiskID[0]) != 1)
// {
// for (i = 0; i < 31; i++)
// {
// if (Convert.ToChar(DiskID[i]) != Convert.ToChar(0))
// {
// ID = ID + Convert.ToChar(DiskID[i]);
// }
// }
// ID = ID.Trim();
// }
// else
// {
// Console.WriteLine("获取硬盘序列号出错");
// }
// return ID;
//}
❼ 如何用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
}
❽ 怎样查看硬盘序列号
查看硬盘序列号方法,操作方法如下。
1、首先进入系统按Win+R打开运行面板,输入cmd按回车键。
❾ 如何查看电脑硬盘序列号
1、首先在开始菜单栏中输入cmd。