Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
2using System.IO;
6
7internal static class Interop
8{
9 internal static class Kernel32
10 {
11 internal struct SECURITY_ATTRIBUTES
12 {
13 internal uint nLength;
14
16
17 internal BOOL bInheritHandle;
18 }
19
20 [DllImport("kernel32.dll", SetLastError = true)]
21 internal static extern bool CloseHandle(IntPtr handle);
22
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);
25
26 internal static string GetMessage(int errorCode)
27 {
28 return GetMessage(errorCode, IntPtr.Zero);
29 }
30
31 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
32 {
33 int num = 12800;
34 if (moduleHandle != IntPtr.Zero)
35 {
36 num |= 0x800;
37 }
38 Span<char> span = stackalloc char[256];
39 fixed (char* lpBuffer = span)
40 {
41 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
42 if (num2 > 0)
43 {
44 return GetAndTrimString(span.Slice(0, num2));
45 }
46 }
47 if (Marshal.GetLastWin32Error() == 122)
48 {
49 IntPtr intPtr = default(IntPtr);
50 try
51 {
52 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
53 if (num3 > 0)
54 {
55 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
56 }
57 }
58 finally
59 {
60 Marshal.FreeHGlobal(intPtr);
61 }
62 }
63 return $"Unknown error (0x{errorCode:x})";
64 }
65
66 private static string GetAndTrimString(Span<char> buffer)
67 {
68 int num = buffer.Length;
69 while (num > 0 && buffer[num - 1] <= ' ')
70 {
71 num--;
72 }
73 return buffer.Slice(0, num).ToString();
74 }
75
76 [DllImport("kernel32.dll")]
77 internal static extern IntPtr GetCurrentProcess();
78
79 [DllImport("kernel32.dll", SetLastError = true)]
80 internal static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeHandle hSourceHandle, IntPtr hTargetProcessHandle, out SafePipeHandle lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions);
81
82 [DllImport("kernel32.dll", SetLastError = true)]
83 internal static extern int GetFileType(SafeHandle hFile);
84
85 [DllImport("kernel32.dll", SetLastError = true)]
86 internal static extern bool CreatePipe(out SafePipeHandle hReadPipe, out SafePipeHandle hWritePipe, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
87
88 [DllImport("kernel32.dll", SetLastError = true)]
89 [return: MarshalAs(UnmanagedType.Bool)]
90 internal unsafe static extern bool ConnectNamedPipe(SafePipeHandle handle, NativeOverlapped* overlapped);
91
92 [DllImport("kernel32.dll", SetLastError = true)]
93 [return: MarshalAs(UnmanagedType.Bool)]
94 internal static extern bool ConnectNamedPipe(SafePipeHandle handle, IntPtr overlapped);
95
96 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "WaitNamedPipeW", SetLastError = true)]
97 [return: MarshalAs(UnmanagedType.Bool)]
98 internal static extern bool WaitNamedPipe(string name, int timeout);
99
100 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
101 internal unsafe static extern bool GetNamedPipeHandleStateW(SafePipeHandle hNamedPipe, uint* lpState, uint* lpCurInstances, uint* lpMaxCollectionCount, uint* lpCollectDataTimeout, char* lpUserName, uint nMaxUserNameSize);
102
103 [DllImport("kernel32.dll", SetLastError = true)]
104 internal unsafe static extern bool GetNamedPipeInfo(SafePipeHandle hNamedPipe, uint* lpFlags, uint* lpOutBufferSize, uint* lpInBufferSize, uint* lpMaxInstances);
105
106 [DllImport("kernel32.dll", SetLastError = true)]
107 [return: MarshalAs(UnmanagedType.Bool)]
108 internal unsafe static extern bool SetNamedPipeHandleState(SafePipeHandle hNamedPipe, int* lpMode, IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout);
109
110 [DllImport("kernel32.dll", SetLastError = true)]
111 internal unsafe static extern bool CancelIoEx(SafeHandle handle, NativeOverlapped* lpOverlapped);
112
113 [DllImport("kernel32.dll", SetLastError = true)]
114 [return: MarshalAs(UnmanagedType.Bool)]
115 internal static extern bool FlushFileBuffers(SafeHandle hHandle);
116
117 [DllImport("kernel32.dll", SetLastError = true)]
118 internal unsafe static extern int ReadFile(SafeHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);
119
120 [DllImport("kernel32.dll", SetLastError = true)]
121 internal unsafe static extern int ReadFile(SafeHandle handle, byte* bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped* overlapped);
122
123 [DllImport("kernel32.dll", SetLastError = true)]
124 internal unsafe static extern int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
125
126 [DllImport("kernel32.dll", SetLastError = true)]
127 internal unsafe static extern int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
128
129 [DllImport("kernel32.dll", SetLastError = true)]
130 [return: MarshalAs(UnmanagedType.Bool)]
131 internal static extern bool DisconnectNamedPipe(SafePipeHandle hNamedPipe);
132
133 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "CreateNamedPipeW", SetLastError = true)]
134 internal static extern SafePipeHandle CreateNamedPipe(string pipeName, int openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeout, ref SECURITY_ATTRIBUTES securityAttributes);
135
136 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "CreateFileW", SetLastError = true)]
137 internal static extern SafePipeHandle CreateNamedPipeClient(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, ref SECURITY_ATTRIBUTES secAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
138
139 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryExW", ExactSpelling = true, SetLastError = true)]
140 internal static extern IntPtr LoadLibraryEx(string libFilename, IntPtr reserved, int flags);
141 }
142
143 internal enum BOOL
144 {
145 FALSE,
146 TRUE
147 }
148
149 internal static class Advapi32
150 {
151 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
152 internal static extern bool RevertToSelf();
153
154 [DllImport("advapi32.dll", SetLastError = true)]
155 [return: MarshalAs(UnmanagedType.Bool)]
156 internal static extern bool ImpersonateNamedPipeClient(SafePipeHandle hNamedPipe);
157 }
158}
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)
Definition Interop.cs:31
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)
Definition Interop.cs:26
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)
Definition Interop.cs:153
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70