這工作:
// szInfDirectory is a directory on the hard drive where your installer copied the driver files to.
TCHAR szInfPath[MAX_PATH];
_tcscpy(szInfPath, szInfDirectory);
_tcscat(szInfPath, _T("YourDriver.inf"));
TCHAR szDestinationInfFileName[MAX_PATH];
if((!SetupCopyOEMInf(szInfPath, szInfDirectory, SPOST_PATH, 0, szDestinationInfFileName, MAX_PATH, NULL, NULL)))
{
nResult = ERR_COPYINF_FAILED;
return;
}
LPCTSTR HardwareIds[] =
{
_T("USB\\Vid_123f&Pid_0444"),
_T("USB\\Vid_123f&Pid_0555"),
};
const size_t cbMax = sizeof(HardwareIds)/sizeof(*HardwareIds);
bool fInnerLoopFailed = false;
for(size_t cb=0; (cb<cbMax) && (!fInnerLoopFailed); cb++)
{
BOOL bRebootReqTemp = FALSE;
if((!UpdateDriverForPlugAndPlayDevices(NULL, HardwareIds[cb], szInfPath, INSTALLFLAG_FORCE, &bRebootReqTemp)))
{
if(ERROR_NO_SUCH_DEVINST == GetLastError())
{
// nothing to do: device not present
}
else
{
nResult = ERR_UPDATE_DRIVER_FAILED;
fInnerLoopFailed = true;
break;
}
}
}
if(fInnerLoopFailed)
{
// error
return;
}
// success
應該指出的是,這種解決方案僅適用於32位系統,需要newdev的.lib/newdev.h – mojuba 2014-09-24 20:11:52