21xrx.com
2024-12-22 17:19:01 Sunday
登录
文章检索 我的文章 写文章
如何使用C++获取硬盘代码
2023-07-06 10:17:16 深夜i     --     --
C++ 硬盘 获取 代码

当需要了解硬盘的状态或信息时,编写一个硬盘代码是一种有效的方法。在C++编程中,我们可以使用以下方法来获取硬盘代码:

1. 使用API函数

API函数提供了访问系统硬件的方法。在Windows系统中,我们可以使用以下API函数来获取硬盘信息:

- GetDiskFreeSpace:获取硬盘的可用空间

- GetFileInformationByHandle:获取硬盘文件的信息

- GetVolumeInformation:获取硬盘卷标和序列号等信息

2. 使用WMI

WMI(Windows Management Instrumentation)提供了一种访问系统信息的方法。我们可以使用WMI C++库来在C++程序中获取硬盘信息。

下面是一个使用WMI获取硬盘信息的示例程序:


#include <iostream>

#include <comdef.h>

#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

using namespace std;

int main()

{

  HRESULT hres;

  // Initialize COM.

  hres = CoInitializeEx(0, COINIT_MULTITHREADED);

  if (FAILED(hres))

  

    cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;

    return 1;

  

  // Initialize security.

  hres = CoInitializeSecurity(

    NULL,

    -1,

    NULL,

    NULL,

    RPC_C_AUTHN_LEVEL_DEFAULT,

    RPC_C_IMP_LEVEL_IMPERSONATE,

    NULL,

    EOAC_NONE,

    NULL

  );

  if (FAILED(hres))

  {

    cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;

    CoUninitialize();

    return 1;

  }

  // Obtain WMI initial namespace.

  IWbemLocator* pLoc = NULL;

  hres = CoCreateInstance(

    CLSID_WbemLocator,

    0,

    CLSCTX_INPROC_SERVER,

    IID_IWbemLocator, (LPVOID*)&pLoc);

  if (FAILED(hres))

  {

    cout << "Failed to create IWbemLocator object. Error code = 0x" << hex << hres << endl;

    CoUninitialize();

    return 1;

  }

  IWbemServices* pSvc = NULL;

  // Connect to the root\cimv2 namespace with the current user and obtain pointer to the namespace.

  hres = pLoc->ConnectServer(

    _bstr_t(L"ROOT\\CIMV2"),

    NULL,

    NULL,

    0,

    NULL,

    0,

    0,

    &pSvc

  );

  if (FAILED(hres))

  {

    cout << "Could not connect. Error code = 0x" << hex << hres << endl;

    pLoc->Release();

    CoUninitialize();

    return 1;

  }

  // Set the security to the connection

  hres = CoSetProxyBlanket(

    pSvc,

    RPC_C_AUTHN_DEFAULT,

    RPC_C_AUTHZ_DEFAULT,

    NULL,

    RPC_C_AUTHN_LEVEL_CALL,

    RPC_C_IMP_LEVEL_IMPERSONATE,

    NULL,

    EOAC_NONE

  );

  if (FAILED(hres))

  {

    cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;

    pSvc->Release();

    pLoc->Release();

    CoUninitialize();

    return 1;

  }

  // Use the IWbemServices pointer to make requests of WMI

  IEnumWbemClassObject* pEnumerator = NULL;

  hres = pSvc->ExecQuery(

    _bstr_t("WQL"),

    _bstr_t("SELECT * FROM Win32_LogicalDisk"),

    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

    NULL,

    &pEnumerator

  );

  if (FAILED(hres))

  {

    cout << "Query for logical disks failed. Error code = 0x" << hex << hres << endl;

    pSvc->Release();

    pLoc->Release();

    CoUninitialize();

    return 1;

  }

  // Get the data from the query

  IWbemClassObject* pclsObj = NULL;

  ULONG uReturn = 0;

  while (pEnumerator)

  {

    hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

    if (0 == uReturn)

    

      break;

    

    VARIANT vtProp;

    // Get the property value.

    hres = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);

    wcout << " DeviceID : " << vtProp.bstrVal << endl;

    VariantClear(&vtProp);

    hres = pclsObj->Get(L"FreeSpace", 0, &vtProp, 0, 0);

    wcout << " FreeSpace : " << vtProp.uintVal << endl;

    VariantClear(&vtProp);

    hres = pclsObj->Get(L"Size", 0, &vtProp, 0, 0);

    wcout << " Size : " << vtProp.uintVal << endl;

    VariantClear(&vtProp);

    hres = pclsObj->Get(L"VolumeSerialNumber", 0, &vtProp, 0, 0);

    wcout << " VolumeSerialNumber : " << vtProp.bstrVal << endl;

    VariantClear(&vtProp);

    cout << endl;

    pclsObj->Release();

  }

  // Cleanup

  pSvc->Release();

  pLoc->Release();

  pEnumerator->Release();

  CoUninitialize();

  return 0;

}

这个程序使用WMI连接和查询Win32_LogicalDisk类来获取硬盘信息。它将查询返回的硬盘信息输出到控制台上。

总结

使用API函数和WMI是获取硬盘信息的常用方法。这些方法可以帮助你编写C++代码来获取硬盘信息,以便你更好地了解你的系统并进行必要的操作。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复