user2341787
user2341787

Reputation:

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

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

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

Upvotes: 35

Views: 84318

Answers (4)

GameDev99
GameDev99

Reputation: 11

Yes whatever it was it was a wrong tutorial, you need to make it a long byte integer.

Try this:

#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
    MessageBox(0,L"Hello",L"Title",0);
    return(0);
}

Upvotes: 1

Jasurbek Nabijonov
Jasurbek Nabijonov

Reputation: 1745

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

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

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

Upvotes: 27

mkchandler
mkchandler

Reputation: 4758

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.

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

#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

And to expand on what @cup said in their answer, you could use the _T() macro and continue to use 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 _UNICODE before you build (documentation).

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

Upvotes: 6

cup
cup

Reputation: 8257

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

#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;
}

Upvotes: 25

Related Questions