4

Setup API を使用してデバイスを有効または無効にする方法を知っています。私が知る必要があるのは、この同じ API を使用して、デバイスが有効か無効かを判断できるかということです。Microsoftのdevconはハードウェアの操作にSetup APIを使用し、そのプログラムはデバイスが有効か無効かを通知するため(デバイスマネージャーと同様)、本当の問題はそれをどのように使用するかだと思います。これはどのように行われますか?この時点までのセットアップ API メソッドの調査では、明確な答えは示されていません。

アンディ

4

1 に答える 1

7

MS のこの API は、最も使用されておらず、理解されておらず、文書化されている最悪の API の 1 つです。最初の投稿で述べたように、Setup API を使用してハードウェアを有効/無効にすることができます。そこで、少し時間を取って、ハードウェアのステータスを確認する方法を最終的に見つけた方法をコミュニティに提供しようと思いました.

簡単に言えば、Setup API からこれを行うわけではありません。もちろん、これは理にかなっています。結局のところ、Setup API を使用してデバイスの状態 (有効または無効) を変更できるため、デバイスの現在の状態を判断するには、まったく別の API を使用する必要があります。ここで、Configuration Manager 32 API に入ります。ハードウェアを有効/無効にするには Setup API を使用する必要がありますが、ハードウェアの状態を把握するには ConfigMgr 32 API (#include cfgmgr32.h) を使用する必要があります。理にかなっていますよね?

他の方法もあるかもしれませんが、私がやった方法はこれです。

#include <Windows.h>
#include <cstdlib>
#include <setupapi.h>
#include <cfgmgr32.h>

GUID driveGuid = {0x4d36e967, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};

// first, get a list of hardware you're interested in using Setup API
HDEVINFO hDevs(SetupDiGetClassDevs(&driveGuid, NULL, NULL, DIGCF_PRESENT));
if(INVALID_HANDLE_VALUE == hDevs)
{
    throw std::runtime_error("unable to build a list of hardware");
}

// this struct has the dev instance ID that the CFG MGR API wants.  The struct must be
// must be inited to the size of the struct, the cbSize member, all others should be 0
SP_DEVINFO_DATA devInfo = {sizeof(SP_DEVINFO_DATA)};
DWORD index(0);
LONG devStatus(0), devProblemCode(0);
char devId[256];
memset(devId, 0, 256)
while(SetupDiEnumDeviceInfo(hDevs, index++, &devInfo))
{
    // use Config Mgr to get a nice string to compare against
    CM_Get_Device_ID(devInfo.DevInst, devId, 256, 0);

    // use whatever mechanism you like to search the string to find out
    // if it's the hardware you're after
    if((std::string(devId)).find("MyHardware") != std::string::npos)
    {
        // goody, it's the hardware we're looking for
        CM_Get_DevNode_Status(&devStatus, &devProblemCode, devInfo.DevInst, 0);

        // if the call to getting the status code was successful, do something
        // meaningful with the data returned.  The fun part of this is that the
        // return codes aren't really documented on MSDN.  You'll have to look
        // through the CfgMgr32.h file.  Incidentally, these values are what
        // are shown in the Device Manager when you look at the device's status.
    }
}

SetupDiDestroyDeviceInfoList(hDevs);

ここにあるリストを検索して、対象のハードウェアの GUID を特定する必要があります。これらのいくつかは、少なくとも、さまざまな Windows ヘッダーで事前定義されています。しかし、現時点で私が知っているのはごくわずかで、たまたま偶然見つけただけです。

上記で使用した関数への関連リンク: SetupDiDestroyDevieInfoList CM_Get_DevNode_Status CM_Get_Device_ID SetupDiEnumDeviceInfo SetupDiGetClassDevs SP_DEVINFO_DATA

これが誰かに役立つことを願っています。

于 2012-10-28T00:18:39.887 に答える