Skip to content

Instantly share code, notes, and snippets.

@meitinger
Last active November 25, 2018 23:34
Show Gist options
  • Save meitinger/9980404f2658fc2017dc226d8cea6d4f to your computer and use it in GitHub Desktop.
Save meitinger/9980404f2658fc2017dc226d8cea6d4f to your computer and use it in GitHub Desktop.
Utility that simply launches another program.
/* Copyright (C) 2018, Manuel Meitinger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define STRICT
#include <Windows.h>
#pragma comment(linker, "/ENTRY:Run")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#pragma comment(lib, "Kernel32.lib")
#pragma comment(lib, "User32.lib")
void Run(void)
{
DWORD exitCode;
LPWSTR cmdLine;
STARTUPINFOW startupInfo;
PROCESS_INFORMATION processInfo;
cmdLine = GetCommandLineW();
if (*cmdLine == L'"')
{
cmdLine++;
while (*cmdLine)
{
if (*cmdLine++ == L'"')
{
break;
}
}
}
else
{
while (*cmdLine && *cmdLine != L' ' && *cmdLine != L'\t')
{
cmdLine++;
}
}
while (*cmdLine == L' ' || *cmdLine == L'\t')
{
cmdLine++;
}
SecureZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_FORCEONFEEDBACK;
SecureZeroMemory(&processInfo, sizeof(processInfo));
if (CreateProcessW(NULL, cmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo))
{
AllowSetForegroundWindow(processInfo.dwProcessId);
exitCode = ERROR_SUCCESS;
}
else
{
exitCode = GetLastError();
}
if (processInfo.hProcess)
{
CloseHandle(processInfo.hProcess);
}
if (processInfo.hThread)
{
CloseHandle(processInfo.hThread);
}
ExitProcess(exitCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment