如何在 Windows 上获取磁盘的标识符?
Posted
技术标签:
【中文标题】如何在 Windows 上获取磁盘的标识符?【英文标题】:How can I acquire a disk's identifier on Windows? 【发布时间】:2012-04-06 10:37:12 【问题描述】:如何在 Windows 上检索磁盘的标识符?不要与卷标识符混淆;他们是两个不同的东西。磁盘标识符是驻留在 MBR 中的 4 字节标识符(如果使用 GPT,则为 16 字节标识符)。如果您运行 diskpart 并查询磁盘的详细信息,则它是标记为“磁盘 ID”的值。
我已经浏览了所有看起来相关的 MSDN 文档,但我还没有找到能够做到这一点的任何东西;不过,显然有可能,因为 diskpart 能够从某个地方获取此值。
我总是可以调用 diskpart 作为最后的手段并解析它的输出,但我真的更愿意避免这样做。有谁知道我如何以编程方式获得这个数字?
【问题讨论】:
你可以打开\\.\PhysicalDrive0
的文件句柄并直接读取MBR,假设你有管理员权限。
【参考方案1】:
您可以使用Win32_DiskDrive WMi 类和Signature
属性。
查看此示例应用
#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
#pragma argsused
int main(int argc, char* argv[])
BSTR strNetworkResource;
//To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource
strNetworkResource = L"\\\\.\\root\\CIMV2";
COAUTHIDENTITY *userAcct = NULL ;
COAUTHIDENTITY authIdent;
// Initialize COM. ------------------------------------------
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
// Set general COM security levels --------------------------
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;
cout << _com_error(hres).ErrorMessage() << endl;
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
// 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;
cout << _com_error(hres).ErrorMessage() << endl;
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(strNetworkResource), // 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;
cout << _com_error(hres).ErrorMessage() << endl;
pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
cout << "Connected to root\\CIMV2 WMI namespace" << endl;
// 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;
cout << _com_error(hres).ErrorMessage() << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
// Use the IWbemServices pointer to make requests of WMI ----
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery( L"WQL", L"SELECT * FROM Win32_DiskDrive",
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hres))
cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl;
cout << _com_error(hres).ErrorMessage() << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 1; // Program has failed.
// Secure the enumerator proxy
hres = CoSetProxyBlanket(
pEnumerator, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
userAcct, // client identity
EOAC_NONE // proxy capabilities
);
// Get the data from the WQL sentence
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn || FAILED(hr))
break;
VARIANT vtProp;
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);// String
if (!FAILED(hr))
if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY))
wcout << "Name : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl;
else
if ((vtProp.vt & VT_ARRAY))
wcout << "Name : " << "Array types not supported (yet)" << endl;
else
wcout << "Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
hr = pclsObj->Get(L"Signature", 0, &vtProp, 0, 0);// Uint32
if (!FAILED(hr))
if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY))
wcout << "Signature : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl;
else
if ((vtProp.vt & VT_ARRAY))
wcout << "Signature : " << "Array types not supported (yet)" << endl;
else
wcout << "Signature : " << hex << vtProp.uintVal << endl;
VariantClear(&vtProp);
pclsObj->Release();
pclsObj=NULL;
// Cleanup
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
if (pclsObj!=NULL)
pclsObj->Release();
CoUninitialize();
cout << "press enter to exit" << endl;
cin.get();
return 0; // Program successfully completed.
此代码返回
和磁盘部分
【讨论】:
我特别希望避免 WMI,因为代码太糟糕了,但看起来别无选择。谢谢。 @CollinDauphinee: 不必使用WMI,IOCTL_DISK_GET_DRIVE_LAYOUT_EX
io 控制代码将返回Mbr.Signature
或Gpt.DiskId
中的值。【参考方案2】:
您应该可以使用WMI 来完成此任务。
您寻找的数据可能是Win32_DiskDrive 类的DeviceID
成员中的数据,否则您将不得不进行实验:)
【讨论】:
【参考方案3】:在命令行中运行以下代码获取附加到系统的磁盘的 MBR 签名(十进制):
wmic diskdrive get signature
在命令行中运行以下代码获取系统上设备的磁盘 ID:
wmic diskdrive get serialnumber
注意:如果外部驱动器(例如 Pendrive)连接到您的机器,它将在上述命令的输出中列出。
【讨论】:
以上是关于如何在 Windows 上获取磁盘的标识符?的主要内容,如果未能解决你的问题,请参考以下文章
设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识
设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识 zz
如何在 Windows 10 Universal 中获取设备的唯一标识符?