當前位置:首頁 » 硬碟大全 » c獲取硬碟序列號
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c獲取硬碟序列號

發布時間: 2022-10-01 14:38:14

❶ 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。