3

So I'm trying to create a window in CodeBlocks using Win32, and so far only this version of WinMain works ( note: this is just a simple and naive example ):

#include <windows.h>

INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow ) {
    MessageBox( NULL, "Title", "Message", MB_OKCANCEL );
    return 0;
}

But this version does not:

#include <windows.h>

INT WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nCmdShow ) {
    MessageBox( NULL, "Title", "Message", MB_OKCANCEL );
    return 0;
}

As far as I know, the latter expects the 3rd argument to be a pointer to a string of wide characters, while the former does not. But when I compile in CodeBlocks, all I get is this message:

undefined reference to WinMain@16

Apparently CodeBlocks is expecting the version of WinMain that doesn't receive a LPWSTR value as argument. My question is, how do I configure CodeBlocks so that it compiles with wWinMain?

  • That's a linker error, not a compiler error. You should be linking to the library that contains the function in question. – PaulMcKenzie Jul 17 '16 at 06:07

1 Answers1

5

wWinMain is compiler specific. It is supported by Visual Studio. Code::Block is usually setup with MinGW, it will compile wWinMain but it gives link error because it doesn't recognize wWinMain as the entry point, it is still looking for WinMain entry point.

You can just use the first version of WinMain, then use GetCommandLineW() for Unicode command line. Example:

int argc;
wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &argc );
for (int i = 0; i < argc; i++)
{
    //output argv[i]
}

There is a difference however between lpCmdLine and GetCommandLineW. See documentation

WinMain:

lpCmdLine: The command line for the application, excluding the program name

GetCommandLine:

GetCommandLineW(): The command-line string for the current process

Note, you should use Visual Studio if you can. It's free!

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 3
    *"`GetCommandLineW()` includes the path of the executable as well as arguments."* - No, it returns the command line as set up by the process calling `CreateProcess`. It is a common **convention** to pass repeat the module name as the first argument, but there is no requirement to do so, and there certainly is nothing to enforce this. *"Note, you should use Visual Studio if you can. It's free!"* - Arguably even more importantly, VS defaults to Unicode project settings (unlike Code::Blocks). – IInspectable Jul 17 '16 at 14:01
  • @IInspectable Thanks, I edited the question, tried to fix that comment. – Barmak Shemirani Jul 17 '16 at 16:06
  • GCC is a really nice compiler, it's such a shame that the Windows tooling is so pathetic. I mean really, the lack of comprehensive Unicode support is ridiculous. Someone should really work on improving MinGW.... A clean-room port of the MSVC runtime libraries does not seem like it would be difficult, just rather time-consuming. – Cody Gray - on strike Jul 17 '16 at 17:39
  • @CodyGray: Porting the CRT **is** difficult. I wouldn't even know where to start to implement exception handling. And do it in a way that's different enough from MS' implementation to keep legal issues out of the way. – IInspectable Jul 18 '16 at 10:35