Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force the visual studio to use the wmain instead of main

I'm in need of parsing unicode parameters, so I wanted to use the wmain instead.

So instead of

int main(int argc, char** argv)

I would like to use

int wmain(int argc, wchar_t** argv)

The problem is that the visual studio is not recognizing the wmain, and it is trying to use main instead:

error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup

This is what I tried:

  • Changing the Properties->General->Character set
  • Changing the Entry point (In this case I got lot of compatibility errors with libraries that don't even have entry point, so it can't be specified there).

    warning LNK4258: directive '/ENTRY:mainCRTStartup' not compatible with switch '/ENTRY:mainWCRTStartup'; ignored
    
  • Tried the _tmain instead, just to find out it is just a macro that changes it to main.

  • Using the #pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")
  • Using the UNICODE macro

Nothing helps.

Edit: I would like to mention, that I'm using the vs120_xp (Win xp compatibile) toolset, but when I tried to use the default one, it still didn't work.

Edit2: I tried to make brand new project, and the wmain worked there out of the box. I didn't have to change anything, so it have to be some specific setting in the current project that is causing it.

like image 651
kovarex Avatar asked Feb 05 '15 11:02

kovarex


1 Answers

  Using the #pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")

You are getting close, not quite close enough. The CRT has four entrypoints:

  • mainCRTStartup => calls main(), the entrypoint for console mode apps
  • wmainCRTStartup => calls wmain(), as above but the Unicode version
  • WinMainCRTStartup => calls WinMain(), the entrypoint for native Windows apps
  • wWinMainCRTStartup => calls wWinMain(), as above but the Unicode version

So it is /ENTRY:wmainCRTStartup

Do beware that the command line arguments are converted to Unicode assuming the default console code page. Which is a bit unpredictable, it is the legacy 437 OEM code page only in Western Europe and the Americas. The user might need to use the CHCP command (CHange Code Page) and tinker with the console window font to keep you happy. YMMV.

like image 53
Hans Passant Avatar answered Nov 09 '22 13:11

Hans Passant