类型为const char *的参数与“LPCWSTR”类型的参数不兼容

我试图在Visual Studio 2012中使用C创建一个简单的Message Box,但是我收到以下错误消息

argument of type const char* is incompatible with parameter of type "LPCWSTR" err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup 

这是源代码

 #include int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow) { MessageBox(0,"Hello","Title",0); return(0); } 

请帮忙

感谢致敬

要使代码在两种模式下编译,请将字符串括在_T()中并使用TCHAR等效项

 #include  #include  int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPrevinstance, LPTSTR lpszCmdLine, int nCmdShow) { MessageBox(0,_T("Hello"),_T("Title"),0); return 0; } 

要在Visual C ++中编译代码,您需要使用Multi-Byte char WinAPI函数而不是Wide char函数。

将项目 – >属性 – >常规 – >字符集选项设置为使用多字节字符集

我在这里找到了https://stackoverflow.com/a/33001454/5646315

我最近遇到了这个问题并做了一些研究,并认为我会记录我在这里找到的一些内容。

首先,在调用MessageBox(...) ,您实际上只是调用一个宏(出于向后兼容性原因),它为ANSI编码调用MessageBoxA(...)或为Unicode编码调用MessageBoxA(...)

因此,如果要使用Visual Studio中的默认编译器设置传入ANSI字符串,则可以调用MessageBoxA(...)

 #include int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow) { MessageBoxA(0,"Hello","Title",0); return(0); } 

MessageBox(...)完整文档位于: https : MessageBox(...) v = MessageBox(...) .aspx

为了扩展@cup在答案中所说的内容,您可以使用_T()宏并继续使用MessageBox()

 #include #include int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow) { MessageBox(0,_T("Hello"),_T("Title"),0); return(0); } 

_T()宏使字符串“字符集中立”。 您可以使用此方法通过在构建( 文档 )之前定义符号_UNICODE将所有字符串设置为Unicode。

希望这些信息能够帮助您和其他任何人遇到此问题。