c++ - 查找可以从 CMD 运行的可执行文件的绝对路径

标签 c++ windows visual-studio

我需要在我的 C++ 应用程序中找到 javaw 的绝对路径。
javaw 可以从命令提示符运行,我可以使用 where javaw 获取它的路径,但我需要 C++ 中的路径
如何在我的 C++ 应用程序中找到 javaw 的路径?

谢谢

最佳答案

此代码实际上是从对 How to execute a command and get output of command within C++? 的最佳答案复制粘贴而来的然后添加来自 main 的调用:

#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()
{
    std::cout << exec("where javaw") << std::endl;
    return 0;
}

这是它在我的 Windows 7 机器上打印的内容:

C:\Windows\System32\javaw.exe
C:\Program Files (x86)\Java\jdk1.7.0_55\bin\javaw.exe

我猜你必须以某种方式处理歧义,但我想我明白你为什么要这样做。

关于c++ - 查找可以从 CMD 运行的可执行文件的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262285/

相关文章:

c++ - 使用 smtp 发送电子邮件

c++ - 我的代码在解压缩时产生垃圾

c++ - 如何从 CreateWindowEx() 窗口获取宽度和高度? C++

visual-studio - 如何在 Visual Studio 中搜索并让它忽略注释掉的内容?

c# - StackOverflowException 的原因是什么,如何解决?

c++ - 如何使用 C 或 C++ 获取目录中的文件列表?

windows - 如何在 Windows 上将 cscope 与 vim 集成?

javascript - Components.interfaces.nsIProcess chop 包含 "&"的 URL 参数

wpf - 如何在 Visual Studio 中快速切换 XAML 和设计模式

c++ - 看似空的 vector