I Wrote a driver for WIN7 64bits computer ,now the driver can be installed on the computer successfully ,but there is a bug. After installed the driver, the driver should run immediately on the compter.but it wasn;t. after i replug the device the driver started working ,anyone know how to soolve this ?is that i need a restart after installation?please give me direction on how to solve this ,thanks .
here are the code for installedselectedsriver( i think this part of code have some problem,but i can find it):
typedef BOOL (*PINSTALLSELECTEDDRIVER)(
HWND hwndParent,
HDEVINFO DeviceInfoSet,
LPCWSTR Reserved,
BOOL Backup,
PDWORD pReboot
);
DeviceInfoSet = SetupDiGetClassDevs(&guidHID1,
NULL,
0,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if(DeviceInfoSet == INVALID_HANDLE_VALUE)
{
MessageBox(_T("the device is not connected "));
return;
}
SP_DEVICE_INTERFACE_DATA strtInterfaceData;
strtInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
TCHAR InstanceId[MAX_PATH];
DWORD index = 0;
CString temp;
BOOL findFlag = false;
int i = 0;
while(1)
{
if(!SetupDiEnumDeviceInterfaces(DeviceInfoSet,
NULL,
&guidHID,
i,
&strtInterfaceData))
{
break;
}
i++;
if(strtInterfaceData.Flags == SPINT_ACTIVE )
{
DWORD strSize = 0, requiesize = 0;
DWORD nSize= 0;
SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,
&strtInterfaceData,
NULL,
0,
&strSize,
NULL);
requiesize = strSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
strtDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
strtDetailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if(!SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,
&strtInterfaceData,
strtDetailData,
strSize,
&requiesize,
&DeviceInfoData))
{
MessageBox(_T("can not get the interface information"));
free(strtDetailData);
break;
}
if(!SetupDiGetDeviceInstanceId(DeviceInfoSet,&DeviceInfoData,InstanceId,sizeof(InstanceId),&nSize))
{
MessageBox(_T("can not get the instanceid"));
}
}
}
DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
Err = GetLastError();
}
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiOpenDeviceInfo(DeviceInfoSet,
(PCTSTR)InstanceId,
NULL,
0,
&DeviceInfoData)) {
Err = GetLastError();
}
if (!SetupDiSetSelectedDevice(DeviceInfoSet,
&DeviceInfoData)) {
Err = GetLastError();
}
DeviceInstallParams.cbSize = sizeof(DeviceInstallParams);
if (!SetupDiGetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams)) {
Err = GetLastError();
}
DeviceInstallParams.Flags |= DI_ENUMSINGLEINF;
CString path2;
if (FAILED(StringCchCopy(DeviceInstallParams.DriverPath,
SIZECHARS(DeviceInstallParams.DriverPath),
path2)))
{
Err = ERROR_INVALID_PARAMETER;
}
DeviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
if (!SetupDiSetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams))
{
Err = GetLastError();
}
if (!SetupDiBuildDriverInfoList(DeviceInfoSet,
&DeviceInfoData,
SPDIT_COMPATDRIVER))
{
Err = GetLastError();
}
if (!SetupDiCallClassInstaller(DIF_SELECTBESTCOMPATDRV,
DeviceInfoSet,
&DeviceInfoData))
{
Err = GetLastError();
}
DriverInfoData.cbSize = sizeof(DriverInfoData);
if (!SetupDiGetSelectedDriver(DeviceInfoSet,
&DeviceInfoData,
&DriverInfoData))
{
Err = GetLastError();
}
if (GetSystemDirectory(NewDevPath, SIZECHARS(NewDevPath)) == 0) {
Err = GetLastError();
}
if (FAILED(StringCchCat(NewDevPath, SIZECHARS(NewDevPath), TEXT("\\NEWDEV.DLL")))) {
Err = ERROR_INSUFFICIENT_BUFFER;
}
hNewDev = LoadLibrary(NewDevPath);
if (!hNewDev) {
Err = GetLastError();
}
pInstallSelectedDriver = (PINSTALLSELECTEDDRIVER)GetProcAddress(hNewDev, "InstallSelectedDriver");
if (!pInstallSelectedDriver) {
Err = GetLastError();
}
if(!pInstallSelectedDriver(NULL,DeviceInfoSet,NULL,TRUE,&Reboot))
{
DWORD myErr;
myErr = GetLastError();
AfxMessageBox(LPCTSTR(myErr));
}