9 internal static class Kernel32
11 internal struct SECURITY_ATTRIBUTES
23 [DllImport(
"kernel32.dll", BestFitMapping =
true,
CharSet =
CharSet.Unicode, EntryPoint =
"FormatMessageW", ExactSpelling =
true,
SetLastError =
true)]
24 private unsafe
static extern int FormatMessage(
int dwFlags,
IntPtr lpSource, uint dwMessageId,
int dwLanguageId,
void* lpBuffer,
int nSize,
IntPtr arguments);
39 fixed (
char* lpBuffer = span)
63 return $
"Unknown error (0x{errorCode:x})";
69 while (num > 0 &&
buffer[num - 1] <=
' ')
73 return buffer.Slice(0, num).ToString();
76 [DllImport(
"kernel32.dll")]
96 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"WaitNamedPipeW",
SetLastError =
true)]
101 internal unsafe
static extern bool GetNamedPipeHandleStateW(
SafePipeHandle hNamedPipe, uint* lpState, uint* lpCurInstances, uint* lpMaxCollectionCount, uint* lpCollectDataTimeout,
char* lpUserName, uint nMaxUserNameSize);
104 internal unsafe
static extern bool GetNamedPipeInfo(
SafePipeHandle hNamedPipe, uint* lpFlags, uint* lpOutBufferSize, uint* lpInBufferSize, uint* lpMaxInstances);
133 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"CreateNamedPipeW",
SetLastError =
true)]
136 [DllImport(
"kernel32.dll", BestFitMapping =
false,
CharSet =
CharSet.Unicode, EntryPoint =
"CreateFileW",
SetLastError =
true)]
139 [DllImport(
"kernel32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"LoadLibraryExW", ExactSpelling =
true,
SetLastError =
true)]
149 internal static class Advapi32
151 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, ExactSpelling =
true, SetLastError =
true)]
154 [DllImport(
"advapi32.dll", SetLastError =
true)]
static bool RevertToSelf()
static bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe)
static bool WaitNamedPipe(string name, int timeout)
static unsafe int ReadFile(SafeHandle handle, byte *bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero)
static unsafe int WriteFile(SafeHandle handle, byte *bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped *lpOverlapped)
static bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeHandle hSourceHandle, IntPtr hTargetProcessHandle, out SafePipeHandle lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
static void SetLastError(int errorCode)
static unsafe bool CancelIoEx(SafeHandle handle, NativeOverlapped *lpOverlapped)
static unsafe bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, uint *lpFlags, uint *lpOutBufferSize, uint *lpInBufferSize, uint *lpMaxInstances)
static SafePipeHandle CreateNamedPipe(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeout, ref SECURITY_ATTRIBUTES securityAttributes)
static bool DisconnectNamedPipe(SafePipeHandle hNamedPipe)
static unsafe bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped *overlapped)
static bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped)
static bool CloseHandle(IntPtr handle)
static IntPtr LoadLibraryEx(string libFilename, IntPtr reserved, int flags)
static unsafe bool SetNamedPipeHandleState(SafePipeHandle hNamedPipe, int *lpMode, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout)
static string GetMessage(int errorCode)
static unsafe int ReadFile(SafeHandle handle, byte *bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped *overlapped)
static unsafe bool GetNamedPipeHandleStateW(SafePipeHandle hNamedPipe, uint *lpState, uint *lpCurInstances, uint *lpMaxCollectionCount, uint *lpCollectDataTimeout, char *lpUserName, uint nMaxUserNameSize)
static unsafe int WriteFile(SafeHandle handle, byte *bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero)
static bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize)
static IntPtr GetCurrentProcess()
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static int GetFileType(SafeHandle hFile)
static SafePipeHandle CreateNamedPipeClient(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, ref SECURITY_ATTRIBUTES secAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
static bool FlushFileBuffers(SafeHandle hHandle)
static string GetAndTrimString(Span< char > buffer)
static void FreeHGlobal(IntPtr hglobal)
static int GetLastWin32Error()
IntPtr lpSecurityDescriptor
static readonly IntPtr Zero
Span< T > Slice(int start)