10 internal static class Kernel32
79 targetHandle.Dispose();
82 this.SetSafeWaitHandle(targetHandle);
102 internal unsafe fixed
byte LeadByte[12];
114 [DllImport(
"kernel32.dll", BestFitMapping =
true,
CharSet =
CharSet.Unicode, EntryPoint =
"FormatMessageW", ExactSpelling =
true,
SetLastError =
true)]
115 private unsafe
static extern int FormatMessage(
int dwFlags,
IntPtr lpSource, uint dwMessageId,
int dwLanguageId,
void* lpBuffer,
int nSize,
IntPtr arguments);
130 fixed (
char* lpBuffer = span)
154 return $
"Unknown error (0x{errorCode:x})";
160 while (num > 0 &&
buffer[num - 1] <=
' ')
164 return buffer.Slice(0, num).ToString();
185 [DllImport(
"kernel32.dll")]
186 [SuppressGCTransition]
189 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"CreateProcessW",
SetLastError =
true)]
195 [DllImport(
"kernel32.dll")]
202 internal static extern bool EnumProcesses(
int[] processIds,
int size, out
int needed);
212 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"K32GetModuleBaseNameW",
SetLastError =
true)]
215 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"K32GetModuleFileNameExW",
SetLastError =
true)]
230 [DllImport(
"kernel32.dll")]
272 [DllImport(
"kernel32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"GetComputerNameW", ExactSpelling =
true)]
278 uint nSize = (uint)span.
Length;
283 return span.
Slice(0, (
int)nSize).ToString();
286 [DllImport(
"kernel32.dll")]
289 [DllImport(
"kernel32.dll")]
295 [DllImport(
"kernel32.dll")]
296 internal unsafe
static extern int MultiByteToWideChar(uint CodePage, uint dwFlags,
byte* lpMultiByteStr,
int cbMultiByte,
char* lpWideCharStr,
int cchWideChar);
298 [DllImport(
"kernel32.dll")]
299 internal unsafe
static extern int WideCharToMultiByte(uint CodePage, uint dwFlags,
char* lpWideCharStr,
int cchWideChar,
byte* lpMultiByteStr,
int cbMultiByte,
IntPtr lpDefaultChar,
IntPtr lpUsedDefaultChar);
310 for (
int i = 0; i < 10 && leadByteRanges[i] != 0; i += 2)
312 leadByteRanges[i] = cPINFOEXW.LeadByte[i];
313 leadByteRanges[i + 1] = cPINFOEXW.LeadByte[i + 1];
415 [DllImport(
"ntdll.dll", ExactSpelling =
true)]
418 [DllImport(
"ntdll.dll", ExactSpelling =
true)]
419 internal unsafe static extern uint
NtQuerySystemInformation(
int SystemInformationClass,
void* SystemInformation, uint SystemInformationLength, uint* ReturnLength);
422 internal static class User32
424 [DllImport(
"user32.dll")]
425 public unsafe
static extern bool EnumWindows(delegate* unmanaged<IntPtr, IntPtr, BOOL> callback,
IntPtr extraData);
427 [DllImport(
"user32.dll")]
430 [DllImport(
"user32.dll", EntryPoint =
"GetWindowLongW")]
433 [DllImport(
"user32.dll", ExactSpelling =
true, SetLastError =
true)]
436 [DllImport(
"user32.dll",
CharSet =
CharSet.Unicode, ExactSpelling =
true, SetLastError =
true)]
439 [DllImport(
"user32.dll", ExactSpelling =
true)]
442 [DllImport(
"user32.dll",
CharSet =
CharSet.Unicode, ExactSpelling =
true)]
445 [DllImport(
"user32.dll")]
448 [DllImport(
"user32.dll", EntryPoint =
"SendMessageTimeoutW")]
451 [DllImport(
"user32.dll")]
536 if (instance.NameLength != 0)
538 return MemoryMarshal.Cast<byte,
char>(data.
Slice(instance.NameOffset, instance.NameLength - 2));
627 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
630 [DllImport(
"advapi32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"LookupPrivilegeValueW", SetLastError =
true)]
633 [DllImport(
"advapi32.dll", SetLastError =
true)]
636 [DllImport(
"advapi32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, ExactSpelling =
true, SetLastError =
true)]
637 internal unsafe
static extern bool CreateProcessWithLogonW(
string userName,
string domain,
IntPtr password,
LogonFlags logonFlags,
string appName,
char* cmdLine,
int creationFlags,
IntPtr environmentBlock,
string lpCurrentDirectory, ref
Kernel32.
STARTUPINFO lpStartupInfo, ref
Kernel32.
PROCESS_INFORMATION lpProcessInformation);
691 [DllImport(
"shell32.dll",
CharSet =
CharSet.Unicode, ExactSpelling =
true, SetLastError =
true)]
static bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, out SafeTokenHandle TokenHandle)
static unsafe bool CreateProcessWithLogonW(string userName, string domain, IntPtr password, LogonFlags logonFlags, string appName, char *cmdLine, int creationFlags, IntPtr environmentBlock, string lpCurrentDirectory, ref Kernel32.STARTUPINFO lpStartupInfo, ref Kernel32.PROCESS_INFORMATION lpProcessInformation)
static unsafe bool AdjustTokenPrivileges(SafeTokenHandle TokenHandle, bool DisableAllPrivileges, TOKEN_PRIVILEGE *NewState, uint BufferLength, TOKEN_PRIVILEGE *PreviousState, uint *ReturnLength)
@ LOGON_NETCREDENTIALS_ONLY
static bool LookupPrivilegeValue([MarshalAs(UnmanagedType.LPTStr)] string lpSystemName, [MarshalAs(UnmanagedType.LPTStr)] string lpName, out LUID lpLuid)
ProcessWaitHandle(SafeProcessHandle processHandle)
static uint GetConsoleCP()
static bool GetThreadTimes(SafeThreadHandle handle, out long creation, out long exit, out long kernel, out long user)
static uint GetConsoleOutputCP()
static bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeHandle hSourceHandle, IntPtr hTargetProcess, out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
static IntPtr GetStdHandle(int nStdHandle)
static int GetModuleBaseName(SafeProcessHandle processHandle, IntPtr moduleHandle, [Out] char[] baseName, int size)
static bool IsWow64Process(IntPtr hProcess, out bool Wow64Process)
static unsafe bool GetModuleInformation(SafeProcessHandle processHandle, IntPtr moduleHandle, out NtModuleInfo ntModuleInfo)
static bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
static bool GetProcessTimes(SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user)
static void SetLastError(int errorCode)
static bool SetProcessPriorityBoost(SafeProcessHandle handle, bool disabled)
static bool CloseHandle(IntPtr handle)
static unsafe int WideCharToMultiByte(uint CodePage, uint dwFlags, char *lpWideCharStr, int cchWideChar, byte *lpMultiByteStr, int cbMultiByte, IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar)
static int GetComputerName(ref char lpBuffer, ref uint nSize)
static unsafe int MultiByteToWideChar(uint CodePage, uint dwFlags, byte *lpMultiByteStr, int cbMultiByte, char *lpWideCharStr, int cchWideChar)
static bool IsWow64Process(SafeProcessHandle hProcess, out bool Wow64Process)
static bool SetPriorityClass(SafeProcessHandle handle, int priorityClass)
static bool GetProcessWorkingSetSizeEx(SafeProcessHandle handle, out IntPtr min, out IntPtr max, out int flags)
static bool GetProcessAffinityMask(SafeProcessHandle handle, out IntPtr processMask, out IntPtr systemMask)
static bool GetThreadPriorityBoost(SafeThreadHandle handle, out bool disabled)
static bool TerminateProcess(SafeProcessHandle processHandle, int exitCode)
static string GetMessage(int errorCode)
static bool EnumProcessModules(SafeProcessHandle handle, IntPtr[] modules, int size, out int needed)
static int GetProcessId(SafeProcessHandle nativeHandle)
static int GetModuleFileNameEx(SafeProcessHandle processHandle, IntPtr moduleHandle, [Out] char[] baseName, int size)
static bool SetThreadPriorityBoost(SafeThreadHandle handle, bool disabled)
static IntPtr SetThreadAffinityMask(SafeThreadHandle handle, IntPtr mask)
static IntPtr GetCurrentProcess()
static unsafe int GetLeadByteRanges(int codePage, byte[] leadByteRanges)
static SafeProcessHandle OpenProcess(int access, bool inherit, int processId)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static bool SetProcessAffinityMask(SafeProcessHandle handle, IntPtr mask)
static bool SetThreadPriority(SafeThreadHandle handle, int priority)
static int GetPriorityClass(SafeProcessHandle handle)
static unsafe BOOL GetCPInfoExW(uint CodePage, uint dwFlags, CPINFOEXW *lpCPInfoEx)
static SafeThreadHandle OpenThread(int access, bool inherit, int threadId)
static bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeHandle hSourceHandle, IntPtr hTargetProcess, out SafeFileHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
static string GetComputerName()
static bool EnumProcesses(int[] processIds, int size, out int needed)
static bool GetProcessPriorityBoost(SafeProcessHandle handle, out bool disabled)
static int GetThreadPriority(SafeThreadHandle handle)
static int SetThreadIdealProcessor(SafeThreadHandle handle, int processor)
static bool SetProcessWorkingSetSizeEx(SafeProcessHandle handle, IntPtr min, IntPtr max, int flags)
static unsafe bool CreateProcess(string lpApplicationName, char *lpCommandLine, ref SECURITY_ATTRIBUTES procSecAttrs, ref SECURITY_ATTRIBUTES threadSecAttrs, bool bInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation)
static bool GetExitCodeProcess(SafeProcessHandle processHandle, out int exitCode)
static bool GetModuleInformation(SafeProcessHandle processHandle, IntPtr moduleHandle, out NtModuleInfo ntModuleInfo, int size)
static string GetAndTrimString(Span< char > buffer)
static unsafe uint NtQueryInformationProcess(SafeProcessHandle ProcessHandle, int ProcessInformationClass, void *ProcessInformation, uint ProcessInformationLength, out uint ReturnLength)
static unsafe(uint status, IntPtr handle) CreateFile(ReadOnlySpan< char > path
static unsafe uint NtQuerySystemInformation(int SystemInformationClass, void *SystemInformation, uint SystemInformationLength, uint *ReturnLength)
static unsafe bool ShellExecuteExW(SHELLEXECUTEINFO *pExecInfo)
static int GetWindowLong(IntPtr hWnd, int uCmd)
static IntPtr SendMessageTimeout(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, int flags, int timeout, out IntPtr pdwResult)
static BOOL IsWindowVisible(IntPtr hWnd)
static int PostMessageW(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
static unsafe int GetWindowThreadProcessId(IntPtr handle, int *processId)
static int GetWindowTextLengthW(IntPtr hWnd)
static unsafe bool EnumWindows(delegate *unmanaged< IntPtr, IntPtr, BOOL > callback, IntPtr extraData)
static IntPtr GetWindow(IntPtr hWnd, int uCmd)
static int WaitForInputIdle(SafeProcessHandle handle, int milliseconds)
static unsafe int GetWindowTextW(IntPtr hWnd, char *lpString, int nMaxCount)
static CultureInfo CurrentCulture
static int GetHRForLastWin32Error()
static void FreeHGlobal(IntPtr hglobal)
static int GetLastWin32Error()
static void ThrowExceptionForHR(int errorCode)
int CounterHelpTitleIndex
int CounterNameTitleIndex
int ParentObjectTitleIndex
static ReadOnlySpan< char > GetName(in PERF_INSTANCE_DEFINITION instance, ReadOnlySpan< byte > data)
override string ToString()
LUID_AND_ATTRIBUTES Privileges
unsafe fixed char CodePageName[260]
unsafe fixed byte LeadByte[12]
unsafe fixed byte DefaultChar[2]
IntPtr lpSecurityDescriptor
unsafe char * lpDirectory
unsafe char * lpParameters
static readonly IntPtr Zero
ReadOnlySpan< T > Slice(int start)
Span< T > Slice(int start)