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

when I tried to compile my project, I got some errors that I cannot solve. In any case, this is 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:

 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 the help. Regards, Messer

+4
source share
1 answer

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

If you do not want to compile in Unicode, you can change your project settings to Project Properties -> Configuration Properties -> General -> Character Set -> Use Multibyte Character Set .

If you want to compile using Unicode, you must add A (ex: GetModuleFileNameA ) to the desired function names.

+5
source

Source: https://habr.com/ru/post/1402027/


All Articles