5

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

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
user1276261
  • 51
  • 1
  • 2
  • Your code doesn't call `GetModuleFileNameW`. Have you left out something relevant? – Carl Norum Mar 17 '12 at 22:02
  • 2
    Your code is compiling as unicode, but it's not. Either change the compilation options, change this function to use wide characters, or specifically call the ASCII version of the function -- `GetModuleFileNameA`. – David Schwartz Mar 17 '12 at 22:05
  • I'm not entirely sure, but your character set in Visual Studio might be set to "Use Unicode Character Set" under "Configuration Properties" > "General" of your project's properties. – Bart Mar 17 '12 at 22:07
  • @Carl Norum The windows header files will define GetModuleFileName to be either GetModuleFileNameA or GetModuleFileNameW depending on whether unicode is set of not – jcoder Mar 17 '12 at 22:08
  • @Bart I think just the reverse, it's using a unicode build now and needs to be a multibyte character build for this to compile. – jcoder Mar 17 '12 at 22:08
  • @JohnB That's what I'm suggesting. I'm not saying it should be set to that. – Bart Mar 17 '12 at 22:24

1 Answers1

6

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.

Marlon
  • 19,924
  • 12
  • 70
  • 101