100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Windows获取电脑的硬盘属性(转速 类型(HDD SSD))

Windows获取电脑的硬盘属性(转速 类型(HDD SSD))

时间:2020-07-25 04:31:09

相关推荐

Windows获取电脑的硬盘属性(转速 类型(HDD SSD))

获取电脑的硬盘属性(转速,类型(HDD,SSD))

参考的是老外技术论坛网站:/questions/23363115/detecting-ssd-in-windows

头文件.h

#pragma once#include <QString>#include <windows.h>#include <string>//==============判断磁盘格式--获取到的是磁盘格式void BuryPoint_GetDiskType(QStringList& disk_type);//==============判断硬盘类型()//SpindleSpeed//MediaType//The Media Type gives you values://0 Unspecified//3 HDD//4 SSD//5 SCMvoid BuryPoint_GetDiskMediaType(QStringList& disk_media_type);class StorageDevice{public:StorageDevice() {};~StorageDevice() {};std::string DeviceId;int BusType;int HealthStatus;int SpindleSpeed;int MediaType;};

实现文件 .cpp

#include <刚才的头文件名>#include <windows.h>#include <string>#include <iostream>#include <windows.h>;#include <Wbemidl.h>#include <comdef.h>#include <vector>#pragma comment(lib, "wbemuuid.lib")using namespace std;void IntializeCOM(){HRESULT hres;hres = CoInitializeEx(0, COINIT_MULTITHREADED);if (FAILED(hres)){cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;// Program has failed.}// Step 2: --------------------------------------------------// Set general COM security levels --------------------------hres = CoInitializeSecurity(NULL,-1,// COM authenticationNULL, // Authentication servicesNULL, // ReservedRPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication infoEOAC_NONE, // Additional capabilities NULL// Reserved);if (FAILED(hres)){cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;CoUninitialize(); // Program has failed.}}void SetupWBEM(IWbemLocator*& pLoc, IWbemServices*& pSvc){// Step 3: ---------------------------------------------------// Obtain the initial locator to WMI -------------------------HRESULT hres;//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();}// Step 4: -----------------------------------------------------// Connect to WMI through the IWbemLocator::ConnectServer method//IWbemServices *pSvc = NULL;// Connect to the ROOT\\\microsoft\\windows\\storage namespace with// the current user and obtain pointer pSvc// to make IWbemServices calls.hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\microsoft\\windows\\storage"), // Object path of WMI namespaceNULL,// User name. NULL = current userNULL,// User password. NULL = current0, // Locale. NULL indicates currentNULL,// Security flags.0, // Authority (for example, 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();}// Step 5: --------------------------------------------------// Set security levels on the proxy -------------------------hres = CoSetProxyBlanket(pSvc, // Indicates the proxy to setRPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxxRPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxxNULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL,// RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxxNULL, // client identityEOAC_NONE// proxy capabilities );if (FAILED(hres)){cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;pSvc->Release();pLoc->Release();CoUninitialize();}}void BuryPoint_GetDiskMediaType(QStringList& disk_media_type){IWbemLocator *wbemLocator = NULL;IWbemServices *wbemServices = NULL;IntializeCOM();SetupWBEM(wbemLocator, wbemServices);IEnumWbemClassObject* storageEnumerator = NULL;HRESULT hres = wbemServices->ExecQuery(bstr_t("WQL"),bstr_t("SELECT * FROM MSFT_PhysicalDisk"),WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&storageEnumerator);if (FAILED(hres)){//cout << "Query for MSFT_PhysicalDisk. Error code = 0x" << hex << hres << endl;wbemServices->Release();wbemLocator->Release();CoUninitialize();}IWbemClassObject *storageWbemObject = NULL;ULONG uReturn = 0;vector<StorageDevice> storageDevices;while (storageEnumerator){HRESULT hr = storageEnumerator->Next(WBEM_INFINITE, 1, &storageWbemObject, &uReturn);if (0 == uReturn || hr != S_OK){break;}StorageDevice storageDevice;VARIANT deviceId;VARIANT busType;VARIANT healthStatus;VARIANT spindleSpeed;VARIANT mediaType;storageWbemObject->Get(L"DeviceId", 0, &deviceId, 0, 0);storageWbemObject->Get(L"BusType", 0, &busType, 0, 0);storageWbemObject->Get(L"HealthStatus", 0, &healthStatus, 0, 0);storageWbemObject->Get(L"SpindleSpeed", 0, &spindleSpeed, 0, 0);storageWbemObject->Get(L"MediaType", 0, &mediaType, 0, 0);storageDevice.DeviceId = deviceId.bstrVal == NULL ? "" : _bstr_t(deviceId.bstrVal);storageDevice.BusType = busType.uintVal;storageDevice.HealthStatus = healthStatus.uintVal;storageDevice.SpindleSpeed = spindleSpeed.uintVal;storageDevice.MediaType = mediaType.uintVal;//cout << "storageDevice.MediaType " << storageDevice.MediaType;//SpindleSpeed//MediaType//The Media Type gives you values://0 Unspecified//3 HDD//4 SSD//5 SCMif (storageDevice.MediaType == 3){disk_media_type.append("HDD");}else if (storageDevice.MediaType == 4){disk_media_type.append("SSD");}storageDevices.push_back(storageDevice);storageWbemObject->Release();}}

顺便图槽一句国内的论坛(不点名了)真的水,找了半天不知所云,还是老外的靠谱!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。