【问题标题】:Getting Path of the executable in current folder C++获取当前文件夹 C++ 中可执行文件的路径
【发布时间】:2017-04-16 13:24:29
【问题描述】:

我正在尝试在该程序所在的同一文件夹中获取 exe 文件的路径。但我不知道该怎么做,我做了这样的事情,但它只获取当前程序路径,我不知道如何替换我的程序和我想要获取路径的程序之间的文件名。

所以你能帮我在这个程序所在的同一文件夹中获取一个 exe 的路径(我知道那个 exe 的名称)...

    char fullp[MAX_PATH];
    char selfp[MAX_PATH] = "..//myprogram.exe"; 
    char otherprogram[MAX_PATH] = "//test.exe";

    DWORD szPath;
    szPath = GetModuleFileName(NULL, selfp, sizeof(selfp));

【问题讨论】:

  • 如果这是特定于 Windows 的,请将 winapi 标签添加到您的问题中。
  • 双斜线有什么用?没有操作系统使用它。
  • 我正在使用 Windows,但我不知道为什么只有“//”对我有效...我测试了其他的并且确实有效
  • 请确认:您在同一个文件夹中有两个程序。您想获取第一个程序的位置并使用该位置生成第二个程序的路径。
  • 艾略特你不需要那些'//'。至少不在那里。

标签: c++ winapi path


【解决方案1】:

OP 是最重要的方式。下面是一个如何完成其​​余部分的示例。

为了简化解决方案,我将char 数组尽可能地抛在后面,并使用std::string。

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

int main()
{
    char selfp[MAX_PATH];
    std::string otherprogram = "Failed to get path";

    DWORD szPath;
    szPath = GetModuleFileName(NULL, selfp, MAX_PATH);
    if (szPath != 0) // successfully got path of current program
    {
        // helper string to make life much, much easier
        std::string helper = selfp;
        //find last backslash in current program path
        size_t pos = helper.find_last_of( "\\" ); 

        if (pos != std::string::npos) // found last backslash
        {
            // remove everything after last backslash. This should remove 
            // the current program's name.
            otherprogram = helper.substr( 0, pos+1);
            // append new program name
            otherprogram += "test.exe";
        }
    }
    std::cout << otherprogram << std::endl;
}

【讨论】:

  • 如果你要使用std::string,你不妨一路走下去,使用std::wstring、wchar_t和GetModuleFileNameW()的Windows API函数的Unicode版本,以获得最大效果。另请注意下面 Remy 的回答(但请注意最低操作系统要求)。
  • @and Remy 的回答没有最低操作系统要求。那些 shlwapi 函数自 Windows 95 以来就已经存在。MSDN 文档意味着最小的支持操作系统,就像微软将提供支持的最低操作系统一样。但是,是的,100% 同意使用 Unicode 和 std::wstring。
  • @CodyGray 我以为 Windows 8 引入了一堆新的路径功能?我忘记了哪个是哪个,所以雷米的答案可能不是新的,我不记得了=P
  • @CodyGray PathAlloc*()、PathCch*() 和 PathIsUNCEx() 函数。我知道新的有一些特定的东西。我想到的是 Alloc 和 Cch 功能。嗯,MAX_PATH 不再是这些功能的限制了吗?我想知道如果我想在 Windows 10 之前的平台上运行我可以使用什么,因为正是这个限制阻止了我使用这些功能:/
  • @and 您可以通过指定以 \\?\ 为前缀的原生 NT 样式路径来绕过 MAX_PATH 限制。这基本上是一种转义,它允许您跳过限制为 MAX_PATH 的 Win32 文件名处理逻辑,并将路径直接传递给 NTFS 子系统,该子系统最多允许 32k。这适用于所有版本的 NT。当然,这对旧的 shlwapi 助手帮助不大,但是你刚刚教给我的新功能可以缓解这个限制。 PathCch* 函数表示它们可以很好地使用 \\?\ 路径。
【解决方案2】:

Win32 API 有一大堆Path Handling functions 可用。

例如,一旦您从GetModuleFileName() 获得调用进程的完整路径,您可以使用PathRemoveFileSpec() 删除文件名,只保留文件夹路径:

char selfdir[MAX_PATH] = {0}; 
GetModuleFileNameA(NULL, selfdir, MAX_PATH);
PathRemoveFileSpecA(selfdir);

然后使用PathAppend() 或PathCombine() 将不同的文件名附加到该路径:

char otherprogram[MAX_PATH] = {0};
lstrcpyA(otherprogram, selfdir);
PathAppendA(otherprogram, "test.exe");

char otherprogram[MAX_PATH] = {0};
PathCombineA(otherprogram, selfdir, "test.exe");

【讨论】:

  • 现在是 2016 年,我非常想对这个使用 ANSI API 的正确答案投反对票。 :-\
  • @CodyGray OP 正在使用 Ansi 缓冲区。由于我们不知道缓冲区的用途,所以我没有更改它们。
猜你喜欢
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 2011-02-07
相关资源
最近更新 更多