Processes and threads v0.1.0

Source
Exiting, threads, waiting and loading libraries.

ALL_PROCESSOR_GROUPS

pub const ALL_PROCESSOR_GROUPS: uint16 = 0xFFFF;

ALL_PROCESSOR_GROUPS: asks GetActiveProcessorCount about every processor group at once.

Source

STACK_SIZE_PARAM_IS_A_RESERVATION

pub const STACK_SIZE_PARAM_IS_A_RESERVATION: uint32 = 0x00010000;

STACK_SIZE_PARAM_IS_A_RESERVATION: CreateThread takes its stack size as the space to reserve rather than to commit, which is what a stack size usually means.

Source

INFINITE

pub const INFINITE: uint32 = 0xFFFFFFFF;

INFINITE: a wait with no time-out.

Source

ExitProcess

pub extern func ExitProcess(exitCode: uint32)

Ends the calling process and all its threads.

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

Source

Parameters

NameTypeDescription
exitCodeuint32the status to report

FreeLibrary

pub extern func FreeLibrary(libModule: *opaque) -> bool32

Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference count reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer valid

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

Source

Parameters

NameTypeDescription
libModule*opaquea module handle from LoadLibraryA

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

GetCommandLineW

pub extern func GetCommandLineW() -> *uint16

Retrieves the command line the process was started with, as one string. The string belongs to the process for its whole life and must not be written or freed. It is the command line exactly as the parent passed it, quotes and all; CommandLineToArgvW splits it into arguments.

See also: https://learn.microsoft.com/windows/win32/api/processenv/nf-processenv-getcommandlinew

Source

Returns: the NUL-terminated UTF-16 command line, which is never null

GetModuleFileNameW

pub extern func GetModuleFileNameW(
    moduleHandle: *opaque,
    buffer: *var uint16,
    size: uint32
) -> uint32

Retrieves the full path of the file a module was loaded from; a null module is the process's executable. The path is NUL-terminated when it fits. When it does not, it is cut to size units, the last of them the terminator, and the call returns size.

See also: https://learn.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew

Source

Parameters

NameTypeDescription
moduleHandle*opaquethe module, or null for the executable
buffer*var uint16receives the path
sizeuint32how many units buffer holds

Returns: the path's length without its terminator, size when it was cut, or zero on failure with the reason in GetLastError

GetEnvironmentStringsW

pub extern func GetEnvironmentStringsW() -> *uint16

Copies the calling process's environment block. The block is a sequence of NUL-terminated name=value UTF-16 strings ended by an empty one. It is a copy owned by the caller, released with FreeEnvironmentStringsW; changes made to the environment after the call do not appear in it.

See also: https://learn.microsoft.com/windows/win32/api/processenv/nf-processenv-getenvironmentstringsw

Source

Returns: the environment block, or null on failure with the reason in GetLastError

GetCurrentProcessId

pub extern func GetCurrentProcessId() -> uint32

Retrieves the process identifier of the calling process.

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

Source

Returns: the calling process's identifier

GetActiveProcessorCount

pub extern func GetActiveProcessorCount(groupNumber: uint16) -> uint32

Retrieves the number of active processors in a processor group, or in all of them.

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

Source

Parameters

NameTypeDescription
groupNumberuint16the processor group, or ALL_PROCESSOR_GROUPS for the whole system

Returns: the number of active processors, or zero on failure

GetCurrentThreadId

pub extern func GetCurrentThreadId() -> uint32
pub extern func GetCurrentThreadId() -> uint32

Retrieves the thread identifier of the calling thread.

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

Source

Returns: the calling thread's identifier

GetProcAddress

pub extern func GetProcAddress(
    libModule: *opaque,
    procName: *char8
) -> *opaque

Retrieves the address of an exported symbol.

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

Source

Parameters

NameTypeDescription
libModule*opaquea module handle from LoadLibraryA
procName*char8the exported symbol's name

Returns: the symbol's address, or null

LoadLibraryA

pub extern func LoadLibraryA(libFileName: *char8) -> *opaque

Loads a DLL into the calling process.

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

Source

Parameters

NameTypeDescription
libFileName*char8the library's name or path

Returns: the module's handle, or null

CreateThread

pub extern func CreateThread(
    securityAttributes: *opaque,
    stackSize: uint64,
    startAddress: *opaque,
    parameter: *opaque,
    creationFlags: uint32,
    threadId: *uint32
) -> *opaque

Creates a thread to execute within the virtual address space of the calling process.

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

Source

Parameters

NameTypeDescription
securityAttributes*opaquea security descriptor, or null for the default
stackSizeuint64the initial stack size, or zero for the default
startAddress*opaquethe thread's entry point
parameter*opaquethe value handed to the entry point
creationFlagsuint32the thread creation flags
threadId*uint32receives the new thread's identifier, or null

Returns: the new thread's handle, or null

Sleep

pub extern func Sleep(milliseconds: uint32)

Suspends the execution of the current thread until the time-out interval elapses.

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

Source

Parameters

NameTypeDescription
millisecondsuint32a duration in milliseconds

SwitchToThread

pub extern func SwitchToThread() -> bool32

Causes the calling thread to yield execution to another thread that is ready to run.

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

Source

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

WaitForSingleObject

pub extern func WaitForSingleObject(
    handle: *opaque,
    milliseconds: uint32
) -> uint32

Waits until the specified object is in the signaled state or the time-out interval elapses.

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

Source

Parameters

NameTypeDescription
handle*opaquethe handle to act on
millisecondsuint32a duration in milliseconds

Returns: why the wait ended

CommandLineToArgvW

pub extern func CommandLineToArgvW(
    commandLine: *uint16,
    count: *var int32
) -> **uint16

Splits a command line into arguments the way the C runtime does. Backslashes, quotes and spaces follow the C runtime's rules, and the first argument, the program, is read up to the first space outside quotes. The result is one allocation holding the pointer array and every argument; it stays valid until passed to LocalFree.

See also: https://learn.microsoft.com/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw

Source

Parameters

NameTypeDescription
commandLine*uint16the NUL-terminated UTF-16 command line, usually from GetCommandLineW
count*var int32receives how many arguments the array holds

Returns: the array of NUL-terminated UTF-16 arguments, or null on failure with the reason in GetLastError