Visual C++:error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'

user1276261 picture user1276261 · Mar 17, 2012 · Viewed 7.2k times · Source

when I tried to compiling my project I got some errors that I can't solve.. anyway this is the one of the codes:

public:
void Init(HMODULE hModule, string Filename)
{
    char szLoc[ MAX_PATH ];
    GetModuleFileName(hModule, szLoc, sizeof( szLoc ) );
    char* dwLetterAddress = strrchr( szLoc, '\\' );
    *( dwLetterAddress + 1 ) = 0;
    strcat( szLoc, Filename.c_str() );
    __OutStream.open( szLoc, ios::app);
}

And the error is:

error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style  cast or function-style cast

Thanks for help.. Regards, Messer

Answer

Marlon picture Marlon · Mar 17, 2012

A lot of the "functions" of the Windows API are actually macroes to either the ANSI (A) or Unicode (W for wide) version of the function. Depending on your project settings, these macroes will be either DoSomeFunctionA or DoSomeFunctionW when you want to call DoSomeFunction. The portable way would be then to use TCHAR because it is defined as char for ANSI and wchar_t for Unicode.

If you don't want to compile with Unicode, you can change your project settings to Project Properties -> Configuration Properties -> General -> Character Set -> Use Multibyte Character Set.

If you do want to compile with Unicode, then you should append an A (ex: GetModuleFileNameA) to the necessary function names.