Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 771
user1276261 Avatar asked Mar 17 '12 21:03

user1276261


1 Answers

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.

like image 164
Marlon Avatar answered Sep 19 '22 06:09

Marlon