0

You can try this right now.

Define both WinMain and wWinMain abd compile it as a static library.

Make a new project for executable file exe.

Set character set setting UNICODE system.(define _UNICODE)

Link the static library just made.

Then your program starts from WinMain.

Whether the character set is multi-byte or unicode, WinMain is called, when both WinMain and wWinMain is defined.

It happens only when you define WinMain in a static library.

When you define WinMain and wWinMain in a source project including, it works correctly,

unicode system calls wWinMain and multi-byte system calls WinMain.

Why does it happen?

skaffman
  • 398,947
  • 96
  • 818
  • 769
ssss
  • 41
  • 1
  • 4

1 Answers1

0

Linker tries to deduce the what your entry point is and substitute correspoinding implementation of __tmainCRTStartup. However there are two limitations here:

  • Linker was not designed to make a guess between two or more entry points. In fact linker produces warning LNK4067: ambiguous entry point when it finds more than one choice.
  • Linker does not know about preprocessor, so whatever preprocessor symbols you defined are irrelevant at the time linker runs.

So, to answer your question why you see this, is you are hitting one of the cases of "undefined behavior". You should not rely on this behavior because it is not guaranteed to work and might change in new compiler version.

I suggest you define one single entry point using _tWinMain:

int APIENTRY _tWinMain(HINSTANCE hInstance,   
                 HINSTANCE hPrevInstance,   
                 LPTSTR    lpCmdLine,   
                 int       nCmdShow)   

There is also /ENTRY option for linker, but I don't recommend you using it, because it is more dangerous and you can accomplish the same functionality by simply defining single entry function and passing correct value for the /SUBSYSTEM linker option.

seva titov
  • 11,720
  • 2
  • 35
  • 54