Skip to content

Instantly share code, notes, and snippets.

@Galarius
Created October 22, 2016 08:55
Show Gist options
  • Save Galarius/e43a27cb42c1d1441c40c6b21e9c59ad to your computer and use it in GitHub Desktop.
Save Galarius/e43a27cb42c1d1441c40c6b21e9c59ad to your computer and use it in GitHub Desktop.
Create independent process in Win API
#include <Windows.h>
#include <cstdio>
void MsgBoxLastError()
{
LPWSTR lpMsgBuf = NULL;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpMsgBuf,
0, NULL) != 0)
{
MessageBox(NULL, lpMsgBuf, L"Error", MB_OK);
}
LocalFree(lpMsgBuf);
}
int main(int argc, char* argv[])
{
const WCHAR pathToApp[] = L"C:/Program Files (x86)/Internet Explorer/iexplore.exe";
const WCHAR pathToFile[] = L"file://C:/Users/galar/Desktop/test.pdf";
WCHAR fullCommandLine[MAX_PATH] = { 0 };
swprintf_s(fullCommandLine, L"\"%s\" \"%s\"", pathToApp, pathToFile);
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(NULL, fullCommandLine, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
} else {
MsgBoxLastError();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment