删除设备,完整程序在DDK的src\general\setup\remove目录下 将程序中argv[1]改成你的硬件ID HDEVINFO DeviceInfoSet; SP_DEVINFO_DATA DeviceInfoData; DWORD i,err; // // Verify the Arguments. // if (argc < 2) { _tprintf(TEXT("usage: remove <Hardware_ID>\n")); return 1; // Remove Failure } // // Create a Device Information Set with all present devices. // DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes 0, 0, DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system if (DeviceInfoSet == INVALID_HANDLE_VALUE) { DisplayError(TEXT("GetClassDevs(All Present Devices)")); return 1; }
// // Enumerate through all Devices. // DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++) { DWORD DataT; LPTSTR p,buffer = NULL; DWORD buffersize = 0;
// // We won't know the size of the HardwareID buffer until we call // this function. So call it with a null to begin with, and then // use the required buffer size to Alloc the nessicary space. // Keep calling we have success or an unknown failure. // while (!SetupDiGetDeviceRegistryProperty( DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INVALID_DATA) { // // May be a Legacy Device with no HardwareID. Continue. // break; } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // // We need to change the buffer size. // if (buffer) LocalFree(buffer); buffer = LocalAlloc(LPTR,buffersize); } else { // // Unknown Failure. // DisplayError(TEXT("GetDeviceRegistryProperty")); goto cleanup_DeviceInfo; } } if (GetLastError() == ERROR_INVALID_DATA) continue;
// // Compare each entry in the buffer multi-sz list with our HardwareID. // for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR)) { _tprintf(TEXT("Compare device ID: [%s]\n"),p); if (!_tcscmp(argv[1],p)) { _tprintf(TEXT("Found! [%s]\n"),p); // // Worker function to remove device. // if (!SetupDiCallClassInstaller(DIF_REMOVE, DeviceInfoSet, &DeviceInfoData)) { DisplayError(TEXT("CallClassInstaller(REMOVE)")); } break; } } if (buffer) LocalFree(buffer); } if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS)) { DisplayError(TEXT("EnumDeviceInfo")); }
i wrote such as this script in installshield. NUMBER nvResult; STRING svResult; STRING szDllName; LPSTR szBuf; NUMBER size; BOOL bCopy; BOOL bDelete; begin ////////////////////////////////////////////// GetSystemInfo (OS,nvResult,svResult); //Get system Operation system type //Disable the component that not for windowsNT or Win2000 if (nvResult = IS_WINDOWSNT) then // XCopyFile (SUPPORTDIR^"xxx.inf",WINDIR^"inf",COMP_NORMAL|LOCKEDFILE); szDllName = WINSYSDIR^"setupapi.dll"; UseDLL(szDllName); bCopy =SetupCopyOEMInfA(SUPPORTDIR^"xxx.inf",NULL,0,8,NULL,0,0,NULL) ; UnUseDLL(szDllName); // if (!bCopy) then // exit; // endif; endif; if (nvResult = IS_WINDOWS9X) then bCopy=XCopyFile (SUPPORTDIR^"xxx.sys",WINDIR^"system32\\drivers",COMP_NORMAL|LOCKEDFILE); bCopy=XCopyFile (SUPPORTDIR^"xxx.inf",WINDIR^"inf",COMP_NORMAL|LOCKEDFILE); szDllName = WINSYSDIR^"kernel32.dll"; UseDLL(szDllName); DeleteFileA(WINDIR^"Inf\\Drvidx.bin"); bDelete =DeleteFileA(WINDIR^"Inf\\Drvdata.bin"); UnUseDLL(szDllName); // if (!bDelete) then // exit; // endif; endif;
将程序中argv[1]改成你的硬件ID
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i,err; //
// Verify the Arguments.
//
if (argc < 2)
{
_tprintf(TEXT("usage: remove <Hardware_ID>\n"));
return 1; // Remove Failure
} //
// Create a Device Information Set with all present devices.
//
DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
0,
0,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
DisplayError(TEXT("GetClassDevs(All Present Devices)"));
return 1;
}
//
// Enumerate through all Devices.
//
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = 0;
//
// We won't know the size of the HardwareID buffer until we call
// this function. So call it with a null to begin with, and then
// use the required buffer size to Alloc the nessicary space.
// Keep calling we have success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INVALID_DATA)
{
//
// May be a Legacy Device with no HardwareID. Continue.
//
break;
}
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
//
// We need to change the buffer size.
//
if (buffer)
LocalFree(buffer);
buffer = LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
DisplayError(TEXT("GetDeviceRegistryProperty"));
goto cleanup_DeviceInfo;
}
} if (GetLastError() == ERROR_INVALID_DATA)
continue;
//
// Compare each entry in the buffer multi-sz list with our HardwareID.
//
for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))
{
_tprintf(TEXT("Compare device ID: [%s]\n"),p); if (!_tcscmp(argv[1],p))
{
_tprintf(TEXT("Found! [%s]\n"),p); //
// Worker function to remove device.
//
if (!SetupDiCallClassInstaller(DIF_REMOVE,
DeviceInfoSet,
&DeviceInfoData))
{
DisplayError(TEXT("CallClassInstaller(REMOVE)"));
}
break;
}
} if (buffer) LocalFree(buffer);
} if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))
{
DisplayError(TEXT("EnumDeviceInfo"));
}
//
// Cleanup.
//
cleanup_DeviceInfo:
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
然后将你的新inf和sys分别复制到相应目录下,同时修改inf文件,将复制文件的部分全部用';'注释掉,插入设备就可以自动找到新的驱动程序了
如果在2000下还可以调用UpdateDriverForPlugAndPlayDevices提前预安装设备
NUMBER nvResult;
STRING svResult;
STRING szDllName;
LPSTR szBuf;
NUMBER size;
BOOL bCopy;
BOOL bDelete;
begin
//////////////////////////////////////////////
GetSystemInfo (OS,nvResult,svResult); //Get system Operation system type
//Disable the component that not for windowsNT or Win2000
if (nvResult = IS_WINDOWSNT) then
// XCopyFile (SUPPORTDIR^"xxx.inf",WINDIR^"inf",COMP_NORMAL|LOCKEDFILE);
szDllName = WINSYSDIR^"setupapi.dll";
UseDLL(szDllName);
bCopy =SetupCopyOEMInfA(SUPPORTDIR^"xxx.inf",NULL,0,8,NULL,0,0,NULL) ;
UnUseDLL(szDllName);
// if (!bCopy) then
// exit;
// endif;
endif;
if (nvResult = IS_WINDOWS9X) then
bCopy=XCopyFile (SUPPORTDIR^"xxx.sys",WINDIR^"system32\\drivers",COMP_NORMAL|LOCKEDFILE);
bCopy=XCopyFile (SUPPORTDIR^"xxx.inf",WINDIR^"inf",COMP_NORMAL|LOCKEDFILE);
szDllName = WINSYSDIR^"kernel32.dll";
UseDLL(szDllName);
DeleteFileA(WINDIR^"Inf\\Drvidx.bin");
bDelete =DeleteFileA(WINDIR^"Inf\\Drvdata.bin");
UnUseDLL(szDllName);
// if (!bDelete) then
// exit;
// endif;
endif;
根据inf将文件拷贝到各自的目录,在安装完成后,
win98下删除drvidx.bin和drvdata.bin
win2000下调用SetupAPI.dll中的SetupCopyOEMInf注册inf文件。欢迎大家继续讨论,我在网上查过,关于这方面的资料不多,写出来应该对大家有用。
NTDDK\src\general\setup下有例子,我没有看过,有兴趣的可拿来一读。