0

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.

2
  • 2
    std::string and LPCSTR are two completely different types. The compiler's error message couldn't be more clear. You should spend some time studying the difference between std::string, a C++ class, and C-style strings, and how to convert between the two. Commented Jul 23, 2016 at 2:29
  • also, exepath needs to be called as exepath.c_str() Commented Jul 23, 2016 at 2:49

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.