Process control v0.1.0

Source
Starting processes with CreateProcessW, their startup information, inherited handles and waits.

SecurityAttributes

pub struct SecurityAttributes {
    pub length: uint32;
    pub securityDescriptor: *opaque;
    pub inheritHandle: bool32;
}

The SECURITY_ATTRIBUTES a pipe or file is created with: here only whether its handle is inheritable.

Source

Fields

NameTypeDescription
lengthuint32The structure's size in bytes.
securityDescriptor*opaqueA security descriptor, or null for the default.
inheritHandlebool32Non-zero when a child process may inherit the handle.

StartupInfo

pub struct StartupInfo {
    pub size: uint32;
    pub reserved: *opaque;
    pub desktop: *uint16;
    pub title: *uint16;
    pub x: uint32;
    pub y: uint32;
    pub xSize: uint32;
    pub ySize: uint32;
    pub xCountChars: uint32;
    pub yCountChars: uint32;
    pub fillAttribute: uint32;
    pub flags: uint32;
    pub showWindow: uint16;
    pub reserved2Size: uint16;
    pub reserved2: *opaque;
    pub standardInput: *opaque;
    pub standardOutput: *opaque;
    pub standardError: *opaque;
}

The STARTUPINFOW a new process is started with; this package fills in its standard handles.

Source

Fields

NameTypeDescription
sizeuint32The structure's size in bytes.
reserved*opaqueReserved; null.
desktop*uint16The desktop, or null.
title*uint16The console title, or null.
xuint32The window's left edge, when flags says so.
yuint32The window's top edge, when flags says so.
xSizeuint32The window's width, when flags says so.
ySizeuint32The window's height, when flags says so.
xCountCharsuint32The console buffer's width in characters, when flags says so.
yCountCharsuint32The console buffer's height in characters, when flags says so.
fillAttributeuint32The console's text and background colors, when flags says so.
flagsuint32Which fields the system reads, such as STARTF_USESTDHANDLES.
showWindowuint16How the window is shown, when flags says so.
reserved2Sizeuint16Reserved; zero.
reserved2*opaqueReserved; null.
standardInput*opaqueThe child's standard input.
standardOutput*opaqueThe child's standard output.
standardError*opaqueThe child's standard error.

ProcessInformation

pub struct ProcessInformation {
    pub process: *opaque;
    pub thread: *opaque;
    pub processId: uint32;
    pub threadId: uint32;
}

The PROCESS_INFORMATION CreateProcessW fills in: the new process and its first thread.

Source

Fields

NameTypeDescription
process*opaqueThe new process's handle, to wait on and close.
thread*opaqueIts first thread's handle, to close.
processIduint32The process identifier.
threadIduint32The first thread's identifier.

STARTF_USESTDHANDLES

pub const STARTF_USESTDHANDLES: uint32 = 0x00000100;

STARTF_USESTDHANDLES: the standard handles in StartupInfo are the child's.

Source

CREATE_UNICODE_ENVIRONMENT

pub const CREATE_UNICODE_ENVIRONMENT: uint32 = 0x00000400;

CREATE_UNICODE_ENVIRONMENT: the environment block is UTF-16.

Source

HANDLE_FLAG_INHERIT

pub const HANDLE_FLAG_INHERIT: uint32 = 0x00000001;

HANDLE_FLAG_INHERIT: the handle is inherited by child processes.

Source

DUPLICATE_SAME_ACCESS

pub const DUPLICATE_SAME_ACCESS: uint32 = 0x00000002;

DUPLICATE_SAME_ACCESS: a duplicated handle has the access of the original.

Source

WAIT_TIMEOUT

pub const WAIT_TIMEOUT: uint32 = 0x00000102;

WAIT_TIMEOUT: a wait ended because its time ran out.

Source

ERROR_BROKEN_PIPE

pub const ERROR_BROKEN_PIPE: uint32 = 109;

ERROR_BROKEN_PIPE: the other end of the pipe was closed.

Source

CreateProcessW

pub extern func CreateProcessW(
    applicationName: *uint16,
    commandLine: *var uint16,
    processAttributes: *SecurityAttributes,
    threadAttributes: *SecurityAttributes,
    inheritHandles: bool32,
    creationFlags: uint32,
    environment: *opaque,
    currentDirectory: *uint16,
    startupInfo: *StartupInfo,
    processInformation: *var ProcessInformation
) -> bool32

