ドライブボリュームのシリアルっぽいものを取得

# 概要
仕事で使うからノート。

# コード
ノートだから、説明もなし。 この中の情報から適当にやればいけるだろ。

```cpp
#include <Windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include <string>

DEFINE_GUID(GUID_DEVINTERFACE_USB_DISK, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);

DWORD getDeviceNumber(HANDLE handle)
{
    STORAGE_DEVICE_NUMBER deviceNumber;
    deviceNumber.DeviceNumber = 1;
    DWORD bytesReturned = 0;
    return DeviceIoControl(
        handle,
        IOCTL_STORAGE_GET_DEVICE_NUMBER,
        NULL,
        0,
        &deviceNumber,
        sizeof(STORAGE_DEVICE_NUMBER),
        &bytesReturned,
        NULL)? deviceNumber.DeviceNumber : _UI32_MAX;
}

bool getDeviceInfo(std::string& deviceInfo, INT volume)
{
    char devicePath[] = "\\\\.\\@:";
    devicePath[4] = (char)volume;

    HANDLE device = CreateFileA(
        devicePath,
        0,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
    if(INVALID_HANDLE_VALUE == device){
        return false;
    }
    DWORD volumeDeviceNumber = getDeviceNumber(device);
    CloseHandle(device);

    HDEVINFO hDevInfo = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_USB_DISK, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if(INVALID_HANDLE_VALUE == hDevInfo){
        return false;
    }
    const DWORD MaxDeviceDetailSize = 512;
    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    BYTE detailBuffer[MaxDeviceDetailSize];
    PSP_INTERFACE_DEVICE_DETAIL_DATA deviceInterfaceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)detailBuffer;

    bool result = false;
    for(DWORD index=0; true; ++index){
        deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DISK, index, &deviceInterfaceData)){
            break;
        }
        DWORD size = 0;
        SetupDiGetDeviceInterfaceDetailA(hDevInfo, &deviceInterfaceData, NULL, 0, &size, NULL);
        if(MaxDeviceDetailSize<size){
            continue;
        }
        ZeroMemory(deviceInterfaceDetail, size);
        deviceInterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
        if(!SetupDiGetDeviceInterfaceDetailA(hDevInfo, &deviceInterfaceData, deviceInterfaceDetail, size, &size, NULL)){
            continue;
        }
        HANDLE drive = CreateFileA(deviceInterfaceDetail->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if(INVALID_HANDLE_VALUE == drive){
            continue;
        }
        DWORD usbDeviceNumber = getDeviceNumber(drive);
        CloseHandle(drive);
        if(usbDeviceNumber == volumeDeviceNumber){
            deviceInfo.assign(deviceInterfaceDetail->DevicePath);
            result = true;
            break;
        }
    }
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return result;
}

int main(int argc, char** argv)
{
    CHAR volumeName[MAX_PATH] = "";
    CHAR volumePath[MAX_PATH] = "";
    DWORD volumePathLength;
    DWORD volumeSerial;
    HANDLE handle = FindFirstVolumeA(volumeName, MAX_PATH);
    if(INVALID_HANDLE_VALUE == handle){
        return 0;
    }
    std::string deviceInfo;
    for(;;){
        if(!GetVolumePathNamesForVolumeNameA(volumeName, volumePath, MAX_PATH, &volumePathLength)){
            break;
        }
        if(GetVolumeInformationA(volumePath, NULL, 0, &volumeSerial, NULL, NULL, NULL, 0)){
            printf("name=%s, path=%s, volume serial=%X\n", volumeName, volumePath, volumeSerial);
            if(getDeviceInfo(deviceInfo, volumePath[0])){
                printf("    %s\n", deviceInfo.c_str());
            }
        }
        if(!FindNextVolumeA(handle, volumeName, MAX_PATH)){
            break;
        }
    }
    FindVolumeClose(handle);
    return 0;
}
```