Functions for process manipulation
This struct encapsulates process hooking functionality.
Returns a Process with pseudo handle of current processes retrieved by GetCurrentProcess that is valid only in the context of current processes.
Returns a Process with "real" handle of current processes that is valid in the context of other processes.
Returns process identifiers of all running processes.
import std.algorithm; bool isProcessRunning(int processId) { return Process.getRunningIds().canFind(processId); }
Construct a Process from a processId. If tryUsePseudoHandle is true and processId is current process id then pseudo handle with PROCESS_ALL_ACCESS access will be used. Otherwise if desiredAccess is non-zero then a process handle will be opened with requested access. Otherwise no handle is opened. In the latter case for each member function with "Required handle access" paragraph in documentation call a temporary handle with required access is opened.
Construct a Process from a processHandle. processHandle access obtained when it was opened should be passed as handleAccess parameter. If remainPseudoHandle is false and processHandle is pseudo handle of current process then "real" handle with access from handleAccess will be opened instead. If remainPseudoHandle is true and processHandle is pseudo handle then handleAccess will be set to PROCESS_ALL_ACCESS.
processId will not be set iff resulting handleAccess doesn't include PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION. In this case calling closeHandle will result in unassociation of this struct.
Launch a new process.
handleAccess will be set to PROCESS_ALL_ACCESS. primaryThread will be set.
// With executable file searching: { Process(ProcessStartInfo("notepad", true)); } // Without executable file searching: import std.process; immutable path = environment["windir"] ~ `\system32\notepad.exe`; { Process(ProcessStartInfo(path, null)); } // using file & arguments { Process(ProcessStartInfo('"' ~ path ~ '"')); } // using command line
Returns whether process is associated with a process. It is asserted that no member functions are called for an unassociated Process struct.
assert(Process.currentLocal.associated); assert(!Process.init.associated); auto h = Process.init.handle; // assert violation
Gets the native handle.
Gets access to the handle.
Gets the process identifier.
Gets associated ProcessMemory instance.
Determines whether this process is running under WOW64.
Required handle access: PROCESS_QUERY_INFORMATION
Initializes internal Windows stuff required for WinAPI functions like EnumProcessModules.
When a process is created such stuff isn't initialized unless some process code is executed.
import std.process; auto p = Process(environment["windir"] ~ `\system32\notepad.exe`, null, true); scope(exit) p.terminate(); HMODULE[256] buff; DWORD needed; // The call will fail because internal Windows stuff isn't initialized yet: assert(!EnumProcessModules(p.handle, buff.ptr, buff.sizeof, &needed)); p.initializeWindowsStuff(); assert(EnumProcessModules(p.handle, buff.ptr, buff.sizeof, &needed));
Gets loaded modules.
import std.process: environment; auto p = Process(environment["windir"] ~ `\system32\notepad.exe`, null, true); scope(exit) p.terminate(); p.initializeWindowsStuff(); auto modules = p.getModules();
Returns thread identifiers of all running threads in the process.
Required handle access: PROCESS_QUERY_INFORMATION | PROCESS_VM_READ
Loads module into the process.
Terminates the process and set its exit code with exitCode.
Calls
TerminateProcess.
Required handle access:
PROCESS_TERMINATE
Waits for the process to exit and returns exit code.
Required handle access: SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION
Waits the specified duration for the process to exit and returns wheter it exited. If it does exited, set exitCode with the process exit code.
Required handle access: SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION
Closes native handle if any.