C++ 获取当前运行程序的路径

GetModuleFileName获取当前进程已加载模块的文件的完整路径,该模块必须由当前进程加载。
其头文件为windows.h

#include <windows.>

std::string GetAppPath()
{
    
    
	char buffer[512] = {
    
    0};
	GetModuleFileName(nullptr, buffer, sizeof(512));
	std::string appPath = buffer;
	appPath = appPath.substr(0, appPath, rfind("\\"));
	return appPath;
}

注意,其返回的路径值使用的分隔符为\\,例如D:\\Test\\Result\\Test.exe。

猜你喜欢

转载自blog.csdn.net/xp178171640/article/details/113716358