首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何以编程方式确定是否安装了Visual C++ Runtime ?

要以编程方式确定是否安装了 Visual C++ Runtime,您可以尝试以下方法:

  1. 使用 Windows API 函数 GetModuleHandle() 检查是否可以加载 msvcp140.dll 或者 vcruntime140.dll。这些 DLL 文件是 Visual Studio 2015 及更高版本的 Visual C++ Runtime 的一部分。
代码语言:cpp
复制
#include<Windows.h>

bool isVisualCRuntimeInstalled() {
    HMODULE msvcp140 = GetModuleHandle("msvcp140.dll");
    HMODULE vcruntime140 = GetModuleHandle("vcruntime140.dll");
    return (msvcp140 != NULL) && (vcruntime140 != NULL);
}
  1. 使用 Windows 注册表检查已安装的 Visual Studio 版本。
代码语言:cpp
复制
#include<Windows.h>
#include<vector>
#include<string>

bool isVisualCRuntimeInstalled() {
    HKEY hKey;
    LONG lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio", 0, KEY_READ, &hKey);
    if (lResult == ERROR_SUCCESS) {
        char subKeyName[256];
        DWORD subKeyNameSize;
        FILETIME lastWriteTime;
        DWORD numSubKeys = 0;
        lResult = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, &lastWriteTime);
        if (lResult == ERROR_SUCCESS) {
            for (DWORD i = 0; i < numSubKeys; i++) {
                subKeyNameSize = 256;
                lResult = RegEnumKeyEx(hKey, i, subKeyName, &subKeyNameSize, NULL, NULL, NULL, &lastWriteTime);
                if (lResult == ERROR_SUCCESS) {
                    std::string subKeyStr(subKeyName);
                    if (subKeyStr.find("14.") != std::string::npos) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

这两种方法都可以检测是否安装了 Visual C++ Runtime,但它们可能不会涵盖所有情况。如果您需要更准确的检测方法,请考虑使用其他方法,例如检查注册表中的特定安装路径或查询已安装的应用程序列表。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券