Windows Command Screen - c

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().

Related

How to correctly build code for gcc with netapi32?

I just try build this example for GCC.
But I got several problems.
My system is:
Win10
msys64 with ucrt64 (x86_64-w64-mingw32)
c:\windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2486.1.5\amd64_microsoft-windows-netapi32_31bf3856ad364e35_10.0.19041.2130_none_0202483cb6bb2fc6\f\netapi32.dll
Atom as IDE (I have not VS)
cmd as console
I try: gcc main.c -o main.exe -lnetapi32
But gcc returned:
undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status
OK - wrong entry point.
Changed wMain in WinMain.
Result:
main.c:11:5: error: conflicting types for 'WinMain'; have 'int(int, wchar_t **)' {aka 'int(int, short unsigned int **)'}
11 | int WinMain(int argc, wchar_t *argv[])
| ^~~~~~~
In file included from c:/msys64/ucrt64/include/windows.h:70,
from main.c:8:
c:/msys64/ucrt64/include/winbase.h:1128:14: note: previous declaration of 'WinMain' with type 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, CHAR *, int)' {aka 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, char *, int)'}
1128 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
What is wrong?
wmain is an extension by Microsoft, and insofar I know not supported by gcc. You need to change the entry function to a standard int main(int argc, char **args). You can also use WinMain but then you'll have to use int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

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.

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

Resources