需要使用 C++ 程序查找 java 版本号

Need to find java version number using c++ program

我是 C++ 编程新手。那么,我应该使用哪些库或函数从注册表中检索此信息?请简要介绍一下从注册表中检索 java 版本号所涉及的步骤。我正在使用 VC++。

如果 java 路径设置正确,你可以只 运行 java -version 来自代码: 使用此处描述的代码 How to execute a command and get output of command within C++ using POSIX? :

#include <string>
#include <iostream>
#include <stdio.h>

std::string exec(char* cmd) {
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    _pclose(pipe);
    return result;
}

使用类似:

int main(void) {
    std::cout << exec("java -version");
    return 0;
}