简体   繁体   English

如何将应用程序文件路径获取为字符串C ++

[英]How to get application file path as string C++

I have a function called "ExePath" 我有一个名为“ ExePath”的函数

string exepath()
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
return std::string(buffer);
}

This returns the path of the application. 这将返回应用程序的路径。 Later, I try to copy the application to another place. 稍后,我尝试将应用程序复制到另一个地方。

CopyFile(exepath, "C:\\Example\\Example.exe", FALSE);

When compiling I get the following error: 编译时出现以下错误:

[Error] cannot convert 'std::string' to 'LPCSTR' for argument '1' to 'WINBOOL CopyFileA(LPCSTR, LPCSTR, WINBOOL)' 

I take this as it cant use the string as a string. 我认为这不能使用字符串作为字符串。 What? 什么? Basically I am trying to find the path that the application has been executed and copy it to another place. 基本上,我试图找到应用程序已执行的路径并将其复制到另一个位置。 Any and all help is appreciated. 任何和所有帮助表示赞赏。

LPCSTR is a Long Pointer to a Const STRing( const char * ), string::c_str function will return the corresponding const char * to your string class. LPCSTR是指向const STRing( const char * )的长指针, string::c_str函数会将对应的const char *返回给您的string类。

So the first parameter should be as exepath.c_str() . 因此,第一个参数应为exepath.c_str()

CopyFile(exepath.c_str(), "C:\\Example\\Example.exe", FALSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM