阅读:7650回复:6
如何安装inf文件?
我手上有一个modem inf文件,用手工添加modem可以成功添加,但不知如何自动安装,请高手指点一下!谢谢!
|
|
沙发#
发布于:2004-12-29 10:45
论坛里以前有这样的帖子的,
自己找找吧。 祝你好运!! |
|
|
板凳#
发布于:2004-12-29 13:51
我找了。是用以下来实现
#define MAX_CLASS_NAME_LEN 32 #define MAX__DESC 256 void DisplayError(TCHAR * ErrorName) /*++ Routine Description: Formats and displays erros in a readable format. Arguments: ErrorName - Supplies a string containing any additional comments to be printed. Return Value: None. --*/ { return; } BOOL PortExists(TCHAR* szPortName) /*++ Routine Description: This routine checks if the port name supplied is a valid one or not Arguments: szPortName - Supplies a string containing a port name (like COM1, COM2) Return Value: The function returns TRUE if it finds a match for port name, otherwise it returns a FALSE. --*/ { TCHAR szDevDesc[MAX__DESC]; BOOL match = FALSE; // GUID const CLASS_GUID = GUID_DEVCLASS_PORTS; GUID const CLASS_GUID = {0x4d36e978L, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}; HDEVINFO hDevInfo; hDevInfo = SetupDiGetClassDevs((LPGUID)&CLASS_GUID, NULL, NULL, DIGCF_PRESENT); if ( INVALID_HANDLE_VALUE != hDevInfo) { SP_DEVINFO_DATA devInfoElem; int index = 0; devInfoElem.cbSize = sizeof(SP_DEVINFO_DATA); while ( SetupDiEnumDeviceInfo(hDevInfo, index++, &devInfoElem)) { HKEY hKeyDev = SetupDiOpenDevRegKey(hDevInfo, &devInfoElem, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if (INVALID_HANDLE_VALUE != hKeyDev) { WORD length = sizeof(szDevDesc); if(ERROR_SUCCESS == RegQueryValueEx(hKeyDev, _T("PortName"), NULL, NULL, (unsigned char *)szDevDesc, (LPDWORD)&length) ) { RegCloseKey(hKeyDev); //printf("ttComparing : %stwitht%sn", szPortName, szDevDesc); if (! _tcsicmp(szPortName, szDevDesc)) { match = TRUE; break; } } } } // while SetupDiDestroyDeviceInfoList(hDevInfo); } return match; } BOOL RegisterModem( IN HDEVINFO hdi, IN PSP_DEVINFO_DATA pdevData, IN LPCTSTR pszPort) /*++ Routine Description: This routine registers a modem device and writes "AttachedTo" string to registry -- this value represents the Port to which Modem is attached. Arguments: hdi -- Device Info Set -- containing the modem Device Info element. pdevData -- Pointer the the modem device information element. szPortName -- Supplies a string containing port name the modem is to be attached to (like COM1, COM2). Return Value: The function returns TRUE if it was able to register the modem and write the "AttachedTo" string to registry, otherwise it returns a FALSE. Note : Once the device is registered using SetupDiRegisterDeviceInfo(...), we need to remove it explicitly in case of an error here after -- may be call SetupDiCallClassInstaller(...) with a DIF_REMOVE function. --*/ { BOOL bRet; SP_DRVINFO_DATA drvData; DWORD nErr = NO_ERROR; DWORD dwRet; TCHAR const c_szAttachedTo[] = _T("AttachedTo"); HKEY hKeyDev; bRet = FALSE; if( !SetupDiRegisterDeviceInfo(hdi, pdevData, 0, NULL, NULL, NULL) ) { DisplayError("Register Device 1n"); return bRet; } hKeyDev = SetupDiOpenDevRegKey(hdi, pdevData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_ALL_ACCESS); //// This call fails.... if( (INVALID_HANDLE_VALUE == hKeyDev) && ( ERROR_KEY_DOES_NOT_EXIST == GetLastError()) ) { hKeyDev = SetupDiCreateDevRegKey(hdi, pdevData, DICS_FLAG_GLOBAL, 0, DIREG_DRV, NULL, NULL); if( INVALID_HANDLE_VALUE == hKeyDev ) { DisplayError(_T("SetupDi Open+Create DevRegKey failed: ")); return FALSE; } } else { DisplayError(_T("Can not open DriverKey: ")); return FALSE; } if (ERROR_SUCCESS != (dwRet = RegSetValueEx (hKeyDev, c_szAttachedTo, 0, REG_SZ, (PBYTE)pszPort, (lstrlen(pszPort)+1)*sizeof(TCHAR)))) { DisplayError(_T("RegSetValueEx :")); SetLastError (dwRet); bRet = FALSE; } RegCloseKey (hKeyDev); if( !SetupDiRegisterDeviceInfo(hdi, pdevData, 0, NULL, NULL, NULL) ) { DisplayError(_T("Register Device 2")); return FALSE; } if ( !SetupDiGetSelectedDriver(hdi, pdevData, &drvData)) { if((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_DRIVER_SELECTED)) DisplayError(_T("SetupDiGetSelectedDriver Failed: ")); else SetLastError(NO_ERROR); } return TRUE; } BOOL FindExistingDevice(IN LPTSTR HardwareId) /*++ Routine Description: This routine finds an existing devnode if present. Arguments: HardwareIdList - Supplies a string containing a hardware ID to be associated with the device. Return Value: The function returns TRUE if it find a devnode with the HardwareID Otherwise it returns FALSE and the logged error can be retrieved with a call to GetLastError. The most common error will be ERROR_NO_MORE_ITEMS, which means the function could not find a devnode with the HardwareID. --*/ { HDEVINFO DeviceInfoSet; SP_DEVINFO_DATA DeviceInfoData; DWORD i,err; BOOL Found; // // 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(_T("GetClassDevs(All Present Devices)")) ; return FALSE; } //_tprintf(_T("nnSearch for Device ID: [%s]nn"),HardwareId); // // Enumerate through all Devices. // Found = FALSE; i = err = 0; DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); //for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++) while ( !Found && SetupDiEnumDeviceInfo(DeviceInfoSet, i++, &DeviceInfoData)) { 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 = (char *)LocalAlloc(LPTR,buffersize); } else { // // Unknown Failure. // DisplayError(_T("GetDeviceRegistryProperty")); err = 1; break; } } if (GetLastError() == ERROR_INVALID_DATA) continue; if( err) break; // // 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(_T("Comparing device ID: [%s]n"),p); if (!_tcscmp(HardwareId,p)) { //_tprintf(_T("Found! [%s]n"),p); Found = TRUE; break; } } if (buffer) LocalFree(buffer); //if (Found) break; } if ((GetLastError() != NO_ERROR)&&(GetLastError() !=ERROR_NO_MORE_ITEMS)) { DisplayError(_T("EnumDeviceInfo")); } // // Cleanup. // err = GetLastError(); SetupDiDestroyDeviceInfoList(DeviceInfoSet); SetLastError(err); return err == NO_ERROR; } BOOL InstallRootEnumeratedDriver(IN LPTSTR HardwareId, IN LPTSTR INFFile, IN LPTSTR MdmPort, OUT PBOOL RebootRequired OPTIONAL ) /*++ Routine Description: This routine creates and installs a new root-enumerated devnode. Arguments: HardwareIdList - Supplies a multi-sz list containing one or more hardware IDs to be associated with the device. These are necessary in order to match up with an INF driver node when we go to do the device installation. InfFile - Supplies the full path to the INF File to be used when installing this device. RebootRequired - Optionally, supplies the address of a boolean that is set, upon successful return, to indicate whether or not a reboot is required to bring the newly-installed device on-line. Return Value: The function returns TRUE if it is successful. Otherwise it returns FALSE and the logged error can be retrieved with a call to GetLastError. --*/ { HDEVINFO DeviceInfoSet = 0; SP_DEVINFO_DATA DeviceInfoData; GUID ClassGUID; TCHAR ClassName[MAX_CLASS_NAME_LEN]; DWORD err; //HKEY hKeyDev; BOOL Remove = FALSE; SetLastError(NO_ERROR); // // Use the INF File to extract the Class GUID. // if (!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0)) { DisplayError(_T("GetINFClass")); return FALSE; } // // Create the container for the to-be-created Device Information Element. // DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0); if(DeviceInfoSet == INVALID_HANDLE_VALUE) { DisplayError(_T("CreateDeviceInfoList")); return FALSE; } do { // // Now create the element. // Use the Class GUID and Name from the INF file. // DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); if (!SetupDiCreateDeviceInfo(DeviceInfoSet, ClassName, &ClassGUID, NULL, 0, DICD_GENERATE_ID, &DeviceInfoData)) { DisplayError(_T("CreateDeviceInfo")); break; } // // Add the HardwareID to the Device's HardwareID property. // if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, (LPBYTE)HardwareId, (lstrlen(HardwareId)+1+1)*sizeof(TCHAR))) { DisplayError(_T("SetDeviceRegistryProperty")); break; } if ( !RegisterModem(DeviceInfoSet, &DeviceInfoData, MdmPort) ) { DisplayError(_T("RegisterModem")); Remove = TRUE; break; } // // Install the Driver. // if (!UpdateDriverForPlugAndPlayDevices(0, HardwareId, INFFile, INSTALLFLAG_FORCE, RebootRequired)) { DisplayError(_T("UpdateDriverForPlugAndPlayDevices")); Remove = TRUE; break; } } while (FALSE); if(Remove) { //Delete Device Instance that was registered using SetupDiRegisterDeviceInfo // May through an error if Device not registered -- who cares?? SetupDiCallClassInstaller( DIF_REMOVE, DeviceInfoSet, &DeviceInfoData); DisplayError(_T("Remove")); } // // Cleanup. // err = GetLastError(); if(err==ERROR_NO_MORE_ITEMS) err=NO_ERROR; SetupDiDestroyDeviceInfoList(DeviceInfoSet); SetLastError(err); return (err == NO_ERROR && !Remove); } extern "C" __declspec(dllexport) int InstallModem(_TCHAR* INFFILENAME,_TCHAR* PORTNAME,_TCHAR* HardwareID) { WIN32_FIND_DATA FindFileData; BOOL RebootRequired = 0; if (FindFirstFile(INFFILENAME,&FindFileData)==INVALID_HANDLE_VALUE) { return 2; // Install Failure } if (FindExistingDevice(HardwareID)) { // // No Need to Create a Device Node, just update the current devnode. // if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle HardwareID, // Hardware ID INFFILENAME, // FileName INSTALLFLAG_FORCE, &RebootRequired)) { DisplayError(_T("UpdateDriverForPlugAndPlayDevices")); return 2; // Install Failure } } else { if (GetLastError()!= ERROR_NO_MORE_ITEMS) { // // An unknown failure from FindExistingDevice() // return 2; // Install Failure } else SetLastError(NO_ERROR); // // Driver Does not exist, Create and call the API. // HardwareID must be a multi-sz string, which argv[2] is. // //test if (!InstallRootEnumeratedDriver(HardwareID, // HardwareID INFFILENAME, // FileName PORTNAME, // Modem Port &RebootRequired)) { return GetLastError();//3; // Install Failure } } //_tprintf(_T("Driver Installed successfully.n")); if (RebootRequired) { //_tprintf(_T("(Reboot Required)n")); return 1; // Install Success, reboot required. } return 0; // Install Success, no reboot required. } 但没有成功不知什么原因,是不是我的方法用错了? 能不能直接在InstallShield中设定,我有INF与SYS文件! 我手工用INF文件添加modem成功了。但现在要自动添加) |
|
地板#
发布于:2004-12-29 13:56
有哪位高手能发一份成功的实例给我!谢谢!
Emil:lzqfree@163.com |
|
地下室#
发布于:2005-01-26 13:54
这位仁兄既然在线,那我向你请教个问题,我看了一个勾子驱动,但我的inf文件写不对,所以安不上,但我又按他说的在注册表里做,也不行,运行net start 的时候,说我路径不对,可我仔细查过了,好像也没什么问题啊.!!
那个代码在这里http://www.xfocus.net/articles/200210/457.html 你帮看看吧,好吗,还有请教你:我在写网络软驱动,该从何入手啊, |
|
5楼#
发布于:2005-01-27 09:14
有哪位高手能发一份成功的实例给我!谢谢! 这个是DDK的一个例子,setup目录里面的 |
|
|
驱动小牛
![]() |
6楼#
发布于:2007-02-07 19:45
非常感谢
|