首页 技术 正文
技术 2022年11月21日
0 收藏 496 点赞 2,633 浏览 16658 个字
 // InstallWDFDriver.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "Shlwapi.h" #pragma comment(lib,"Shlwapi.lib")
#pragma comment (lib,"setupapi.lib")
#pragma comment(lib, "newdev.lib")
///////////////////////////////////////////////////////////////////////////////////// BOOL IsDeviceInstallInProgress(VOID);
int RemoveDriver(_TCHAR *HardwareID);
VOID UninstallWDMDriver(LPCTSTR theHardware);
BOOL UnicodeToAnsi(LPCWSTR Source, const WORD wLen, LPSTR Destination, const WORD sLen);
BOOL AnsiToUnicode(LPCSTR Source, const WORD sLen, LPWSTR Destination, const WORD wLen);
BOOL GetINFData(FILE *pFile);
VOID FindComma(LPSTR szData);
BOOL GetSectionData(FILE* pFile, const char* szKey, const char bIsVender);
BOOL IsInstalled();
BOOL InstallClassDriver(LPCTSTR theINFName);
BOOL StartInstallWDMDriver(LPCTSTR theInfName) ; BOOL FindExistingDevice(IN LPTSTR HardwareId); VOID InitialGlobalVar(); ////////////////////////////////////////////////////////////////////////////// WORD g_wVender = ;
WORD g_wHardware = ;
TCHAR g_strVender[][] = {};
TCHAR g_strHardware[][] = {};
TCHAR g_strHID[MAX_PATH+] = {};
///////////////////////////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[])
{
WCHAR Wlp_USB_PATH[] = L"C:\\Windows\\System32\\drivers\\WLP_USB_Driver.sys";
WCHAR New_Inf_Path[] = L"D:\\wlp driver\\WDF Driver _new\\win7\\x64\\cyusb3.inf";
CHAR szInfPath[] = {};
FILE *fp = NULL; printf("This Process is Driver Installtion Program...\n"); if(PathFileExists(Wlp_USB_PATH))
{
printf("Exists WLP_USB_Driver.sys!\n"); UninstallWDMDriver(L"USB\\VID_0451&PID_AF32&REV_0000"); if(DeleteFile(L"C:\\Windows\\System32\\drivers\\WLP_USB_Driver.sys"))
{
printf("WLP_USB_Driver.sys File has Deleted!\n");
} /* if(FindExistingDevice(L"USB\\VID_0451&PID_AF32&REV_0000"))
{
printf("找打了!\n"); getch();
}
else
{
printf("没找到!\n");
getch();
}*/ printf("Get Started Install the New DLF Driver...\n"); // RemoveDriver(L"USB\\VID_0451&PID_AF32&REV_0000");
while(IsDeviceInstallInProgress())
{
printf("Has Driver Installtion Program is Processing!\n");
} printf("Get Started analyse inf File!\n"); UnicodeToAnsi(New_Inf_Path, _tcslen(New_Inf_Path), szInfPath, ); if ((fopen_s(&fp, szInfPath, "r"))!=)
{
_tprintf(_T("can not open file %s\n"), New_Inf_Path);
return ;
} GetINFData(fp);
fclose(fp); // 安装WDM驱动 if (StartInstallWDMDriver(New_Inf_Path) == FALSE)
{
_tprintf(_T("Start Install WDF Driver failed\n"));
getch();
return ;
} //if(CopyFile(L"D:\\wlp driver\\WDF Driver _new\\win7\\x64\\cyusb3.sys",L"C:\\Windows\\System32\\drivers\\cyusb3.sys",TRUE))
// printf("New DLF Driver successed!\n");
getch(); }
else
{
printf("No Exists WLP_USB_Driver.sys!\n"); printf("Get Started Install the New DLF Driver...\n"); // RemoveDriver(L"USB\\VID_0451&PID_AF32&REV_0000");
//while(IsDeviceInstallInProgress())
//{
// printf("Has Driver Installtion Program is Processing!\n");
//} printf("Get Started analyse inf File!\n"); UnicodeToAnsi(New_Inf_Path, _tcslen(New_Inf_Path), szInfPath, ); if ((fopen_s(&fp, szInfPath, "r"))!=)
{
_tprintf(_T("can not open file %s\n"), New_Inf_Path);
return ;
} GetINFData(fp);
fclose(fp); // 安装WDM驱动 if (StartInstallWDMDriver(New_Inf_Path) == FALSE)
{
_tprintf(_T("Start Install WDF Driver failed\n"));
getch();
return ;
} // if(CopyFile(L"D:\\wlp driver\\WDF Driver _new\\win7\\x64\\cyusb3.sys",L"C:\\Windows\\System32\\drivers\\cyusb3.sys",TRUE))
// printf("New DLF Driver successed!\n");
getch(); } return ;
} BOOL IsDeviceInstallInProgress (VOID)
{
return !(CM_WaitNoPendingInstallEvents() == WAIT_OBJECT_0);
} int RemoveDriver(_TCHAR *HardwareID)
{
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i,err;
//GUID devGUID ={36fc9e60-c465-11cf-8056-444553540000}; DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
,
,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
printf("GetClassDevs(All Present Devices)\n");
return ;
} //
// Enumerate through all Devices.
//
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = ; //
// 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 = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
printf("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(HardwareID,p))
{
//_tprintf(TEXT("Found! [%s]/n"),p); //
// Worker function to remove device.
//
//if (SetupDiCallClassInstaller(DIF_REMOVE,
// DeviceInfoSet,
// &DeviceInfoData)) if (SetupDiRemoveDevice(DeviceInfoSet, &DeviceInfoData)) {
printf("CallClassInstaller(REMOVE)\n");
}
else
printf("Remove Driver Fail\n");
break;
} //printf("TTTTTTTTTTTTTTTTT is %s\n",p);
//getch();
} if (buffer) LocalFree(buffer);
} if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))
{
printf("EnumDeviceInfo\n");
} //
// Cleanup.
//
cleanup_DeviceInfo:
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet); return err;
} VOID UninstallWDMDriver(LPCTSTR theHardware)
{
SP_DEVINFO_DATA spDevInfoData = {};
HDEVINFO hDevInfo = 0L;
WORD wIdx, wCount = ; //得到设备信息结构的句柄
hDevInfo = SetupDiGetClassDevs(0L, 0L, 0L, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("Fail!\n");
return;
} wIdx = ;
while (TRUE)
{
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//找到所有的硬件设备,并且可以得到所有的硬件设备的详细信息
if (SetupDiEnumDeviceInfo(hDevInfo, wIdx, &spDevInfoData))
{
char Buffer[] = {}; //可以在前面得到的指向某一个具体设备信息集合的指针中取出某一项信息
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_HARDWAREID,
0L, (PBYTE)Buffer, , 0L))
{
if (!_tcscmp(theHardware, (LPTSTR)Buffer))
{
//从系统中删除一个注册的设备接口
if (!SetupDiRemoveDevice(hDevInfo, &spDevInfoData))
printf("Remove Fail is %d!\n",GetLastError());
wCount++;
}
}
}
else
break;
wIdx++;
} if (wCount != )
_tprintf(_T("UnInstall Successed...\n")); //销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
//InitialGlobalVar();
return;
} BOOL UnicodeToAnsi(LPCWSTR Source, const WORD wLen, LPSTR Destination, const WORD sLen)
{
return WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, Source, wLen, Destination,
sLen, 0L, 0L);
} BOOL AnsiToUnicode(LPCSTR Source, const WORD sLen, LPWSTR Destination, const WORD wLen)
{
return MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Source, sLen, Destination, wLen);
} BOOL GetINFData(FILE *pFile)
{
WORD wLoop; if (!g_wVender || !g_wHardware)
InitialGlobalVar();
if (GetSectionData(pFile, "[Manufacturer]", TRUE) == FALSE)
return FALSE; for (wLoop = ; wLoop < g_wVender; wLoop++)
{
CHAR szVender[] = {};
UnicodeToAnsi(g_strVender[wLoop], _tcslen(g_strVender[wLoop]), szVender, );
GetSectionData(pFile, szVender, FALSE);
}
if (g_wHardware != )
{
if (IsInstalled() == TRUE)//如果已经安装
return FALSE;
else
return TRUE;
}
return FALSE;
} VOID InitialGlobalVar()
{
WORD wLoop; g_wVender = g_wHardware = ;
for (wLoop = ; wLoop < ; wLoop++)
{
RtlZeroMemory(g_strVender[wLoop], sizeof(TCHAR)*);
RtlZeroMemory(g_strHardware[wLoop], sizeof(TCHAR)*);
}
} VOID FindComma(LPSTR szData)
{
WORD wLen = (WORD)strlen(szData);
WORD wIdx;
WORD wLoop;
CHAR szTmp[] = {}; for (wIdx = , wLoop = ; wLoop < wLen; wLoop++)
{
if (szData[wLoop] == ',')
szData[wLoop] = '.';
else if (szData[wLoop] == ' ')
continue;
szTmp[wIdx++] = szData[wLoop];
}
memcpy(szData, szTmp, wIdx*sizeof(char));
szData[wIdx] = ;
} VOID StrLTrim(LPSTR szData)
{
LPSTR ptr = szData;
//判断是否为空格
while (isspace(*ptr))
ptr++; if (strcmp(ptr, szData))
{
WORD wLen = (WORD)(strlen(szData) - (ptr - szData));
memmove(szData, ptr, (wLen+)*sizeof(char));
}
} VOID StrRTrim(LPSTR szData)
{
LPSTR ptr = szData;
LPSTR pTmp = NULL; //debug模式下 使用isspace判断中文 需要设置编码
#if defined(WIN32) && defined(_DEBUG)
char* locale = setlocale( LC_ALL, ".OCP" );
#endif while (*ptr != )
{
//判断是否为空格
if (isspace(*ptr))
{
if (!pTmp)
pTmp = ptr;
}
else
pTmp = NULL;
ptr++;
} if (pTmp)
{
*pTmp = ;
memmove(szData, szData, strlen(szData) - strlen(pTmp));
}
} //从字符串右边开始截取字符串
VOID StrRight(LPSTR szData, WORD wCount)
{
WORD wLen = (WORD)strlen(szData) - wCount; if (wCount > 0x7FFF)//负数
wCount = ;
if (wCount >= (WORD)strlen(szData))
return; memmove(szData, szData + wLen, wCount * sizeof(char));
szData[wCount] = ;
} VOID ConvertGUIDToString(const GUID guid, LPSTR pData)
{
CHAR szData[] = {};
CHAR szTmp[] = {};
WORD wLoop; sprintf_s(pData, _countof(szData), "%04X-%02X-%02X-", guid.Data1, guid.Data2, guid.Data3);
for (wLoop = ; wLoop < ; wLoop++)
{
if (wLoop == )
strcat_s(szData, "-");
sprintf_s(szTmp, _countof(szTmp), "%02X", guid.Data4[wLoop]);
strcat_s(szData, szTmp);
} memcpy(pData + strlen(pData), szData, strlen(szData));
} BOOL IsInstalled()
{
HDEVINFO hDevInfo = 0L;
SP_DEVINFO_DATA spDevInfoData = {0L};
WORD wIdx;
BOOL bIsFound; //得到设备信息结构的句柄
hDevInfo = SetupDiGetClassDevs(0L, , , DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs is %d",GetLastError());
return FALSE;
} spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
wIdx = ;
bIsFound = ;
while (++wIdx)
{
//找到所有的硬件设备,并且可以得到所有的硬件设备的详细信息
if (SetupDiEnumDeviceInfo(hDevInfo, wIdx, &spDevInfoData))
{
LPTSTR ptr;
LPBYTE pBuffer = NULL;
DWORD dwData = 0L;
DWORD dwRetVal;
DWORD dwBufSize = 0L; while (TRUE)
{
//可以在前面得到的指向某一个具体设备信息集合的指针中取出某一项信息
dwRetVal = SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_HARDWAREID,
&dwData, (PBYTE)pBuffer, dwBufSize, &dwBufSize);
if (!dwRetVal)
dwRetVal = GetLastError();
else
break;
if (dwRetVal == ERROR_INVALID_DATA)
break;
else if (dwRetVal == ERROR_INSUFFICIENT_BUFFER)
{
if (pBuffer)
LocalFree(pBuffer);
pBuffer = (LPBYTE)LocalAlloc(LPTR, dwBufSize);
}
else
{
printf("SetupDiGetDeviceRegistryProperty is %d",dwRetVal);
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
return FALSE;
}
} if (dwRetVal == ERROR_INVALID_DATA)
continue; for (ptr = (LPTSTR)pBuffer; *ptr && (ptr < (LPTSTR)&pBuffer[dwBufSize]); ptr += _tcslen(ptr) + sizeof(TCHAR))
{
WORD wLoop; for (wLoop = ; wLoop < g_wHardware; wLoop++)
{
if (!_tcscmp(g_strHardware[wLoop], ptr))
{
bIsFound = TRUE;
break;
}
}
}
if (pBuffer)
LocalFree(pBuffer);
if (bIsFound)
break;
}
}
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
return bIsFound;
} //寻找指定的节名 如果找到返回TRUE 反之返回FALSE
BOOL FindSectionName(FILE *pFile, const char *szKey)
{
char szData[] = {}; if (!pFile)
return FALSE; //将文件内部的位置指针重新指向一个流(数据流/文件)的开头
rewind(pFile);
//循环读取文件内容
while (!feof(pFile))
{
//读取一行
fgets(szData, , pFile);
//去除前后空格
StrLTrim(szData);
StrRTrim(szData); if (strcmp(szKey, szData) == )
return TRUE;
}
return FALSE;
} //得到INF文件中节的数量
BOOL GetSectionData(FILE* pFile, const char* szKey, const char bIsVender)
{
char szData[] = {}; if (bIsVender)
strcpy_s(szData, szKey);
else
sprintf_s(szData, _countof(szData), "[%s]", szKey); if (FindSectionName(pFile, szData) == FALSE)
return FALSE; RtlZeroMemory(szData, sizeof(char)*);
while (!feof(pFile))
{
char *str = NULL;
fgets(szData, , pFile);
szData[strlen(szData)-] = ;
StrLTrim(szData);
StrRTrim(szData);
if (!*szData)
continue;
if (szData[] == ';')
continue; if (strchr(szData, '['))
{
StrLTrim(szData);
if (szData[] != ';')
return ;
else
continue;
} if (bIsVender)
str = strchr(szData, '=');
else
str = strchr(szData, ','); if (*str)
{
char szTmp[] = {};
WORD pos = (WORD)(str - szData + ); StrRight(szData, (short)(strlen(szData)-pos));
StrLTrim(szData);
StrRTrim(szData);
FindComma(szData);
if (bIsVender)
{
AnsiToUnicode(szData, strlen(szData), g_strVender[g_wVender++], );
}
else
{
AnsiToUnicode(szData, strlen(szData), g_strHardware[g_wHardware++], );
}
}/* end if */
}
return TRUE;
} //实质性的安装驱动
BOOL InstallClassDriver(LPCTSTR theINFName)
{
GUID guid = {};
SP_DEVINFO_DATA spDevData = {};
HDEVINFO hDevInfo = 0L;
TCHAR className[MAX_CLASS_NAME_LEN] = {};
LPTSTR pHID = NULL;
WORD wLoop;
BOOL bRebootRequired; //取得此驱动的GUID值
if (!SetupDiGetINFClass(theINFName, &guid, className, MAX_CLASS_NAME_LEN, ))
{
printf( "SetupDiGetINFClass is %d\n",GetLastError());
return FALSE;
} //创建设备信息块列表
hDevInfo = SetupDiCreateDeviceInfoList(&guid, );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiCreateDeviceInfoList is %d\n",GetLastError());
return FALSE;
} spDevData.cbSize = sizeof(SP_DEVINFO_DATA);
//创建设备信息块
if (!SetupDiCreateDeviceInfo(hDevInfo, className, &guid, 0L, 0L, DICD_GENERATE_ID, &spDevData))
{
printf("SetupDiCreateDeviceInfo is %d",GetLastError());
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
return FALSE;
} //for (wLoop = 0; wLoop < g_wHardware; wLoop++)
//{
if (pHID)
LocalFree(pHID); pHID = (LPTSTR)LocalAlloc(LPTR, _tcslen(L"USB\\VID_0451&PID_AF32")**sizeof(TCHAR));
if (!pHID)
{
printf("LocalAlloc is %d",GetLastError());
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
return FALSE;
} _tcscpy_s(pHID, _tcslen(L"USB\\VID_0451&PID_AF32")*,
L"USB\\VID_0451&PID_AF32");
//设定硬件ID
if (!SetupDiSetDeviceRegistryProperty(hDevInfo, &spDevData, SPDRP_HARDWAREID, (PBYTE)pHID,
(DWORD)(_tcslen(L"USB\\VID_0451&PID_AF32")**sizeof(TCHAR))))
{
printf("SetupDiSetDeviceRegistryProperty is %d",GetLastError());
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
LocalFree(pHID);
return FALSE;
}
//调用相应的类程序来注册设备
if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE, hDevInfo, &spDevData))
{
printf("SetupDiCallClassInstaller is %d", GetLastError());
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
LocalFree(pHID);
return FALSE;
} bRebootRequired = FALSE;
//安装更新和硬件ID相匹配的驱动程序
if (!UpdateDriverForPlugAndPlayDevices(0L, L"USB\\VID_0451&PID_AF32"
, theINFName,
INSTALLFLAG_FORCE, &bRebootRequired))
{
DWORD dwErrorCode = GetLastError();
//调用相应的类程序来移除设备
if (!SetupDiCallClassInstaller(DIF_REMOVE, hDevInfo, &spDevData))
printf("SetupDiCallClassInstaller(Remove) is %d",GetLastError());
printf("UpdateDriverForPlugAndPlayDevices is %d",(WORD)dwErrorCode);
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
LocalFree(pHID); getch();
return FALSE;
}
LocalFree(pHID);
pHID = NULL; //}
//销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo);
_tprintf(_T("Install Successed\n"));
return TRUE;
} // 安装WDM驱动的测试工作
BOOL StartInstallWDMDriver(LPCTSTR theInfName)
{
HDEVINFO hDevInfo = 0L;
GUID guid = {0L};
SP_DEVINSTALL_PARAMS spDevInst = {0L};
TCHAR strClass[MAX_CLASS_NAME_LEN] = {0L}; //取得此驱动的GUID值
if (!SetupDiGetINFClass(theInfName, &guid, strClass, MAX_CLASS_NAME_LEN, ))
{
printf("SetupDiGetINFClass is %d",GetLastError());
return FALSE;
} //得到设备信息结构的句柄
hDevInfo = SetupDiGetClassDevs(&guid, 0L, 0L, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_PROFILE);
if (!hDevInfo)
{
printf("SetupDiGetClassDevs is %d",GetLastError());
return FALSE;
} spDevInst.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
//获得指定设备的安装信息
if (!SetupDiGetDeviceInstallParams(hDevInfo, 0L, &spDevInst))
{
printf("SetupDiGetDeviceInstallParams is %d",GetLastError());
return FALSE;
} spDevInst.Flags = DI_ENUMSINGLEINF;
spDevInst.FlagsEx = DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
_tcscpy_s(spDevInst.DriverPath, _countof(spDevInst.DriverPath), theInfName); //为设备信息集或者是一个实际的设备信息单元设置或清除类安装参数
if (!SetupDiSetDeviceInstallParams(hDevInfo, , &spDevInst))
{ printf("SetupDiSetDeviceInstallParams is %d",GetLastError());
return FALSE;
} //获取这个设备的驱动程序信息列表
if (!SetupDiBuildDriverInfoList(hDevInfo, , SPDIT_CLASSDRIVER))
{ printf("SetupDiDeviceInstallParams is %d",GetLastError());
return FALSE;
} //销毁一个设备信息集合
SetupDiDestroyDeviceInfoList(hDevInfo); //进入安装设备驱动函数
return InstallClassDriver(theInfName);
} BOOL FindExistingDevice(IN LPTSTR 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
,
,
DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
return printf("GetClassDevs(All Present Devices)"); } //_tprintf(TEXT("Search for Device ID: [%s]/n"),HardwareId); //
// Enumerate through all Devices.
//
Found = FALSE;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR p,buffer = NULL;
DWORD buffersize = ; //
// 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 = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
//
// Unknown Failure.
//
printf("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(HardwareId,p))
{
//_tprintf(TEXT("Found! [%s]/n"),p);
Found = TRUE;
break;
}
} if (buffer) LocalFree(buffer);
if (Found) break;
} if (GetLastError() != NO_ERROR)
{
printf("EnumDeviceInfo");
} //
// Cleanup.
//
cleanup_DeviceInfo:
err = GetLastError();
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
SetLastError(err); return err == NO_ERROR; //???
}

遇到的问题:mainifest文件要改为管理员权限,或者点击exe要为管理员权限才能正确安装

硬件ID有两个,可能是不一样的用途??至今仍未搞明白,我用同一个硬件ID,卸载了之后,就不能安装了。

UpdateDriverForPlugAndPlayDevices function

发生

ERROR_NO_SUCH_DEVINST错误码,意思是找不到此硬件ID的设备,是不是把硬件ID的名字空间删了?

但是我用同一个硬件的另一个硬件ID(因为有两个:USB\VID_0451&PID_AF32&REV_0000

,USB\VID_0451&PID_AF32

)就能安装了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848