C++訪問註冊表獲取已安裝軟件信息列表示例代碼工具
最後,若是你們若是在自學遇到困難,想找一個C++的學習環境,能夠加入咱們的C++學習圈,點擊我加入吧,會節約不少時間,減小不少在學習中遇到的難題。學習
// ---------------------------------------------------------------開發工具
// FlieNmae:spa
// SofInfo.horm
// Remark:ip
// 經過讀取註冊表得到本機已安裝軟件信息。開發
// ---------------------------------------------------------------get
struct SoftInfoit
{io
// 軟件名
CString m_strSoftName;
// 軟件版本號
CString m_strSoftVersion;
// 軟件安裝目錄
CString m_strInstallLocation;
// 軟件發佈廠商
CString m_strPublisher;
// 主程序所在完整路徑
CString m_strMainProPath;
// 卸載exe所在完整路徑
CString m_strUninstallPth;
};
class CSoftInfo
{
private:
// 保存已安裝經常使用軟件安裝信息
std::vector<SoftInfo> m_SoftInfoArr;
// 保存系統補丁信息
std::vector<SoftInfo> m_SystemPatchesArr;
public:
CSoftInfo();
~CSoftInfo(){}
// 獲取一個包含經常使用軟件安裝信息的Vector
std::vector<SoftInfo> GetSoftInfo (void) const;
// 獲取全部已安裝經常使用軟件名
void GetSoftName (std::vector<LPCTSTR>& lpszSoftName);
// 獲取全部已安裝經常使用軟件版本號
void GetSoftVersion (std::vector<LPCTSTR>& lpszSoftVersion);
// 獲取全部已安裝經常使用軟件安裝目錄
void GetInstallLocation (std::vector<LPCTSTR>& lpszInstallLocation);
// 獲取全部已安裝經常使用軟件發佈廠商
void GetPublisher (std::vector<LPCTSTR>& lpszPublisher);
// 獲取全部已安裝經常使用軟件主程序所在路徑
void GetMainProPath (std::vector<LPCTSTR>& lpszMainProPath);
// 獲取全部已安裝經常使用軟件卸載程序所在路徑
void GetUninstallPth (std::vector<LPCTSTR>& lpszSoftName);
// 獲取一個包含系統補丁信息的Vector
std::vector<SoftInfo> GetSystemPatchesInfo (void) const;
// 獲取全部已安裝系統補丁名
void GetSystemPatchesName (std::vector<LPCTSTR>& lpszSoftName);
};
或者
// FlieNmae: Softinfo.cpp
CSoftInfo::CSoftInfo()
{
struct SoftInfo softinfo;
HKEY RootKey; // 主鍵
LPCTSTR lpSubKey; // 子鍵名稱
HKEY hkResult; // 將要打開鍵的句柄
HKEY hkRKey;
LONG lReturn; // 記錄讀取註冊表是否成功
CString strBuffer;
CString strMidReg;
DWORD index = 0;
TCHAR szKeyName[255] = {0}; // 註冊表項名稱
TCHAR szBuffer[255] = {0};
DWORD dwKeyLen = 255;
DWORD dwNameLen = 255;
DWORD dwType = REG_BINARY|REG_DWORD|REG_EXPAND_SZ|REG_MULTI_SZ|REG_NONE|REG_SZ;
RootKey = HKEY_LOCAL_MACHINE;
lpSubKey = _T("SOFTWAREMicrosoftWindowsCurrentVersionUninstall");
lReturn = RegOpenKeyEx(RootKey, lpSubKey, 0, KEY_ALL_ACCESS, &hkResult);
if (lReturn == ERROR_SUCCESS)
{
while (ERROR_NO_MORE_ITEMS !=RegEnumKeyEx(hkResult, index, szKeyName, &dwKeyLen, 0, NULL, NULL, NULL))
{
index++;
strBuffer.Format(_T("%s"), szKeyName);
if (!strBuffer.IsEmpty())
{
strMidReg = (CString)lpSubKey +_T("") + strBuffer;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, strMidReg, 0, KEY_ALL_ACCESS, &hkRKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hkRKey, _T("DisplayName"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strSoftName = szBuffer;
dwNameLen = 255;
memset(szBuffer, 0, 255);
RegQueryValueEx(hkRKey, _T("DisplayVersion"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strSoftVersion = szBuffer;
dwNameLen = 255;
memset(szBuffer, 0, 255);
RegQueryValueEx(hkRKey, _T("InstallLocation"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strInstallLocation = szBuffer;
dwNameLen = 255;
memset(szBuffer, 0, 255);
RegQueryValueEx(hkRKey, _T("Publisher"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strPublisher = szBuffer;
dwNameLen = 255;
RegQueryValueEx(hkRKey, _T("InstallLocation"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strMainProPath = szBuffer;
dwNameLen = 255;
memset(szBuffer, 0, 255);
RegQueryValueEx(hkRKey, _T("UninstallString"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen);
softinfo.m_strUninstallPth = szBuffer;
dwNameLen = 255;
memset(szBuffer, 0, 255);
if(!softinfo.m_strSoftName.IsEmpty())
{
if(strBuffer.GetAt(0) == 'K' && strBuffer.GetAt(1) == 'B')
{
m_SystemPatchesArr.push_back(softinfo);
}
else
{
m_SoftInfoArr.push_back(softinfo);
}
}
}
dwKeyLen = 255;
memset(szKeyName,0, 255);
}
}
RegCloseKey(hkResult);
}
else
{
::MessageBox(NULL, _T("打開註冊表失敗!"), NULL, MB_ICONWARNING);
}
}
std::vector<SoftInfo> CSoftInfo::GetSoftInfo (void) const
{
return m_SoftInfoArr;
}
void CSoftInfo::GetSoftName (std::vector<LPCTSTR>& lpszSoftName)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
lpszSoftName.push_back(iter->m_strSoftName);
}
}
void CSoftInfo::GetSoftVersion (std::vector<LPCTSTR>& lpszSoftVersion)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
if (!(iter->m_strSoftVersion).IsEmpty())
{
lpszSoftVersion.push_back(iter->m_strSoftVersion);
}
}
}
void CSoftInfo::GetInstallLocation (std::vector<LPCTSTR>& lpszInstallLocation)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
if (!(iter->m_strInstallLocation).IsEmpty())
{
lpszInstallLocation.push_back(iter->m_strInstallLocation);
}
}
}
void CSoftInfo::GetPublisher (std::vector<LPCTSTR>& lpszPublisher)
{
std::vector<SoftInfo>::iterator iter;
bool bSign;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
bSign = true;
// 初步去重複廠商
std::vector<LPCTSTR>::iterator itr;
for (itr = lpszPublisher.begin(); itr != lpszPublisher.end(); itr++)
{
if (iter->m_strPublisher == (CString)*itr)
{
bSign = false;
}
}
if (bSign)
{
lpszPublisher.push_back(iter->m_strPublisher);
}
}
}
void CSoftInfo::GetMainProPath (std::vector<LPCTSTR>& lpszMainProPath)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
if (!(iter->m_strMainProPath).IsEmpty())
{
lpszMainProPath.push_back(iter->m_strMainProPath);
}
}
}
void CSoftInfo::GetUninstallPth (std::vector<LPCTSTR>& lpszSoftName)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
{
if (!(iter->m_strUninstallPth).IsEmpty())
{
lpszSoftName.push_back(iter->m_strUninstallPth);
}
}
}
std::vector<SoftInfo> CSoftInfo::GetSystemPatchesInfo (void) const
{
return m_SystemPatchesArr;
}
void CSoftInfo::GetSystemPatchesName (std::vector<LPCTSTR>& lpszSoftName)
{
std::vector<SoftInfo>::iterator iter;
for (iter = m_SystemPatchesArr.begin(); iter != m_SystemPatchesArr.end(); iter++)
{
lpszSoftName.push_back(iter->m_strSoftName);
}
}
好啦,今天就分享到這啦!喜歡此篇文章或以爲這篇文章對你有幫助的讀者能夠分享給身邊的朋友們。若是你是小白也能夠私信回覆「資料」領取大禮包一份,以及開發工具一份。