Created
July 21, 2012 09:22
-
-
Save hostage/3155194 to your computer and use it in GitHub Desktop.
Start a Windows console application without getting a pop-up command shell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo Building with GCC for windows (http://www.gnu.org/software/gcc/) | |
set PATH=%PATH%;%GCCTOOLS% | |
pushd | |
cd /d %~dp0 | |
gcc -Wl,-subsystem,windows ScriptWrapper.c -o ScriptWrapper | |
popd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
/* | |
We define WinMain to simply run the user's command line; this is nothing they | |
couldn't already do via Windows+Run. It is useful if you want to prevent any | |
console windows from appearing on the user's desktop. | |
We wait for the process to exit and return the exit code to the caller. | |
@param hInstance Not used; o/s module handle for current process. | |
@param hPrevInstance Not used; o/s module handle for other process. | |
@param lCmdLine Command line to execute. | |
@param nCmdShow Not used; o/s suggested window mode. | |
@return Exit status for the child process or -1 if the child could not be run. | |
*/ | |
int APIENTRY WinMain( | |
HINSTANCE hInstance, | |
HINSTANCE hPrevInstance, | |
LPSTR lpCmdLine, | |
int nCmdShow) | |
{ | |
// Create an empty STARTUPINFO; we don't need to change any window parameters | |
// because there will be no windows, so we leave this blank. | |
STARTUPINFO sinfo; | |
ZeroMemory(&sinfo, sizeof(STARTUPINFO)); | |
sinfo.cb = sizeof(STARTUPINFO); | |
// This will be populated with information about the process being started. | |
PROCESS_INFORMATION pinfo; | |
ZeroMemory(&pinfo, sizeof(PROCESS_INFORMATION)); | |
int exitcode = -1; | |
if (CreateProcess( | |
// Setting the application name to NULL tells CreateProcess to find the | |
// executable name on the command line. | |
NULL, | |
lpCmdLine, | |
NULL, // Default process attributes is fine. | |
NULL, // Default thread attributes is fine. | |
FALSE, // Do not permit inherited handles; the child shouldn't need our handles. | |
CREATE_NO_WINDOW, // This is what prevents the pop-up window. | |
NULL, // Setting the environment to null means the child will inherit ours. | |
NULL, // Setting the current directory to null means the child will use the same one as us. | |
&sinfo, | |
&pinfo)) | |
{ | |
// Wait "forever" for our child to wake up | |
DWORD dwError = WaitForSingleObject(pinfo.hProcess, INFINITE); | |
if (dwError != WAIT_OBJECT_0) | |
{ | |
// If something interrupted our wait we abort the process | |
TerminateProcess(pinfo.hProcess, 0); | |
} | |
else | |
{ | |
// Get the process exit code if possible | |
DWORD dwError; | |
if (GetExitCodeProcess(pinfo.hProcess, &dwError)) | |
{ | |
exitcode = dwError; | |
} | |
} | |
// Explicitly closing the handles we opened; mainly for completeness. | |
CloseHandle(pinfo.hProcess); | |
CloseHandle(pinfo.hThread); | |
} | |
return exitcode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment