如何在C ++中获取当前正在运行的可执行文件的文件名 [英] How to Get the Filename of the Currently Running Executable in C++

查看:776
本文介绍了如何在C ++中获取当前正在运行的可执行文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前进程的完整路径。

I want to get the full path of the current process.

我使用 _getcwd 来获取当前进程的完整路径。工作目录。

I use _getcwd to get the current working directory. But it not includes file name.

如何获取文件名,例如: filename.exe

How can I get file name like: filename.exe?

推荐答案

在Windows上,您可以使用:

On windows you can use:

TCHAR szExeFileName[MAX_PATH]; 
GetModuleFileName(NULL, szExeFileName, MAX_PATH);

szExeFileName将包含完整路径+可执行文件名

szExeFileName will contain full path + executable name

[edit]

对于更便携的解决方案,请使用 argv [0] 或其他特定于平台的代码。您可以在这里找到这样的方法: https:// github。 com / mirror / boost / blob / master / libs / log / src / process_name.cpp

For more portable solution use argv[0] or some other platform specific code. You can find such aproach here: https://github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp.

这篇关于如何在C ++中获取当前正在运行的可执行文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