关于c ++:WinMain可以编译,但是wWinMain不在CodeBlocks中

WinMain compiles, but wWinMain does not in CodeBlocks

本问题已经有最佳答案,请猛点这里访问。

因此,我试图使用Win32在CodeBlocks中创建一个窗口,到目前为止,只有此版本的WinMain可以工作(注意:这只是一个简单而天真的示例):

1
2
3
4
5
6
#include <windows.h>

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

但是此版本不:

1
2
3
4
5
6
#include <windows.h>

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

据我所知,后者期望第三个参数是指向一串宽字符的指针,而前者则不然。 但是当我在CodeBlocks中编译时,我得到的只是以下消息:

undefined reference to WinMain@16

显然,CodeBlocks期望WinMain的版本不接收LPWSTR值作为参数。
我的问题是,我该如何配置CodeBlocks以便使用wWinMain进行编译?


wWinMain是特定于编译器的。 Visual Studio支持它。 Code :: Block通常是用MinGW设置的,它将编译wWinMain,但由于没有将wWinMain识别为入口点,所以它给出了链接错误,它仍在寻找WinMain入口点。

您可以仅使用WinMain的第一个版本,然后将GetCommandLineW()用于Unicode命令行。 例:

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

但是lpCmdLineGetCommandLineW之间存在差异。 查看文件

WinMain:

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

GetCommandLine:

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

注意,如果可以,您应该使用Visual Studio。 免费!