C语言 const char* 类型的参数与“LPCWSTR”类型的参数不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21834833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:46:15  来源:igfitidea点击:

argument of type const char* is incompatible with parameter of type "LPCWSTR"

c

提问by

I am trying to make a simple Message Box in C in Visual Studio 2012, but I am getting the following error messages

我正在尝试在 Visual Studio 2012 中用 C 语言制作一个简单的消息框,但我收到以下错误消息

argument of type const char* is incompatible with parameter of type "LPCWSTR"

err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup

Here is the source code

这是源代码

#include<Windows.h>

int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{

    MessageBox(0,"Hello","Title",0);

    return(0);
}

Please Help

请帮忙

Thanks and Regards

感谢致敬

回答by cup

To make your code compile in both modes, enclose the strings in _T() and use the TCHAR equivalents

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

#include <tchar.h>
#include <windows.h>

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

回答by Jasurbek Nabijonov

To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.

要在 Visual C++ 中编译代码,您需要使用多字节字符 WinAPI 函数而不是宽字符函数。

Set Project -> Properties -> General -> Character Set option to Use Multi-Byte Character Set

设置项目 -> 属性 -> 常规 -> 字符集选项以使用多字节字符集

I found it here https://stackoverflow.com/a/33001454/5646315

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

回答by mkchandler

I recently ran in to this issue and did some research and thought I would document some of what I found here.

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

To start, when calling MessageBox(...), you are really just calling a macro (for backwards compatibility reasons) that is calling either MessageBoxA(...)for ANSI encoding or MessageBoxW(...)for Unicode encoding.

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

So if you are going to pass in an ANSI string with the default compiler setup in Visual Studio, you can call MessageBoxA(...)instead:

因此,如果您要在 Visual Studio 中使用默认编译器设置传递 ANSI 字符串,则可以MessageBoxA(...)改为调用:

#include<Windows.h>

int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{

    MessageBoxA(0,"Hello","Title",0);

    return(0);
}

Full documentation for MessageBox(...)is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

完整文档MessageBox(...)位于此处:https: //msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

And to expand on what @cup said in their answer, you could use the _T()macro and continue to use MessageBox():

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

#include<tchar.h>
#include<Windows.h>

int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{

    MessageBox(0,_T("Hello"),_T("Title"),0);

    return(0);
}

The _T()macro is making the string "character set neutral". You could use this to setup all strings as Unicode by defining the symbol _UNICODEbefore you build (documentation).

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

Hope this information will help you and anyone else encountering this issue.

希望这些信息对您和其他遇到此问题的人有所帮助。