遍历注册表得到所有USB驱动

来源:互联网 发布:南瑞科技 知乎 编辑:程序博客网 时间:2024/05/20 08:43

遍历注册表得到所有USB驱动,并对比是否存在指定驱动,这是为了解决USB驱动重复安装会导致鼠标键盘不可用问题,存在跟驱动包相同的版本,则不安装


// bios.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"//#include "windows.h"#pragma warning(disable:4996)#pragma comment (lib,"Advapi32.lib")#include <afx.h>BOOL USBDriverWin7 = FALSE;//  ErrorMessage support function.//  Retrieves the system error message for the GetLastError() code.//  Note: caller must use LocalFree() on the returned LPCTSTR buffer.static LPCTSTR ErrorMessage(DWORD error){LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS,NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL);return((LPCTSTR)lpMsgBuf);}//  PrintError support function.//  Simple wrapper function for error output.static void PrintError(LPCTSTR errDesc){LPCTSTR errMsg = ErrorMessage(GetLastError());_tprintf(TEXT("\n** ERROR ** %s:%d, %s\n"), errDesc, GetLastError(), errMsg);//CString text;//text.Format(_T("%u"), GetLastError());//CString str = errDesc;//str = str + text;//str = str + errMsg;//AfxMessageBox(str);LocalFree((LPVOID)errMsg);}int main(int argc, char* argv[]){HKEY hKEYtmp;HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭  //打开与路径data_Set相关的hKEYLPCTSTR data_Set = _T("SYSTEM\\ControlSet001\\Control\\Class\\{36FC9E60-C465-11CF-8056-444553540000}");//访问注册表,hKEY则保存此函数所打开的键的句柄if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_READ, &hKEY) != ERROR_SUCCESS){PrintError(_T("RegOpenKeyEx:"));_tprintf(_T("Error: RegOpenKeyEx unable to query information about the registry\n"));return 1;}DWORD dwSubKeyCnt;          // 子键的数量  DWORD dwSubKeyNameMaxLen;   // 子键名称的最大长度(不包含结尾的null字符)  DWORD dwKeyValueCnt;        // 键值项的数量  DWORD dwKeyValueNameMaxLen; // 键值项名称的最大长度(不包含结尾的null字符)  DWORD dwKeyValueDataMaxLen; // 键值项数据的最大长度(in bytes)   if (RegQueryInfoKey(hKEY,NULL,NULL,NULL,&dwSubKeyCnt,&dwSubKeyNameMaxLen,NULL,&dwKeyValueCnt,&dwKeyValueNameMaxLen,&dwKeyValueDataMaxLen,NULL,NULL) != ERROR_SUCCESS ) // Error  {PrintError(_T("RegQueryInfoKey:"));_tprintf(_T("Error:RegQueryInfoKey unable to query information about the registry\n"));return 1;}//  // 以下代码中的变量dwSubKeyNameMaxLen&dwSubKeyCnt由  // RegQueryInfoKey获取  //  LPTSTR lpszSubKeyName = new TCHAR[dwSubKeyNameMaxLen + 1];for (int index = 0; index < dwSubKeyCnt; ++index){hKEYtmp = hKEY;memset(lpszSubKeyName, 0, sizeof(TCHAR)*(dwSubKeyNameMaxLen + 1));DWORD dwNameCnt = dwSubKeyNameMaxLen + 1;int ret = RegEnumKeyEx(hKEY,index,lpszSubKeyName,&dwNameCnt,NULL,NULL,NULL,NULL);if (ret == ERROR_NO_MORE_ITEMS){_tprintf(_T("Error:RegEnumKeyEx ERROR_NO_MORE_ITEMS\n"));//return 1;}if (ret == ERROR_MORE_DATA){_tprintf(_T("Error:RegEnumKeyEx ERROR_MORE_DATA\n"));//return 1;}if (ret != ERROR_SUCCESS) // Error    {PrintError(_T("RegEnumKeyEx:"));_tprintf(_T("Error:RegEnumKeyEx unable to query information about the registry\n"));   return 1; }printf("lpszSubKeyName:%s\n", lpszSubKeyName); /*for (unsigned int index = 0; index < dwKeyValueCnt; ++index){LPTSTR lpszKeyValueName = new TCHAR[dwKeyValueNameMaxLen + 1];memset(lpszKeyValueName, 0, sizeof(TCHAR)*(dwKeyValueNameMaxLen + 1));DWORD  dwNameCnt = dwKeyValueNameMaxLen + 1;DWORD  dwKeyValueType;LPBYTE lpbKeyValueData = new BYTE[dwKeyValueDataMaxLen];DWORD  dwKeyValueDataLen;int ret = RegEnumValue(hKEY,index,lpszKeyValueName,&dwNameCnt,NULL,&dwKeyValueType,lpbKeyValueData,&dwKeyValueDataLen);if (ret != ERROR_SUCCESS){PrintError(_T("RegEnumValue:"));_tprintf(_T("Error:RegEnumValue  unable to query information about the registry\n"));return 1;}printf("lpszKeyValueName:%s\n", lpszKeyValueName); delete[] lpszKeyValueName; delete[] lpbKeyValueData;} */if (RegOpenKeyEx(hKEYtmp, lpszSubKeyName, 0, KEY_READ, &hKEYtmp) != ERROR_SUCCESS){PrintError(_T("RegOpenKeyEx:"));_tprintf(_T("Error: RegOpenKeyEx unable to query information about the registry\n"));return 1;}TCHAR DriverVersion[256];TCHAR DriverDate[256];DWORD dwSzType = REG_SZ;DWORD dwSizeDriverVersion = sizeof(DriverVersion);DWORD dwSizeDriverDate = sizeof(DriverDate);if (RegQueryValueEx(hKEYtmp, _T("DriverVersion"), 0, &dwSzType, (LPBYTE)&DriverVersion, &dwSizeDriverVersion) != ERROR_SUCCESS){//PrintError(_T("RegQueryValueEx:")); _tprintf(_T("Error:RegQueryValueEx  unable to query information about the registry\n"));// return 1;} CString USBVersionWin7 = { _T("10.0.14393.1358") }; if (USBVersionWin7 == DriverVersion) { _tprintf(_T("DriverVersion:%s\n"), DriverVersion); BOOL USBtmp = TRUE; if (USBtmp) USBDriverWin7 = TRUE; }RegCloseKey(hKEYtmp);}//for  delete[] lpszSubKeyName; //程序结束,关闭打开的hKEYRegCloseKey(hKEY);//system(_T("pause"));return 0;} 



原创粉丝点击