设备安装应用程序如何检查正在进行的设备安装

设备安装应用程序应在执行设备安装之前确定其他设备安装活动是否正在进行。 若要做出此判断,设备安装应用程序应调用 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 驱动程序工具包 (WDK) 的 src\general\toaster 子目录下的 toaster 安装包。