TInstalledSoftwareItem可以自己扩展内容,只要解析对应的注册表键值就行了
struct TInstalledSoftwareItem
{
QString displayName, installPath;
};
class TInstalledSoftwareList: public QVector<TInstalledSoftwareItem>
{
public:
bool contains(const QString& name)
{
for (auto& item: *this)
{
if (item.displayName == name)
{
return true;
}
}
return false;
}
TInstalledSoftwareItem& getItem(const QString& name)
{
for (auto& item : *this)
{
if (item.displayName == name)
{
return item;
}
}
}
};
TInstalledSoftwareList CRegistryHelper::getInstalledSoftwares()
{
TInstalledSoftwareList list;
auto readReg = [&](const QString& key)
{
QSettings reg(key, QSettings::NativeFormat);
QStringList groupsList = reg.childGroups();
foreach(QString group, groupsList)
{
reg.beginGroup(group);
QString name = reg.value("DisplayName").toString();
if (!name.trimmed().isEmpty() && !list.contains(name))
{
TInstalledSoftwareItem item;
item.displayName = name;
item.installPath = reg.value("InstallLocation").toString();
list.push_back(item);
}
reg.endGroup();
}
};
readReg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
readReg("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
readReg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
readReg("HKEY_CURRENT_USER\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
return std::move(list);
}
TInstalledSoftwareList CRegistryHelper::getInstalledBrowsers()
{
TInstalledSoftwareList list;
auto readReg = [&](const QString& key)
{
QString baseKey = key;
QSettings reg(baseKey, QSettings::NativeFormat);
QStringList groupsList = reg.childGroups(); //文件夹列表,childkeylist代表所有子项目列表
foreach(QString group, groupsList)
{
reg.beginGroup(group);
QSettings regName(baseKey + "\\" + group, QSettings::NativeFormat);
QString name = regName.value(".").toString(); //读取key:(默认)
if (!name.trimmed().isEmpty() && !list.contains(name))
{
QSettings regPath(baseKey + "\\" + group + "\\shell\\open\\command", QSettings::NativeFormat);
QString path = regPath.value(".").toString(); //读取key:(默认)
if (!path.isEmpty()) {
TInstalledSoftwareItem item;
item.displayName = name;
item.installPath = path;
list.push_back(item);
}
}
reg.endGroup();
}
};
readReg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\StartMenuInternet");
readReg("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Clients\\StartMenuInternet");
readReg("HKEY_CURRENT_USER\\SOFTWARE\\Clients\\StartMenuInternet");
return std::move(list);
}