描述:
USB驱动安装时setup程序如何使得设备接到PC时不用手动指向某个驱动inf和sys所在地文件夹,而是直接让系统从某个文件夹安装驱动?通过改inf文件的SourceDisksNames字段能否解决?否则是否设置注册表?
解决方案1:
发表于: 2004/7/27 - 10:26
/*************************************************************************************
*
* Name: FindWdmDevice
* Desc: Find the device info
* Para: HardwareId: the device's hardware id
* pDeviceINfoData: Save the device info
* Retn: if find the device info, return DeviceInfoSet that contain the device info,
* Otherwise return INVALID_HANDLE_VALUE
*
***************************************************************************************/
HDEVINFO
FindWdmDevice(
IN LPCTSTR HardwareId,
IN PSP_DEVINFO_DATA pDeviceInfoData
)
{
HDEVINFO DeviceInfoSet;
DWORD i;
DWORD DataT;
LPTSTR p;
CCHAR Buffer[ 1024 ];
DWORD BufferSize = 0;
if( HardwareId == NULL || pDeviceInfoData == NULL )
return INVALID_HANDLE_VALUE;
//
// Create a Device Information Set with all present devices.
//
DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
0,
0,
DIGCF_ALLCLASSES );
if( DeviceInfoSet == INVALID_HANDLE_VALUE )
{
TRACE( "GetClassDevs(All Present Devices) failedn" );
return INVALID_HANDLE_VALUE;
}
TRACE( "Search for Device ID: [%s]n", HardwareId );
pDeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
for ( i=0; SetupDiEnumDeviceInfo( DeviceInfoSet, i, pDeviceInfoData ); i++ )
{
// 获取DEVICE_ID
BufferSize = sizeof( Buffer );
if( !SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
pDeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)Buffer,
BufferSize,
&BufferSize) )
{
// 未知错误
// TRACE1( "GetDeviceRegistryProperty failed: 0x%x", GetLastError() );
continue;
}
// 按字符串比较(一个设备可能有多个ID )
for( p = Buffer; *p && ( p < &Buffer[BufferSize] ); p += lstrlen( p ) + sizeof( TCHAR ) )
{
// TRACE1( "Device: %s", p );
if (!_tcsicmp( HardwareId, p ) )
{
// 找到了指定的设备
return DeviceInfoSet;
}
}
}// for
// 没找到
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return INVALID_HANDLE_VALUE;
}
typedef BOOL ( WINAPI *FPUpdatePNPDevice )( HWND hwndParent, LPCTSTR HardwareId, LPCTSTR FullInfPath, DWORD InstallFlags, PBOOL bRebootRequired OPTIONAL );
typedef BOOL ( WINAPI *FPSetupCopyOEMInf )( LPCTSTR, LPCTSTR, DWORD, DWORD, LPTSTR, DWORD, PDWORD, LPTSTR* );
/*************************************************************************************
*
* Name: UpdateWinNTDriver
* Desc: Update the device driver under WindowsNT based os
* Para: InfPath: the device's inf file path, must include full path and file name
* HardwareId: the device's hardware id
* pisNeedReboot: ture means need restart the windows to complete the install
* Retn: return TRUE means install success, otherwise install failed
*
***************************************************************************************/
BOOL
UpdateWinNTDriver(
IN LPCTSTR InfPath,
IN LPCTSTR HardwareId,
IN OUT PBOOL pisNeedReboot
)
{
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
FPUpdatePNPDevice fpUpdatePNPDevice;
FPSetupCopyOEMInf fpSetupCopyOEMInf;
HMODULE hModule;
BOOL bRet = FALSE;
DWORD dwErrCode;
DeviceInfoSet = FindWdmDevice( HardwareId, &DeviceInfoData );
if( DeviceInfoSet == INVALID_HANDLE_VALUE )
{
hModule = LoadLibrary( "SetupApi.dll" );
if( hModule != NULL )
{
fpSetupCopyOEMInf = ( FPSetupCopyOEMInf )
GetProcAddress( hModule, "SetupCopyOEMInfA" );
if( fpSetupCopyOEMInf == NULL )
{
TRACE1( "Get SetupCopyOEMInf function failed: 0x%x", GetLastError() );
bRet = FALSE;
}
else
{
bRet = ( *fpSetupCopyOEMInf )( InfPath, NULL, SPOST_PATH, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL );
if( !bRet )
{
dwErrCode = GetLastError();
TRACE1( "SetupCopyOEMInf failed: 0x%x", dwErrCode );
if( dwErrCode == ERROR_FILE_EXISTS )
{
TRACE0( "The inf file exists" );
bRet = TRUE;
}
}
}
FreeLibrary( hModule );
}
return bRet;
}
SetupDiDestroyDeviceInfoList( DeviceInfoSet );
hModule = LoadLibrary( "newdev.dll" );
if( hModule != NULL )
{
fpUpdatePNPDevice = ( FPUpdatePNPDevice )
GetProcAddress( hModule, "UpdateDriverForPlugAndPlayDevicesA" );
if( fpUpdatePNPDevice == NULL )
{
TRACE0( "UpdateDriverForPlugAndPlayDevice function not find" );
FreeLibrary( hModule );
return FALSE;
}
bRet = ( *fpUpdatePNPDevice )( GetDesktopWindow(),
HardwareId,
InfPath,
INSTALLFLAG_FORCE,
pisNeedReboot
);
if( !bRet )
{
TRACE1( "UpdateDriverForPlugAndPlayDevice failed: 0x%x", GetLastError() );
}
FreeLibrary( hModule );
}
return bRet;
}
/*************************************************************************************
*
* Name: UpdateWin9xDriver
* Desc: Update the device driver under Windows9x based os
* Para: InfPath: the device's inf file path, must include full path and file name
* HardwareId: the device's hardware id
* pisNeedReboot: ture means need restart the windows to co