Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of type "char *" is incompatible with parameter of type "LPWSTR"

Tags:

c++

This has probably been asked before but I can't seem to find the solution:

std::string GetPath()
{
    char buffer[MAX_PATH];
    ::GetSystemDirectory(buffer,MAX_PATH);
    strcat(buffer,"\\version.dll");

    return std::string(buffer);
}

This returns an error stating:

argument of type "char *" is incompatible with parameter of type "LPWSTR"

So yeah. Anyone got an answer?

like image 327
OliverMller Avatar asked Dec 16 '13 13:12

OliverMller


1 Answers

You need to use the ansi version:

std::string GetPath()
{
     char buffer[MAX_PATH] = {};
     ::GetSystemDirectoryA(buffer,_countof(buffer)); // notice the A
     strcat(buffer,"\\version.dll");

     return std::string(buffer);
 }

Or use unicode:

std::wstring GetPath()
{
     wchar_t buffer[MAX_PATH] = {};
     ::GetSystemDirectoryW(buffer,_countof(buffer)); // notice the W, or drop the W to get it "by default"
     wcscat(buffer,L"\\version.dll");

     return std::wstring(buffer);
 }

Rather than call the A/W versions explicitly you can drop the A/W and configure the whole project to use ansi/unicode instead. All this will do is change some #defines to replace foo with fooA/W.

Notice that you should use _countof() to avoid incorrect sizes depending on the buffers type too.

like image 101
paulm Avatar answered Oct 17 '22 00:10

paulm