Starts a program as a new process.

See also: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw

Source

Parameters

NameTypeDescription
applicationName*uint16the program's path, or null to take it from the command line
commandLine*var uint16the command line, which the call may modify
processAttributes*SecurityAttributesthe process handle's security, or null
threadAttributes*SecurityAttributesthe thread handle's security, or null
inheritHandlesbool32non-zero to let the child inherit the parent's inheritable handles
creationFlagsuint32flags such as CREATE_UNICODE_ENVIRONMENT
environment*opaquethe environment block, or null to inherit the parent's
currentDirectory*uint16the child's working directory, or null for the parent's
startupInfo*StartupInfohow the child starts, including its standard handles
processInformation*var ProcessInformationreceives the new process and thread

Returns: non-zero on success, and zero on failure with the reason in GetLastError

GetExitCodeProcess

pub extern func GetExitCodeProcess(
    process: *opaque,
    exitCode: *var uint32
) -> bool32

Reads a process's exit code, or STILL_ACTIVE while it runs.

See also: https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess

Source

Parameters

NameTypeDescription
process*opaquethe process handle
exitCode*var uint32receives the code

Returns: non-zero on success, and zero on failure with the reason in GetLastError

TerminateProcess

pub extern func TerminateProcess(
    process: *opaque,
    exitCode: uint32
) -> bool32

Ends a process and all its threads.

See also: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess

Source

Parameters

NameTypeDescription
process*opaquethe process handle
exitCodeuint32the code the process ends with

Returns: non-zero on success, and zero on failure with the reason in GetLastError

CreatePipe

pub extern func CreatePipe(
    readPipe: *var *opaque,
    writePipe: *var *opaque,
    attributes: *SecurityAttributes,
    size: uint32
) -> bool32

Creates an anonymous pipe.

See also: https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createpipe

Source

Parameters

NameTypeDescription
readPipe*var *opaquereceives the read end
writePipe*var *opaquereceives the write end
attributes*SecurityAttributeswhether the ends are inheritable, or null
sizeuint32the buffer size suggestion, or zero for the default

Returns: non-zero on success, and zero on failure with the reason in GetLastError

SetHandleInformation

pub extern func SetHandleInformation(
    handle: *opaque,
    mask: uint32,
    flags: uint32
) -> bool32

Changes a handle's flags, such as whether it is inherited.

See also: https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-sethandleinformation

Source

Parameters

NameTypeDescription
handle*opaquethe handle
maskuint32the flags to change
flagsuint32their new values

Returns: non-zero on success, and zero on failure with the reason in GetLastError

DuplicateHandle

pub extern func DuplicateHandle(
    sourceProcess: *opaque,
    sourceHandle: *opaque,
    targetProcess: *opaque,
    targetHandle: *var *opaque,
    desiredAccess: uint32,
    inheritHandle: bool32,
    options: uint32
) -> bool32

Duplicates a handle, here into the same process with different inheritance.

See also: https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle

Source

Parameters

NameTypeDescription
sourceProcess*opaquethe process that owns the handle
sourceHandle*opaquethe handle
targetProcess*opaquethe process to duplicate it into
targetHandle*var *opaquereceives the duplicate
desiredAccessuint32the access to grant, ignored with DUPLICATE_SAME_ACCESS
inheritHandlebool32non-zero to make the duplicate inheritable
optionsuint32flags such as DUPLICATE_SAME_ACCESS

Returns: non-zero on success, and zero on failure with the reason in GetLastError

GetCurrentProcess

pub extern func GetCurrentProcess() -> *opaque

Returns a pseudo-handle for the calling process, which needs no closing.

See also: https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess

Source

Returns: the pseudo-handle

SetEnvironmentVariableW

pub extern func SetEnvironmentVariableW(
    name: *uint16,
    value: *uint16
) -> bool32

Sets or removes an environment variable of the calling process.

See also: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew

Source

Parameters

NameTypeDescription
name*uint16the variable's name, NUL-terminated
value*uint16its new value, NUL-terminated, or null to remove it

Returns: non-zero on success, and zero on failure with the reason in GetLastError