Skip to content

Instantly share code, notes, and snippets.

@peta909
Last active January 18, 2019 01:06
Show Gist options
  • Save peta909/2879f05b0e2074e5440c0ad5f032e397 to your computer and use it in GitHub Desktop.
Save peta909/2879f05b0e2074e5440c0ad5f032e397 to your computer and use it in GitHub Desktop.
Simple CreateProcessW()
#include <stdio.h> //c header for things like Printf
#include <Windows.h> //Added in order to use windows apis; could also be added to pch.h
int main()
{
wchar_t cmd[] = L"notepad.exe";//unicode string as parameters for strings are unicode for CreateProcessW
STARTUPINFO si = { sizeof(si) };
//memset(&si, 0, sizeof(si));//These 2 lines are the same as the init done via C style shortcut in the line above
//si.cb = sizeof(ci)
PROCESS_INFORMATION pi;
BOOL OK = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (OK)// check if Process is created
{
printf("Applicaiton is running\n");
printf("PID = %d\n", pi.dwProcessId);
//Wait forever till Process object is signaled (terminated)
DWORD status = WaitForSingleObject(pi.hProcess, INFINITE);
if (status == WAIT_OBJECT_0)//The state of the specified object is signaled.
{
printf("PID = %d is closed!\n", pi.dwProcessId);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
printf("Application NOT running! \t Error code %d", GetLastError());
}
//Add breakpoint here to prevent console from automatically closing after debugging is finished
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment