程序如何获取自身的可执行名称
How can program get executable name of itself?
可能的重复项:
提取当前可执行文件名称
我创建了一个从 ini 文件读取配置的程序,该文件的名称应该与可执行文件的名称相同,但当然还有它的扩展名。因此,如果我将其命名myprogram.exe
则配置应该是myprogram.ini
的,如果我在编译后更改exe的名称,它应该看起来与它的新名称一致。
我知道可以从argv[0]
获取程序名称,但这只有在从命令行启动时才有效,当它在资源管理器中单击时,此数组为空。
当我阅读这里的答案时,我认为它必须与这个函数有关: https://stackoverflow.com/a/10572632/393087 - 但我找不到任何使用该函数的好例子,我对 c++ 非常初学者,一般函数定义(如 Microsoft 页面上介绍的)对我来说太难理解了,但是当我得到一个工作示例时,对我来说很容易理解。
#include <windows.h>
#include <Shlwapi.h>
// remember to link against shlwapi.lib
// in VC++ this can be done with
#pragma comment(lib, "Shlwapi.lib")
// ...
TCHAR buffer[MAX_PATH]={0};
TCHAR * out;
DWORD bufSize=sizeof(buffer)/sizeof(*buffer);
// Get the fully-qualified path of the executable
if(GetModuleFileName(NULL, buffer, bufSize)==bufSize)
{
// the buffer is too small, handle the error somehow
}
// now buffer = "c:whateveryourexecutable.exe"
// Go to the beginning of the file name
out = PathFindFileName(buffer);
// now out = "yourexecutable.exe"
// Set the dot before the extension to 0 (terminate the string there)
*(PathFindExtension(out)) = 0;
// now out = "yourexecutable"
现在你有一个指向可执行文件的"基名称"的指针;请记住,它指向buffer
内部,所以当buffer
超出范围时out
不再有效。
GetModuleFileName(NULL, .....)
但是我找不到使用该函数的任何好例子
你还不够努力。msdn 上的"获取模块文件名"文章中的"示例"部分
相关文章:
- 无法获取进程 ID 4 (ntoskrnl.exe) 的可执行路径
- 获取自己的可执行文件的当前路径 (C++)
- 获取生成文件以更改输出可执行文件名称
- 在C 中使用AssocqueryString获取可执行路径
- 获取 Qt/C++ 中 QProcess 的可执行文件名称
- 如何在C++中获取可执行文件名
- 通过我的可执行文件的资源 ID 获取图标索引,C++
- 获取<whatever>包含正在运行的可执行文件的 USB 驱动器C++ /dev/
- 从 MFC(c++) 应用程序启动的可执行 jar 中获取返回值
- 如何在C++中获取当前运行的可执行文件的文件名
- 从已编译的可执行文件中获取编译器选项
- 任务管理器如何获取进程的命令行可执行路径
- 程序如何获取自身的可执行名称
- 从服务启动的可执行文件中获取服务名称
- C++ WinAPI - 如何从可执行文件的名称中获取其安装路径
- 获取系统本机形式的可执行路径
- 获取cmake可执行路径以在自定义命令上运行它
- 通过路径获取 Windows 可执行文件的显示名称
- 使用qmake安装文件:如何获取可执行文件
- Qt:从应用程序/可执行目录获取设置ini文件的问题