列出我的计算机上安装的物理驱动器

发布于 2024-12-26 12:27:04 字数 225 浏览 3 评论 0原文

可能的重复:
如何列出物理磁盘?

什么是“最佳方法”(最快)C++方法列出我的计算机上安装的物理驱动器?有没有一个boost库可以做到这一点?

Possible Duplicate:
How to list physical disks?

What is the "best way" (fastest) C++ way to List physical drives installed on my computer? Is there a boost library to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2025-01-02 12:27:04

使用GetLogicalDriveStrings() 检索所有可用的逻辑驱动器。

#include <windows.h>
#include <stdio.h>


DWORD mydrives = 100;// buffer length
char lpBuffer[100];// buffer for drive string storage

int main()
{
      DWORD test = GetLogicalDriveStrings( mydrives, lpBuffer);

      printf("The logical drives of this machine are:\n\n");

      for(int i = 0; i<100; i++)    printf("%c", lpBuffer[i]);


      printf("\n");
      return 0;
}

或使用GetLogicalDrives()

#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>

// initial value
TCHAR szDrive[ ] = _T(" A:");

int main()
{
  DWORD uDriveMask = GetLogicalDrives();
  printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);
  printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);
  if(uDriveMask == 0)
      printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError());
  else
  {
      printf("\nThis machine has the following logical drives:\n");
  while(uDriveMask)
    {// use the bitwise AND, 1–available, 0-not available
     if(uDriveMask & 1)
        printf("%s\n",szDrive);
     // increment... 
     ++szDrive[1];
      // shift the bitmask binary right
      uDriveMask >>= 1;
     }
    printf("\n ");
   }
   return 0;
}

use GetLogicalDriveStrings() to retrieve all the available logical drives.

#include <windows.h>
#include <stdio.h>


DWORD mydrives = 100;// buffer length
char lpBuffer[100];// buffer for drive string storage

int main()
{
      DWORD test = GetLogicalDriveStrings( mydrives, lpBuffer);

      printf("The logical drives of this machine are:\n\n");

      for(int i = 0; i<100; i++)    printf("%c", lpBuffer[i]);


      printf("\n");
      return 0;
}

or use GetLogicalDrives()

#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>

// initial value
TCHAR szDrive[ ] = _T(" A:");

int main()
{
  DWORD uDriveMask = GetLogicalDrives();
  printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);
  printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);
  if(uDriveMask == 0)
      printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError());
  else
  {
      printf("\nThis machine has the following logical drives:\n");
  while(uDriveMask)
    {// use the bitwise AND, 1–available, 0-not available
     if(uDriveMask & 1)
        printf("%s\n",szDrive);
     // increment... 
     ++szDrive[1];
      // shift the bitmask binary right
      uDriveMask >>= 1;
     }
    printf("\n ");
   }
   return 0;
}
再可℃爱ぅ一点好了 2025-01-02 12:27:04

一种可能性是使用 WMI 枚举 Win32_DiskDrive

One possibility is to use WMI to enumerate the instances of Win32_DiskDrive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文