Argument of type “char *” is incompatible with parameter of type “LPWSTR”

前端 未结 2 1058
抹茶落季
抹茶落季 2020-12-01 20:00

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

std::string GetPath()
{
    char buffer[MAX_PATH];
    ::GetSystemDirectory(buffe         


        
相关标签:
2条回答
  • 2020-12-01 20:06

    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.

    0 讨论(0)
  • 2020-12-01 20:30

    If you compile your code using MultiByte support it will compile correctly,but when you compile it using Unicode flag it will give an error because in Unicode support ::GetSystemDirectoryA becomes ::GetSystemDirectoryW use consider using TCHAR instead of char.TCHAR is defined such that it becomes char in Multibyte flag and wchar_t with Unicode flag

    TCHAR buffer[MAX_PATH];
    ::GetSystemDirectory(buffer,MAX_PATH);
    _tcscat(buffer,_T("\\version.dll"));
    

    You can use typedef for string /wstring so your code becomes independent

    #ifdef UNICODE 
    typedef wstring STRING;
    #else
    typedef string STRING;
    #endif
    
    STRING GetPath()
    {
        TCHAR buffer[MAX_PATH];
        ::GetSystemDirectory(buffer,MAX_PATH);
        _tcscat(buffer,_T("\\version.dll"));
    
        return STRING(buffer);
    }
    
    0 讨论(0)
提交回复
热议问题