jjjjjdude
jjjjjdude

Reputation: 109

How to get application file path as string C++

I have a function called "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.

Upvotes: 0

Views: 620

Answers (1)

jblixr
jblixr

Reputation: 1395

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.

So the first parameter should be as exepath.c_str().

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

Upvotes: 1

Related Questions