- #include
#include
#include
#include
- BOOL GetHardwareID(HDEVINFO hDev, SP_DEVINFO_DATA& DeviceInfoData, LPCTSTR pstrHardwareID, int nSize);
-
-
-
-
#define MAX_CLASS_NAME_LEN 128
-
-
extern "C" DWORD WINAPI InstallDriver(LPCTSTR INFFile)
-
{
-
GUID guid;
-
TCHAR szClassName[MAX_CLASS_NAME_LEN];
-
TCHAR szHardwareID[256];
-
DWORD dwError = 0;
-
-
memset(szHardwareID, 0, sizeof(szHardwareID));
-
-
if (!::SetupDiGetINFClass(INFFile, &guid, szClassName, MAX_CLASS_NAME_LEN, NULL))
-
{
-
dwError = GetLastError();
-
-
return dwError;
-
}
-
-
HDEVINFO hDev = INVALID_HANDLE_VALUE;
-
SP_DEVINFO_DATA DeviceInfoData;
-
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
-
-
hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE);
-
-
if (hDev != INVALID_HANDLE_VALUE)
-
{
-
hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);
-
-
if (hDev != INVALID_HANDLE_VALUE)
-
{
-
// 设备已经安装,先删除
-
if (GetHardwareID(hDev, DeviceInfoData, szHardwareID, 255))
-
{
-
::SetupDiCallClassInstaller(DIF_REMOVE, hDev, &DeviceInfoData);
-
}
-
}
-
-
::SetupDiDestroyDeviceInfoList(hDev);
-
}
-
-
if (szHardwareID[0] == _T('\x0') )
-
{
-
// 没有得到HardwareID,使用ClassName
-
szHardwareID[0] = _T('*');
-
#ifdef _UNICODE
-
wcscpy(szHardwareID+1, szClassName);
-
_wcslwr(szHardwareID);
-
#else
-
strcpy(szHardwareID+1, szClassName);
-
_strlwr(szHardwareID);
-
#endif
-
}
-
-
BOOL bSuccess = FALSE;
-
-
hDev = SetupDiCreateDeviceInfoList(&guid, NULL);
-
if (hDev != INVALID_HANDLE_VALUE)
-
{
-
if (::SetupDiCreateDeviceInfo(hDev, szClassName, &guid, NULL, NULL, DICD_GENERATE_ID, &DeviceInfoData))
-
{
-
if (::SetupDiSetDeviceRegistryProperty(hDev,
-
&DeviceInfoData,
-
SPDRP_HARDWAREID,
-
(LPBYTE)szHardwareID,
-
(
-
#ifdef _UNICODE
-
wcslen(szHardwareID)
-
#else
-
strlen(szHardwareID)
-
#endif
-
+ 2 ) * sizeof(TCHAR) ) )
-
{
-
bSuccess = ::SetupDiCallClassInstaller(DIF_REGISTERDEVICE, hDev, &DeviceInfoData);
-
}
-
}
-
}
-
dwError = GetLastError();
-
-
::SetupDiDestroyDeviceInfoList(hDev);
-
-
if (bSuccess)
-
{
-
BOOL bReBoot;
-
-
bSuccess = UpdateDriverForPlugAndPlayDevices(NULL,
-
szHardwareID,
-
INFFile,
-
1,
-
&bReBoot);
-
-
dwError = bSuccess ? 0 : GetLastError();
-
}
-
-
return dwError;
-
}
-
-
extern "C" DWORD WINAPI UninstallDriver(LPCTSTR INFFile)
-
{
-
GUID guid;
-
TCHAR szClassName[MAX_CLASS_NAME_LEN];
-
TCHAR szHardwareID[256];
-
DWORD dwError = 0;
-
-
memset(szHardwareID, 0, sizeof(szHardwareID));
-
-
if (!::SetupDiGetINFClass(INFFile, &guid, szClassName, MAX_CLASS_NAME_LEN, NULL))
-
{
-
dwError = GetLastError();
-
return dwError;
-
}
-
-
HDEVINFO hDev = INVALID_HANDLE_VALUE;
-
SP_DEVINFO_DATA DeviceInfoData;
-
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
-
-
hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE);
-
-
if (hDev != INVALID_HANDLE_VALUE)
-
{
-
hDev = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);
-
-
if (hDev != INVALID_HANDLE_VALUE)
-
{
-
// 设备已经安装,先删除
-
if (GetHardwareID(hDev, DeviceInfoData, szHardwareID, 255))
-
{
-
if (!::SetupDiCallClassInstaller(DIF_REMOVE, hDev, &DeviceInfoData) )
-
dwError = -1;
-
}
-
}
-
-
::SetupDiDestroyDeviceInfoList(hDev);
-
}
-
-
return dwError;
-
}
-
-
BOOL GetHardwareID(HDEVINFO hDev, SP_DEVINFO_DATA& DeviceInfoData, LPCTSTR pstrHardwareID, int nSize)
-
{
-
int nIndex = 0;
-
-
if (::SetupDiEnumDeviceInfo(hDev, nIndex, &DeviceInfoData))
-
{
-
DWORD dwSize = nSize;
-
DWORD dwType;
-
-
if (::SetupDiGetDeviceRegistryProperty(hDev, &DeviceInfoData, SPDRP_HARDWAREID,
-
&dwType, (LPBYTE)pstrHardwareID, nSize, &dwSize))
-
{
-
if (!::SetupDiGetDeviceRegistryProperty(hDev,
-
&DeviceInfoData,
-
SPDRP_HARDWAREID,
-
&dwType,
-
(LPBYTE)pstrHardwareID,
-
nSize,
-
&dwSize))
-
{
-
return FALSE;
-
}
-
}
-
else
-
{
-
return FALSE;
-
}
-
}
-
else
-
{
-
return FALSE;
-
}
-
-
return TRUE;
-
}
其中newdev.h是DDK里面的,如果没有安装也可以自己从newdev.dll获取UpdateDriverForPlugAndPlayDevices的地址
阅读(3141) | 评论(0) | 转发(0) |