gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:34:58 25 4
gpt4 key购买 nike

我有一个名为“ExePath”的函数

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

这将返回应用程序的路径。后来,我尝试将应用程序复制到另一个地方。

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

编译时出现以下错误:

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

我认为这是因为它不能将字符串用作字符串。什么?基本上我试图找到应用程序已执行的路径并将其复制到另一个地方。感谢您提供任何帮助。

最佳答案

LPCSTR 是一个指向 Const STRing(const char *) 的长指针,string::c_str 函数将返回相应的 const char * 到您的 string 类。

所以第一个参数应该是exepath.c_str()

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

关于c++ - 如何将应用程序文件路径获取为字符串 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38537555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com