I tried to compile the codes above with Code::Blocks, but it can't be done.
undefined reference to 'WinMain@16'
Since I'm very new to windows programming, I can't figure out what the error codes mean. Is there any mistake in these codes and how to fix it?
Does this relate to the SDK? Can Code::Blocks accomplish windows programming? I tried to compile the codes with Microsoft Visual C++ 2010 Express(the free version, http://www.microsoft.com/express/Downloads/#2010-Visual-CPP), but I couldn't even find the BUILD button.
And the BMP Loader(from http://www.cplusplus.com/src/) won't work for me either. So please tell me whether Code::Blocks is OK for windows programming or I should go for something else.
It's a linker issue. You need to tell the linker it's a Console app and not a Windows app. The two kinds of apps have different entry points (main and WinMain respectively).
Code Blocks can work with windows programs just fine, but I don't think all compilers support wWinMain (Code::Blocks is mostly used with GCC/MinGW, so I guess you have that). Try WinMain instead and replace the PWSTR in the arguments by PSTR. If you want to get Unicode command line arguments with MinGW use the GetCommandLineW function instead.
It works. You've got my first windows program done! Thank you very much.
But why does your way work? You mean GCC/MinGW doesn't support wWinMain and PWSTR right? So what exactly are WinMain, wWinMain, PWSTR and PSTR? Are they part of windows.h? How can I distinguish these differences from various compilers?
And what are Unicode and GetCommandLineW? Where can I learn all the beginners' stuff?
WinMain
The Entry point for a Windows GUI application is WinMain. It's just a matter of fact. The designers could have used main, but found it more suitable to pass in different arguments to the app at startup.
When WIN32 came around, it had explicit support for non-GUI apps and so was forced to support main as an entry point. So, if you build your app as a GUI, the environment uses WinMain and a Console app uses main.
WinMain/wWinMain, PSTR/PWSTR, GetCommandLine/GetCommandLineW
The WIN32 API is Unicode-16. However, it does support ANSI with CodePages as per the previous versions of Microsofts Operating Systems. GetCommandLine() is the ANSI version, GetCommandLineW() is the Unicode version. The same applised all the W functions and types.
Whereas it is more convenient to use the ANSI, all ANSI functions convert to/from Unicode before calling into the kernel as necessary.
Technically GetCommandLine() will resolve to either GetCommandLineW() [Unicode] or GetCommandLineA() [ANSI] depending on UNICODE settings. The same as most of the API.