1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows の物理ドライブを列挙する

Last updated at Posted at 2025-04-12

1. はじめに

需要があるかどうかはわかりませんが、Windowsの物理ドライブを列挙するプログラムです。
呼び出すと "\\.\PhisycalDriveX" が vectorに入って返ってきます。

QueryDosDevice に必要なサイズが事前にわからないので、十分なサイズになるまでループするようにしています。たいていは1回で済むはずです。

EnumPhisicalDrive.cpp

std::vector<std::string> EnumPhysicalDrive()
{
    // 結果を保存するベクタ
    std::vector<std::string>    enumResult;

    static std::string  search = "PhysicalDrive";
    static std::string  prefix = "\\\\.\\";

    // デバイス名を保存するバッファ
    std::unique_ptr<char[]> buffer;
    const DWORD maxBufferSizeStep = 32768;
    DWORD maxBufferSize = maxBufferSizeStep;

    DWORD dwQueryResult;
    do {
        buffer.reset(new char[maxBufferSize]);
        dwQueryResult = QueryDosDeviceA(NULL, buffer.get(), maxBufferSize);
        maxBufferSize += maxBufferSizeStep;
    } while (dwQueryResult == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

    char* deviceName = buffer.get();
    while (*deviceName) {
        std::string strDevice = deviceName;
        if (strDevice.find(search) != std::string::npos) {
            enumResult.push_back(prefix + strDevice);
        }
        // 次のデバイス名へ移動
        deviceName += strlen(deviceName) + 1;
    }
    return enumResult;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?