WMI returns information in different formats

爷,独闯天下 提交于 2020-01-17 11:17:52

问题


I want to read the hardware configuration to check if a license for my software is valid. Currently, I tried using WMI. This works fine on many machines for several weeks but sometimes, without an obvious reason, WMI returns the hardware configuration in a different format. For example, the serial number of the primary hard disc is converted from characters to a hex string, with all character hex values being swapped pair wise. I figured out that different Windows user types (admin/normal) influence the format but it also changes in other situations and in different ways, for which I am unable to figure out a pattern.

Does anybody know how to reliably check the hardware configuration using WMI? Or would it be possible to avoid the above problem using MFC?


回答1:


WMI is indeed unreliable. You should avoid using it when you don't need it.

Here's one way without WMI:

#include <string>
#include <Dbt.h>
#include <winioctl.h>
#include <SetupAPI.h>
#pragma comment(lib, "SetupAPI.lib")

#include <initguid.h>

DWORD getDeviceNumber(HANDLE hDeviceHandle)
{
    STORAGE_DEVICE_NUMBER sdn = { 0 };
    sdn.DeviceNumber = -1;
    DWORD dwBytesReturned = 0;
    if (!DeviceIoControl(hDeviceHandle, IOCTL_STORAGE_GET_DEVICE_NUMBER, nullptr, 0, &sdn, sizeof(sdn), &dwBytesReturned, nullptr))
    {
        return -1; //Error
    }
    return sdn.DeviceNumber;
}

bool GetDeviceString(std::wstring &out)
{
    wchar_t wDevicePath[] = L"\\\\.\\@:";
    wDevicePath[4] = L'C'; //Replace this with your drive-letter & adjust code (C: / D: whatever)
    HANDLE deviceHandle = CreateFileW(wDevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
    if (deviceHandle == INVALID_HANDLE_VALUE)
        return false;
    DWORD dwVolumeDeviceNumber = getDeviceNumber(deviceHandle);
    CloseHandle(deviceHandle);
    HDEVINFO hDevInfo = SetupDiGetClassDevsW(&GUID_DEVINTERFACE_DISK, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (hDevInfo == INVALID_HANDLE_VALUE)
        return false; //Error
    std::vector<BYTE> buf(1024);
    PSP_DEVICE_INTERFACE_DETAIL_DATA_W psp = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA_W>(buf.data());
    SP_DEVICE_INTERFACE_DATA spInt;
    SP_DEVINFO_DATA spDev;
    spInt.cbSize = sizeof(spInt);

    DWORD dwIndex = 0;
    while (true)
    {
        if (!SetupDiEnumDeviceInterfaces(hDevInfo, nullptr, &GUID_DEVINTERFACE_DISK, dwIndex, &spInt))
            break;
        DWORD dwSize = 0;
        SetupDiGetDeviceInterfaceDetailW(hDevInfo, &spInt, nullptr, 0, &dwSize, nullptr);
        if (dwSize && dwSize <= buf.size())
        {
            psp->cbSize = sizeof(*psp);
            memset(&spDev, sizeof(spDev), 0);
            spDev.cbSize = sizeof(spDev);

            long res = SetupDiGetDeviceInterfaceDetailW(hDevInfo, &spInt, psp, dwSize, &dwSize, &spDev);
            if (res)
            {
                HANDLE hDrive = CreateFileW(psp->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
                if (hDrive != INVALID_HANDLE_VALUE)
                {
                    DWORD dwUsbDeviceNumber = getDeviceNumber(hDrive);
                    if (dwUsbDeviceNumber == dwVolumeDeviceNumber)
                    {
                        //Found
                        out = psp->DevicePath;
                        break;
                    }
                }
                CloseHandle(hDrive);
            }
        }
        ++dwIndex;
    }
    SetupDiDestroyDeviceInfoList(hDevInfo);

    if (out.empty()) //Was not found
        return false;
    return true;
}

After that, you will get a large device string. You might want to read the needed information out of it.
Check the following regular expressions to retrieve these:
(Note that the string CAN change, depending on the device-type, so test it and add/adjust the regular expressions - these are from an USB-stick test)

ven_([^&#]+)    //Vendor String/ID
prod_([^&#]+)   //Product String/ID
rev_([^&#]+)    //Revision String/ID
&[^#]*#([^&#]+) //Serial String/Number

Regular expressions ? Another example:

std::wregex (see std::basic_regex...).
std::wsmatch (see std::match_results...)

std::wstring wsDeviceString;
if (GetDeviceString(wsDeviceString))
{
    std::wregex regexSerialNumber(L"&[^#]*#([^&#]+)");
    std::wsmatch match;
    if (std::regex_search(wsDeviceString, match, regexSerialNumber))
        std::wcout << L"Serial Number of device is: " << match[1].str() << std::endl;
}

One license for your product please =)



来源:https://stackoverflow.com/questions/34785587/wmi-returns-information-in-different-formats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!