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

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

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

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

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.

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

Related

How to display variables on windows api message box C programming

I'm trying to print out a variable to a message box from Language C
This is my current code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreveInstance, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
char temp[128];
sprintf(temp, "The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
my attempt was assigning the string which included a variable and then I putting that assigned string into the MessageBox but whenever I compiled this It will give me a warning saying
error C2220: warning treated as error - no 'object' file generated
warning C4133: 'function': incompatible types - from 'char [128]' to 'LPCWSTR'
warning C4100: 'nCmdShow': unreferenced formal parameter
warning C4100: 'lpCmdLine': unreferenced formal parameter
warning C4100: 'hPreveInstance': unreferenced formal parameter
warning C4100: 'hInstance': unreferenced formal parameter
would there be any solution to this?
I am currently using Visual Studio 2017
MessageBox is actually a macro - there are two versions: MessageBoxA which takes chars and MessageBoxW which takes wide chars. Depending on the default character set, it will take either the A or W version. By default, it takes the W version.
If you go into the project properties, under general, near the bottom of the dialog, there is an entry for character set. By default, it is set to unicode (the W version). Just change this to MBCS (Multi byte character set) and your program should build after you've removed the L from the MessageBox title
Alternatively leave it as Unicode and change the code to the following. Note that you don't need winmain if it is not using the GUI. You can use MessageBox in a console application
int main()
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
wchar temp[128];
wsprintf(temp, L"The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
There is a third solution using TCHAR but I'll have to look it up before I post it.
Edit the third solution
If you look in stdafx.h, it has probably already included tchar.h. These are character agnostic definitions. You can use a MessageBox with a C++ Win32 console application.
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
TCHAR temp[128];
_stprintf(temp, _T("The die shows: %d"), dice);
MessageBox(NULL, temp, _T("Dice"), MB_YESNO);
return 0;
}

I am trying to run this code in code::blocks and it's showing a an error like undefined reference to 'WinMain#16'. Can anyone help me fix the problem?

#include <windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int CmdShow)
{
MessageBoxW(NULL,szCmdLine, L"Title", MB_OK);
return 0;
}
I presume you're using Visual Studio. In project properties, under General, check Character Set. It should be Unicode.
With Unicode, the toolchain assumes your entry point is wWinMain, with multibyte it assumes WinMain.

mingw g++ Windows Subsystem WinMain not getting hInstance value

I'm converting a VS2015 C++ directx/winforms app to VS code using mingw G++ (both on windows 10).
I have it compiling and linking after adding the -mwindows option (and a whole bunch of libraries) but upon stepping into the WinMain there is no value in hInstance.
#include <objbase.h>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LoadString(hInstance, IDS_APP_TITLE,g_szAppName, MAX_LOADSTRING);
...
Which results in anything depending on it like LoadString or RegisterClassEx not working.
What should I be looking for?

Nothing printing to stdout from WinMain - C/VS 2017

I should preface with the fact that I'm relatively new to VS, however I am not new to C.
The problem I'm encountering is that nothing shows up on stdout when printed. Neither printf/_s, nor fprintf/_s(stdout, ...) produce any output. Interestingly enough fprinf(file, ...) does in fact produce output to the given file. Is there a chance this has to do with printf deprecation (I have tried the preproc. _CRT_SECURE_NO_DEPRECATE)?
Below is my full program:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
FILE * pFile = fopen("outputTest.txt", "w");
fprintf(pFile, "At top of main1.\n"); //works
printf("At top of main2.\n"); //doesn't work
printf_s("At top of main3.\n"); //doesn't work
fprintf_s(stdout, "At top of main4.\n"); //doesn't work
fflush(stdout);
fclose(pFile);
return FALSE;
}
I'm using Visual Studio 2017, and the program is a Win32 (App?). Also I've ruled out the possibility that Linker->System->Subsystem is the problem.
Any ideas are appreciated.
EDIT: I'm not sure if it matters but the "Solution Platforms" dropdown at the top of VS says Win32, unlike when you create a new "Windows Desktop App." where it says x86.

Weird Error in WinMain() to main() Macro

/** converts 'WinMain' to the traditional 'main' entrypoint **/
#define PRO_MAIN(argc, argv)\
int __main (int, LPWSTR*, HINSTANCE, int);\
int WINAPI WinMain (HINSTANCE __hInstance, HINSTANCE __hPrevInstance, \
LPSTR __szCmdLine, int __nCmdShow)\
{\
int nArgs;\
LPWSTR* szArgvW = CommandLineToArgvW (GetCommandLineW(), &nArgs);\
assert (szArgvW != NULL);\
return __main (nArgs, szArgvW, __hInstance, __nCmdShow);\
}\
\
int __main (int __argc, LPWSTR* __argv, HINSTANCE __hInstance, int __nCmdShow)
Now, when I use this code here:
PRO_MAIN(argc, argv)
{
...
}
I get the error:
error: conflicting types for '__main'
note: previous declaration of '__main' was here
What's the problem?
You have broken the rules: double-underscores are reserved for implementation! (Among other things.)
You simply cannot use __main, main__, _Main, etc. You should pick something else.
I would recommend you make this work:
int main(int argc, char* argv[])
{
// main like normal
}
// defines WinMain, eventually makes call to main()
PRO_MAIN;
Which has the added advantage that for non-Windows applications, PRO_MAIN can simply expand to nothing, and the program still compiles with the standard main function. This is what I do.

Resources