mingw g++ Windows Subsystem WinMain not getting hInstance value - winforms

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?

Related

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.

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.

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

GCC Win32 API Linking Issue with ComCtl32

I can't get Comctl32.lib to link with my program using GCC (MinGW).
GCC Input:
gcc -o program.exe main.c images.o -lgdi32 -lcomctl32 -mwindows
GCC Output
main.c: In function 'WinMain':
main.c:120:2: error: unknown type name 'INITCOMMONCONTROLSEX'
main.c:124:9: error: request for member 'dwICC' in something not a structure or union
Related Code in main.c
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#include <windows.h>
#include <commctrl.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmd)
{
Line 120: INITCOMMONCONTROLSEX icex;
Line 124: icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
}
Thank you for any help or information you can provide. I've been at this too long and just can't come up with an answer.
I have resolved the issue thanks to David H. I had to define
#define _WIN32_IE 0x0900
I was under the impression that commctrl.h was defining 0x0500 by default (0x0300 is required for my functions), but it appears it wasn't.
If you do not define the _WIN32_IE macro in your project, it is automatically defined as 0x0500. - MSDN Source

Windows Command Screen

Here's an example script of a simple program I wrote in CodeBlocks
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Trollface", "Title", MB_OK);
return 0;
}
That's great and all, it shows a messagebox, but there is a pesky console behind it. How can I remove the console?
You have probably created a console project. You can detach from a console using FreeConsole().

Resources