C++拜訪注冊表獲得已裝置軟件信息列表現例代碼。本站提示廣大學習愛好者:(C++拜訪注冊表獲得已裝置軟件信息列表現例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C++拜訪注冊表獲得已裝置軟件信息列表現例代碼正文
// ---------------------------------------------------------------
// FlieNmae:
// SofInfo.h
// Remark:
// 經由過程讀取注冊表取得本機已裝置軟件信息。
// ---------------------------------------------------------------
#pragma once
#include <vector>
struct SoftInfo
{
// 軟件名
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
#include "stdafx.h"
#include "SoftInfo.h"
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("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
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);
}
}