デバイス インストール アプリケーションが進行中のデバイスのインストールをチェックする方法

デバイス インストール アプリケーションは、インストールを実行する前に、他のデバイス インストール アクティビティが進行中かどうかを判断する必要があります。 この決定を行うには、デバイス インストール アプリケーションが通常はタイムアウト値 0 で CMP_WaitNoPendingInstallEvents を呼び出す必要があります。 この関数の戻り値が、他のインストール アクティビティが保留中であることを示している場合 (たとえば、新しいハードウェアの検出ウィザードがアクティブになっている可能性があります)、デバイス インストール アプリケーションは終了する必要があります。

デバイス インストール アプリケーションを、CMP_WaitNoPendingInstallEventsをサポートしていないプラットフォームと互換性を持たせるには、アプリケーションに次のコードを含める必要があります。

BOOL
IsDeviceInstallInProgress (VOID)
{
    HMODULE hModule;
    CMP_WAITNOPENDINGINSTALLEVENTS_PROC pCMP_WaitNoPendingInstallEvents;

    hModule = GetModuleHandle(TEXT("setupapi.dll"));
    if(!hModule)
    {
        // Should never happen since we're linked to SetupAPI, but...
        return FALSE;
    }

    pCMP_WaitNoPendingInstallEvents =
        (CMP_WAITNOPENDINGINSTALLEVENTS_PROC)GetProcAddress(hModule,
                                             "CMP_WaitNoPendingInstallEvents");
    if(!pCMP_WaitNoPendingInstallEvents)
    {
        // We're running on a release of the operating system that doesn't supply this function.
        // Trust the operating system to suppress AutoRun when appropriate.
        return FALSE;
    }
    return (pCMP_WaitNoPendingInstallEvents(0) == WAIT_TIMEOUT);
}

int
__cdecl
_tmain(IN int argc, IN PTCHAR argv[])
{
    if(IsDeviceInstallInProgress()) {
        //
        // We don't want to start right now.  Instead, our
        // device co-installer will invoke this application
        // (if necessary) during finish-install processing.
        //
        return -1;
    }
    .
    .
}

このコードの使用は、プラットフォームが CMP_WaitNoPendingInstallEventsをサポートしていない場合、インストール アクティビティが進行中の場合、プラットフォームは AutoRun を開始しないという前提に基づいています。

このコードの使用例については、Windows Driver Kit (WDK)の src\general\toaster サブディレクトリにあるトースター インストール パッケージを参照してください。