Skip to content

Instantly share code, notes, and snippets.

@un4ckn0wl3z
Created March 19, 2023 04:47
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 un4ckn0wl3z/340f018cfc162454b780f8e33410898c to your computer and use it in GitHub Desktop.
Save un4ckn0wl3z/340f018cfc162454b780f8e33410898c to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
namespace PInvoke
{
internal class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool CreateProcessW(string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,
string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
static void Main(string[] args)
{
var si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
var pa = new SECURITY_ATTRIBUTES();
pa.nLength = Marshal.SizeOf(pa);
var ta = new SECURITY_ATTRIBUTES();
ta.nLength = Marshal.SizeOf(ta);
var pi = new PROCESS_INFORMATION();
var success = CreateProcessW(
"C:\\Windows\\System32\\notepad.exe",
null,
ref pa,
ref ta,
false,
0,
IntPtr.Zero,
"C:\\Windows\\System32",
ref si,
out pi);
if (success)
Console.WriteLine("Process created with PID: {0}.", pi.dwProcessId);
else
Console.WriteLine("Failed to create process. Error code: {0}.", Marshal.GetLastWin32Error());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment