****C newbie alert**** How do I compile a C app so that it runs without showing a console window on Windows? I'm using Windows XP and GCC 3.4.5 (mingw-vista special r3). I've googled this exhaustively and I've come up with the following which, according to what I've read, sounds like it's supposed to do the trick, but doesn't on my system:
#include <windows.h>
#include <stdlib.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
system("start notepad.exe");
}
I've also tried passing the "-mwindows" argument to GCC to no avail. The code sample launches Notepad but still flashes up a command prompt.
EDIT: FWIW I have also tried ShellExecute as an alernative to system(), although I would be happy to even get an app with an empty main() or WinMain() working at this point.
Retain the -mwindows flag and use this:
#include <windows.h>
#include <process.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
execl("c:\\winnt\\system32\\notepad.exe", 0);
// or: execlp("notepad.exe", 0);
}
Note: you need the full path for the execl() call but not the execlp() one.
Edit: a brief explanation of why this works - using system() starts a shell (like cmd.exe) to exec the command which produces a console window. Using execl doesn't.
Related
Currently I'm having issues with WinMain in C (specifically in Visual Studio).
For instance...
#include <stdio.h>
#include <Windows.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, INT nCmdShow)
{
return(0);
}
1>------ Build started: Project: GameB, Configuration: Debug x64 ------
1>LIBCMTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
1>E:\James\VisualStudio\CProjects\GameB\x64\Debug\GameB.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "GameB.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Just this basic set-up gives me an "inconsistent annotation for WinMain" warning. I have been searching for any help for 2 days and the the closest I come to an answer is people talking about WinMain in the context of C++. I have a feeling this is a problem with Visual Studio as I was originally just using VS Code and managed to get an app (one that generated a pop-up window) to compile and run.
You just set up your winmain wrong. This should work:
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
or
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, INT nCmdShow)
{
return 0;
}
Don't forget to #include <Windows.h> and change subsystem to windows.
To use the WinMain program entry point, you need to tell the linker to target the "Windows" subsystem, rather than building a console application. Otherwise, the linker will look for (and fail to find) the standard main entry point for C programs.
In the Solution Explorer, right-click on your project and select "Properties". Then navigate to the "Linker -> System" page and select "Windows (/SUBSYSTEM:WINDOWS)" as the target:
On your "inconsistent annotation" warning, see this Q/A: Inconsistent annotation for 'WinMain'
Also, as noted in the answer by SNO, you should add the WINAPI attribute to your WinMain function.
You need to set subsystem from Console to Windows in settings, by right clicking your project in the solution explorer, selecting properties, and going to Linker->System and setting SubSystem to Windows(/SUBSYSTEM:WINDOWS).
Just working on a small project using SDL2...
System info: Windows
App info: using C (pure C, no c++), mingw-x64 & SDL2.
Now;
Firstly, SDL.h requires the main function to be renamed as WinMain.
On the other hand, when getting the screen resolution, I tended to use the GetSystemMetrics function, which requires windows.h to be included in the pre-processor section and at this point, WinMain in my code conflicts with WinMain declared previously in winbase.h. When I'm using both (SDL.h and windows.h), compiler responds:
previous declaration of 'WinMain' was here: int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
That's because, WinMain is declared somewhere in the winbase.h, which is auto-included via windows.h.
Is there a way to get the screen resolution w/o using GetSystemMetrics / windows.h? Any other ideas?
Ok! I've realized what I was doing wrong...
For the linking phase, I was using the following libraries and the linker was complaining about the WinMain function not to be found.
-lSDL2main
-lSDL2
Then I added the mingw32 library as shown below and the problem got solved (the order of the libraries does matter I think).
-lmingw32
-lSDL2main
-lSDL2
How do I specify the following nCmdShow parameter? Whats the command line argument name? Or is this parameter just used when using CreateProcess?
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow)
Should it be something like: myexe.exe /nCmdShow=1?
nCmdShow has nothing to do with app command line. Docs say next:
Controls how the window is to be shown.
And the value is one of the SW_* codes. To check what exact value is passed - check application startup source code that should be available with Windows SDK (At least was for Platform SDK for w2k). For example see here.
I am new to programming and tried to make a simple message box with title "Title" and message "hello". Instead of the desired result after building and debugging the project, it shows the title and message in Korean or Chinese language.
Here are my code:
#include <windows.h>
int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdline, int nCmdShow)
{
MessageBox(0,"Hello","Treat",0);
return (0);
}'
Try MessageBox(0,_T("Hello"),_T("Treat"),0);
_T is a macro defined within <windows.h> which will convert your string constants to UTF-16 if building for the Unicode version of Win32 API.
Try calling MessageBoxA, which takes ASCII strings, instead of MessageBox.
Okay this is the sample code
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "A threat has been detected by Windows!!", "Warning!!", MB_OK);
return 0;
}
But whenever I compile this. It gives me a Message Box as ecpected but what I get more is the Command Prompt with it. I just don't want that ugly thing how can I modify or compile my code to get only the GUI ? I am using Code::Blocks IDE.
This is related to Code Blocks IDE, you have to change the type application in your project.
From the project properties, tab Build Targets, change the type console application to GUI application.
Check: http://forums.codeblocks.org/index.php?topic=12007.0