Skip to content

Instantly share code, notes, and snippets.

@maxtruxa
Last active December 30, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxtruxa/7883151 to your computer and use it in GitHub Desktop.
Save maxtruxa/7883151 to your computer and use it in GitHub Desktop.
CLI utility for Windows that can start a process hiding it's window. #c #windows #cli
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Max Truxa
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>
int printUsage() {
TCHAR executable[MAX_PATH];
DWORD length;
DWORD errCode;
length = GetModuleFileName(NULL, executable, _countof(executable));
if (length == 0 || (length == _countof(executable) && GetLastError() != ERROR_SUCCESS)) {
_tcscpy_s(executable, _countof(executable), "hstart");
}
// In Visual Studio 13 and earlier "%s" is mapped to the natural width of the format string.
// Microsoft fixed this in Visual Studio 2014 CTP1 (_MSC_VER == 1900) and introduced "%Ts".
#if defined _MSC_VER && MSC_VER >= 1900
# define STRING_FORMAT_SPECIFIER _T("%Ts")
#else
# define STRING_FORMAT_SPECIFIER _T("%s")
#endif
_tprintf(
_T("Usage:\n")
_T(" ") STRING_FORMAT_SPECIFIER _T(" <CommandToRun>\n"),
executable
);
return 0;
}
int runSilent(TCHAR* cmdLine) {
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
DWORD errCode;
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
if (CreateProcess(NULL, cmdLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi) == FALSE) {
errCode = GetLastError();
_tprintf(_T("ERROR %u\n"), errCode);
return (int)errCode;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
int CALLBACK _tWinMain(HINSTANCE, HINSTANCE, TCHAR* cmdLine, int) {
if (*cmdLine == '\0') {
return printUsage();
}
return runSilent(cmdLine);
}
@maxtruxa
Copy link
Author

This is the solution of this question on stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment