【收藏】获取系统安装软件(vc++源码)
获取系统安装软件(vc++源码)
http://download.csdn.net/detail/yy_kici/2193865
typedef struct SetupSoftInfo { mystring displayName; //软件名称 mystring publisher; //发行商 mystring displayVersion; //版本 mystring installDate; //安装日期 mystring uninstallString; //卸载命令及参数 }*PSetupSoftInfo;
BOOL CNEPAction::GetSetupSofts(vector<SetupSoftInfo>&stupInfo) { const int MAX_LEG = 256 * sizeof(TCHAR); HKEY hKey; DWORD dwRegNum = MAX_LEG; TCHAR regBufferName[MAX_LEG] = {0}; if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS ) { if( RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwRegNum, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ){ for( int i = 0; i < (int)dwRegNum; i++ ) { DWORD dwRegSize = MAX_LEG; RegEnumKeyEx(hKey, i, regBufferName, &dwRegSize, NULL, NULL, NULL, NULL); DWORD dwType; HKEY hSubKey; if( RegOpenKeyEx(hKey, regBufferName, NULL, KEY_READ, &hSubKey) == ERROR_SUCCESS) { SetupSoftInfo info; TCHAR regBufferValue[MAX_LEG] = {0}; dwRegSize = MAX_LEG; RegQueryValueEx(hSubKey, _T("DisplayName"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.displayName = regBufferValue; //软件名称 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("Publisher"), 0, &dwType,(LPBYTE)regBufferValue, &dwRegSize); info.publisher = regBufferValue; //发行商 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("DisplayVersion"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.displayVersion = regBufferValue; //版本 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); // 判断是否能在注册表中获取到安装时间, 否取子项创建时间 if(RegQueryValueEx(hSubKey, _T("InstallDate"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize) == ERROR_SUCCESS ) { info.installDate = regBufferValue; } else { FILETIME fileLastTime; RegQueryInfoKey(hSubKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &fileLastTime); SYSTEMTIME sTime, stLocal ; FileTimeToSystemTime(&fileLastTime,&sTime); SystemTimeToTzSpecificLocalTime(NULL, &sTime, &stLocal); TCHAR tchTime[MAX_LEG] = {0}; _stprintf(tchTime,_T("%d%02d%02d"),stLocal.wYear,stLocal.wMonth,stLocal.wDay); info.installDate = tchTime; } dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("UninstallString"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.uninstallString = regBufferValue;//卸载命令及参数 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); // 取ParentKeyName键值,判断是否是补丁信息, 是补丁信息键值为"OperatingSystem" RegQueryValueEx(hSubKey, _T("ParentKeyName"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); TCHAR tchLocal[MAX_LEG] = {0}; _tcscpy(tchLocal,regBufferValue); _tcscpy(regBufferValue,_T("")); dwRegSize = MAX_LEG; RegQueryValueEx(hSubKey, _T("QuietUninstallString"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); if( _tcscmp(regBufferValue,_T("") ) ) { info.displayName = regBufferName; info.uninstallString = regBufferValue; } if( (info.displayName != _T("") ) &&( _tcscmp( tchLocal,_T("OperatingSystem") ) )) //判断是否是补丁信息 { stupInfo.push_back(info); } } }//end for(; ;) } }else { return FALSE; //打开键失败 } RegCloseKey(hKey); return TRUE; }