c# 很多代码是用c# 写的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // Generated by Reflector from C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll
2 namespace Microsoft.Win32
3 {
4 using Microsoft.Win32.SafeHandles;
5 using System;
6 using System.IO;
7 using System.Runtime.CompilerServices;
8 using System.Runtime.ConstrainedExecution;
9 using System.Runtime.InteropServices;
10 using System.Security;
11 using System.Security.Principal;
12 using System.Text;
13 using System.Threading;
14
15 [SuppressUnmanagedCodeSecurity]
16 internal static class Win32Native
17 {
18 internal const string ADVAPI32 = "advapi32.dll";
19 internal const uint ANONYMOUS_LOGON_LUID = 0x3e6;
20 internal const string CRYPT32 = "crypt32.dll";
21 internal const uint CRYPTPROTECTMEMORY_BLOCK_SIZE = 0x10;
22 internal const uint CRYPTPROTECTMEMORY_CROSS_PROCESS = 1;
23 internal const uint CRYPTPROTECTMEMORY_SAME_LOGON = 2;
24 internal const uint CRYPTPROTECTMEMORY_SAME_PROCESS = 0;
25 internal const int CSIDL_APPDATA = 0x1a;
26 internal const int CSIDL_COMMON_APPDATA = 0x23;
27 internal const int CSIDL_COOKIES = 0x21;
28 internal const int CSIDL_DESKTOP = 0;
29 internal const int CSIDL_DESKTOPDIRECTORY = 0x10;
30 internal const int CSIDL_DRIVES = 0x11;
31 internal const int CSIDL_FAVORITES = 6;
32 internal const int CSIDL_HISTORY = 0x22;
33 internal const int CSIDL_INTERNET_CACHE = 0x20;
34 internal const int CSIDL_LOCAL_APPDATA = 0x1c;
35 internal const int CSIDL_MYMUSIC = 13;
36 internal const int CSIDL_MYPICTURES = 0x27;
37 internal const int CSIDL_PERSONAL = 5;
38 internal const int CSIDL_PROGRAM_FILES = 0x26;
39 internal const int CSIDL_PROGRAM_FILES_COMMON = 0x2b;
40 internal const int CSIDL_PROGRAMS = 2;
41 internal const int CSIDL_RECENT = 8;
42 internal const int CSIDL_SENDTO = 9;
43 internal const int CSIDL_STARTMENU = 11;
44 internal const int CSIDL_STARTUP = 7;
45 internal const int CSIDL_SYSTEM = 0x25;
46 internal const int CSIDL_TEMPLATES = 0x15;
47 internal const int CTRL_BREAK_EVENT = 1;
48 internal const int CTRL_C_EVENT = 0;
49 internal const int CTRL_CLOSE_EVENT = 2;
50 internal const int CTRL_LOGOFF_EVENT = 5;
51 internal const int CTRL_SHUTDOWN_EVENT = 6;
52 internal const int DOMAIN_USER_RID_GUEST = 0x1f5;
53 internal const uint DUPLICATE_CLOSE_SOURCE = 1;
54 internal const uint DUPLICATE_SAME_ACCESS = 2;
55 internal const uint DUPLICATE_SAME_ATTRIBUTES = 4;
56 internal const int ENABLE_ECHO_INPUT = 4;
57 internal const int ENABLE_LINE_INPUT = 2;
58 internal const int ENABLE_PROCESSED_INPUT = 1;
59 internal const int ERROR_ACCESS_DENIED = 5;
60 internal const int ERROR_ALREADY_EXISTS = 0xb7;
61 internal const int ERROR_BAD_IMPERSONATION_LEVEL = 0x542;
62 internal const int ERROR_BAD_LENGTH = 0x18;
63 internal const int ERROR_BAD_PATHNAME = 0xa1;
64 internal const int ERROR_CALL_NOT_IMPLEMENTED = 120;
65 internal const int ERROR_CANT_OPEN_ANONYMOUS = 0x543;
66 internal const int ERROR_DLL_INIT_FAILED = 0x45a;
67 internal const int ERROR_ENVVAR_NOT_FOUND = 0xcb;
68 internal const int ERROR_FILE_EXISTS = 80;
69 internal const int ERROR_FILE_NOT_FOUND = 2;
70 internal const int ERROR_FILENAME_EXCED_RANGE = 0xce;
71 internal const int ERROR_INSUFFICIENT_BUFFER = 0x7a;
72 internal const int ERROR_INVALID_ACL = 0x538;
73 internal const int ERROR_INVALID_DATA = 13;
74 internal const int ERROR_INVALID_DRIVE = 15;
75 internal const int ERROR_INVALID_FUNCTION = 1;
76 internal const int ERROR_INVALID_HANDLE = 6;
77 internal const int ERROR_INVALID_NAME = 0x7b;
78 internal const int ERROR_INVALID_OWNER = 0x51b;
79 internal const int ERROR_INVALID_PARAMETER = 0x57;
80 internal const int ERROR_INVALID_PRIMARY_GROUP = 0x51c;
81 internal const int ERROR_INVALID_SECURITY_DESCR = 0x53a;
82 internal const int ERROR_INVALID_SID = 0x539;
83 internal const int ERROR_MORE_DATA = 0xea;
84 internal const int ERROR_NO_DATA = 0xe8;
85 internal const int ERROR_NO_MORE_FILES = 0x12;
86 internal const int ERROR_NO_SECURITY_ON_OBJECT = 0x546;
87 internal const int ERROR_NO_SUCH_PRIVILEGE = 0x521;
88 internal const int ERROR_NO_TOKEN = 0x3f0;
89 internal const int ERROR_NON_ACCOUNT_SID = 0x4e9;
90 internal const int ERROR_NONE_MAPPED = 0x534;
91 internal const int ERROR_NOT_ALL_ASSIGNED = 0x514;
92 internal const int ERROR_NOT_ENOUGH_MEMORY = 8;
93 internal const int ERROR_NOT_READY = 0x15;
94 internal const int ERROR_NOT_SUPPORTED = 50;
95 internal const int ERROR_OPERATION_ABORTED = 0x3e3;
96 internal const int ERROR_PATH_NOT_FOUND = 3;
97 internal const int ERROR_PIPE_NOT_CONNECTED = 0xe9;
98 internal const int ERROR_PRIVILEGE_NOT_HELD = 0x522;
99 internal const int ERROR_SHARING_VIOLATION = 0x20;
100 internal const int ERROR_SUCCESS = 0;
101 internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6fd;
102 internal const int ERROR_UNKNOWN_REVISION = 0x519;
103 internal const int EVENT_MODIFY_STATE = 2;
104 internal const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
105 internal const int FILE_ATTRIBUTE_READONLY = 1;
106 internal const int FILE_ATTRIBUTE_REPARSE_POINT = 0x400;
107 internal const int FILE_TYPE_CHAR = 2;
108 internal const int FILE_TYPE_DISK = 1;
109 internal const int FILE_TYPE_PIPE = 3;
110 internal const int FIND_ENDSWITH = 0x200000;
111 internal const int FIND_FROMEND = 0x800000;
112 internal const int FIND_FROMSTART = 0x400000;
113 internal const int FIND_STARTSWITH = 0x100000;
114 private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000;
115 private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
116 private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;
117 internal const int HWND_BROADCAST = 0xffff;
118 internal const int INVALID_FILE_SIZE = -1;
119 internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
120 internal const int IO_REPARSE_TAG_MOUNT_POINT = -1610612733;
121 internal const string KERNEL32 = "kernel32.dll";
122 internal const int KEY_CREATE_LINK = 0x20;
123 internal const int KEY_CREATE_SUB_KEY = 4;
124 internal const int KEY_ENUMERATE_SUB_KEYS = 8;
125 internal const short KEY_EVENT = 1;
126 internal const int KEY_NOTIFY = 0x10;
127 internal const int KEY_QUERY_VALUE = 1;
128 internal const int KEY_READ = 0x20019;
129 internal const int KEY_SET_VALUE = 2;
130 internal const int KEY_WRITE = 0x20006;
131 internal const int LCID_SUPPORTED = 2;
132 internal const int LCMAP_SORTKEY = 0x400;
133 internal const int LMEM_FIXED = 0;
134 internal const int LMEM_ZEROINIT = 0x40;
135 internal const int LPTR = 0x40;
136 internal const string LSTRCPY = "lstrcpy";
137 internal const string LSTRCPYN = "lstrcpyn";
138 internal const string LSTRLEN = "lstrlen";
139 internal const string LSTRLENA = "lstrlenA";
140 internal const string LSTRLENW = "lstrlenW";
141 internal const int MEM_COMMIT = 0x1000;
142 internal const int MEM_FREE = 0x10000;
143 internal const int MEM_RELEASE = 0x8000;
144 internal const int MEM_RESERVE = 0x2000;
145 internal const string MICROSOFT_KERBEROS_NAME = "Kerberos";
146 internal const string MOVEMEMORY = "RtlMoveMemory";
147 internal const string MSCORWKS = "mscorwks.dll";
148 internal const int MUTEX_ALL_ACCESS = 0x1f0001;
149 internal const int MUTEX_MODIFY_STATE = 1;
150 internal const int NameSamCompatible = 2;
151 internal static readonly IntPtr NULL = IntPtr.Zero;
152 internal const string OLE32 = "ole32.dll";
153 internal const string OLEAUT32 = "oleaut32.dll";
154 internal const int PAGE_READWRITE = 4;
155 internal const int READ_CONTROL = 0x20000;
156 internal const int REG_BINARY = 3;
157 internal const int REG_DWORD = 4;
158 internal const int REG_DWORD_BIG_ENDIAN = 5;
159 internal const int REG_DWORD_LITTLE_ENDIAN = 4;
160 internal const int REG_EXPAND_SZ = 2;
161 internal const int REG_FULL_RESOURCE_DESCRIPTOR = 9;
162 internal const int REG_LINK = 6;
163 internal const int REG_MULTI_SZ = 7;
164 internal const int REG_NONE = 0;
165 internal const int REG_QWORD = 11;
166 internal const int REG_RESOURCE_LIST = 8;
167 internal const int REG_RESOURCE_REQUIREMENTS_LIST = 10;
168 internal const int REG_SZ = 1;
169 internal const int REPLACEFILE_IGNORE_MERGE_ERRORS = 2;
170 internal const int REPLACEFILE_WRITE_THROUGH = 1;
171 internal const uint SE_GROUP_ENABLED = 4;
172 internal const uint SE_GROUP_ENABLED_BY_DEFAULT = 2;
173 internal const uint SE_GROUP_LOGON_ID = 0xc0000000;
174 internal const uint SE_GROUP_MANDATORY = 1;
175 internal const uint SE_GROUP_OWNER = 8;
176 internal const uint SE_GROUP_RESOURCE = 0x20000000;
177 internal const uint SE_GROUP_USE_FOR_DENY_ONLY = 0x10;
178 internal const uint SE_PRIVILEGE_DISABLED = 0;
179 internal const uint SE_PRIVILEGE_ENABLED = 2;
180 internal const uint SE_PRIVILEGE_ENABLED_BY_DEFAULT = 1;
181 internal const uint SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
182 internal const string SECUR32 = "secur32.dll";
183 internal const int SECURITY_ANONYMOUS = 0;
184 internal const int SECURITY_ANONYMOUS_LOGON_RID = 7;
185 internal const int SECURITY_AUTHENTICATED_USER_RID = 11;
186 internal const int SECURITY_BUILTIN_DOMAIN_RID = 0x20;
187 internal const int SECURITY_LOCAL_SYSTEM_RID = 0x12;
188 internal const int SECURITY_SQOS_PRESENT = 0x100000;
189 internal const int SEM_FAILCRITICALERRORS = 1;
190 internal const int SEMAPHORE_MODIFY_STATE = 2;
191 internal const string SHFOLDER = "shfolder.dll";
192 internal const int SHGFP_TYPE_CURRENT = 0;
193 internal const string SHIM = "mscoree.dll";
194 internal const int STANDARD_RIGHTS_READ = 0x20000;
195 internal const int STANDARD_RIGHTS_WRITE = 0x20000;
196 internal const uint STATUS_ACCESS_DENIED = 0xc0000022;
197 internal const int STATUS_ACCOUNT_RESTRICTION = -1073741714;
198 internal const uint STATUS_INSUFFICIENT_RESOURCES = 0xc000009a;
199 internal const uint STATUS_NO_MEMORY = 0xc0000017;
200 internal const uint STATUS_NONE_MAPPED = 0xc0000073;
201 internal const uint STATUS_SOME_NOT_MAPPED = 0x107;
202 internal const int STD_ERROR_HANDLE = -12;
203 internal const int STD_INPUT_HANDLE = -10;
204 internal const int STD_OUTPUT_HANDLE = -11;
205 internal const int SYNCHRONIZE = 0x100000;
206 internal const int UOI_FLAGS = 1;
207 internal const string USER32 = "user32.dll";
208 internal const int VER_PLATFORM_WIN32_NT = 2;
209 internal const int VER_PLATFORM_WIN32_WINDOWS = 1;
210 internal const int VER_PLATFORM_WIN32s = 0;
211 internal const int VER_PLATFORM_WINCE = 3;
212 internal const int WM_SETTINGCHANGE = 0x1a;
213 internal const int WSF_VISIBLE = 1;
214
215 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
216 internal static extern bool AdjustTokenPrivileges([In] SafeTokenHandle TokenHandle, [In] bool DisableAllPrivileges, [In] ref TOKEN_PRIVILEGE NewState, [In] uint BufferLength, [In, Out] ref TOKEN_PRIVILEGE PreviousState, [In, Out] ref uint ReturnLength);
217 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
218 internal static extern bool AllocateLocallyUniqueId([In, Out] ref LUID Luid);
219 [DllImport("kernel32.dll", SetLastError=true)]
220 internal static extern bool Beep(int frequency, int duration);
221 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
222 internal static extern bool CheckTokenMembership([In] SafeTokenHandle TokenHandle, [In] byte[] SidToCheck, [In, Out] ref bool IsMember);
223 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
224 internal static extern bool CloseHandle(IntPtr handle);
225 [DllImport("advapi32.dll", EntryPoint="ConvertSecurityDescriptorToStringSecurityDescriptorW", CharSet=CharSet.Unicode, SetLastError=true)]
226 internal static extern int ConvertSdToStringSd(byte[] securityDescriptor, uint requestedRevision, uint securityInformation, out IntPtr resultString, ref uint resultStringLength);
227 [DllImport("advapi32.dll", EntryPoint="ConvertStringSecurityDescriptorToSecurityDescriptorW", CharSet=CharSet.Unicode, SetLastError=true)]
228 internal static extern int ConvertStringSdToSd(string stringSd, uint stringSdRevision, out IntPtr resultSd, ref uint resultSdLength);
229 [DllImport("advapi32.dll", EntryPoint="ConvertStringSidToSidW", CharSet=CharSet.Unicode, SetLastError=true)]
230 internal static extern int ConvertStringSidToSid(string stringSid, out IntPtr ByteArray);
231 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
232 internal static extern bool CopyFile(string src, string dst, bool failIfExists);
233 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Ansi)]
234 internal static extern void CopyMemoryAnsi(IntPtr pdst, string psrc, IntPtr sizetcb);
235 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Ansi)]
236 internal static extern void CopyMemoryAnsi(StringBuilder pdst, IntPtr psrc, IntPtr sizetcb);
237 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Unicode)]
238 internal static extern void CopyMemoryUni(IntPtr pdst, string psrc, IntPtr sizetcb);
239 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Unicode)]
240 internal static extern void CopyMemoryUni(StringBuilder pdst, IntPtr psrc, IntPtr sizetcb);
241 [DllImport("ole32.dll")]
242 internal static extern IntPtr CoTaskMemAlloc(int cb);
243 [DllImport("ole32.dll")]
244 internal static extern void CoTaskMemFree(IntPtr ptr);
245 [DllImport("ole32.dll")]
246 internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, int cb);
247 [DllImport("mscorwks.dll", CharSet=CharSet.Auto)]
248 internal static extern int CreateAssemblyEnum(out IAssemblyEnum ppEnum, IApplicationContext pAppCtx, IAssemblyName pName, uint dwFlags, IntPtr pvReserved);
249 [DllImport("mscorwks.dll", CharSet=CharSet.Unicode)]
250 internal static extern int CreateAssemblyNameObject(out IAssemblyName ppEnum, string szAssemblyName, uint dwFlags, IntPtr pvReserved);
251 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
252 internal static extern bool CreateDirectory(string path, SECURITY_ATTRIBUTES lpSecurityAttributes);
253 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
254 internal static extern SafeWaitHandle CreateEvent(SECURITY_ATTRIBUTES lpSecurityAttributes, bool isManualReset, bool initialState, string name);
255 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
256 private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
257 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
258 internal static extern SafeFileMappingHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, uint fProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
259 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
260 internal static extern SafeWaitHandle CreateMutex(SECURITY_ATTRIBUTES lpSecurityAttributes, bool initialOwner, string name);
261 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
262 internal static extern int CreateWellKnownSid(int sidType, byte[] domainSid, [Out] byte[] resultSid, ref uint resultSidLength);
263 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
264 internal static extern bool DecryptFile(string path, int reservedMustBeZero);
265 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
266 internal static extern bool DeleteFile(string path);
267 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
268 internal static extern bool DeleteVolumeMountPoint(string mountPoint);
269 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
270 internal static extern bool DuplicateHandle([In] IntPtr hSourceProcessHandle, [In] SafeTokenHandle hSourceHandle, [In] IntPtr hTargetProcessHandle, [In, Out] ref SafeTokenHandle lpTargetHandle, [In] uint dwDesiredAccess, [In] bool bInheritHandle, [In] uint dwOptions);
271 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
272 internal static extern bool DuplicateHandle([In] IntPtr hSourceProcessHandle, [In] IntPtr hSourceHandle, [In] IntPtr hTargetProcessHandle, [In, Out] ref SafeTokenHandle lpTargetHandle, [In] uint dwDesiredAccess, [In] bool bInheritHandle, [In] uint dwOptions);
273 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
274 internal static extern bool DuplicateTokenEx([In] SafeTokenHandle ExistingTokenHandle, [In] TokenAccessLevels DesiredAccess, [In] IntPtr TokenAttributes, [In] SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, [In] System.Security.Principal.TokenType TokenType, [In, Out] ref SafeTokenHandle DuplicateTokenHandle);
275 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
276 internal static extern bool DuplicateTokenEx([In] SafeTokenHandle hExistingToken, [In] uint dwDesiredAccess, [In] IntPtr lpTokenAttributes, [In] uint ImpersonationLevel, [In] uint TokenType, [In, Out] ref SafeTokenHandle phNewToken);
277 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
278 internal static extern bool EncryptFile(string path);
279 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
280 internal static extern int ExpandEnvironmentStrings(string lpSrc, StringBuilder lpDst, int nSize);
281 [DllImport("kernel32.dll", SetLastError=true)]
282 internal static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, short wColorAttribute, int numCells, COORD startCoord, out int pNumBytesWritten);
283 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
284 internal static extern bool FillConsoleOutputCharacter(IntPtr hConsoleOutput, char character, int nLength, COORD dwWriteCoord, out int pNumCharsWritten);
285 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")]
286 internal static extern bool FindClose(IntPtr handle);
287 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
288 internal static extern SafeFindHandle FindFirstFile(string fileName, [In, Out] WIN32_FIND_DATA data);
289 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
290 internal static extern bool FindNextFile(SafeFindHandle hndFindFile, [In, Out, MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATA lpFindFileData);
291 [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
292 internal static extern unsafe int FindNLSString(int Locale, int dwFindFlags, char* lpStringSource, int cchSource, char* lpStringValue, int cchValue, IntPtr pcchFound);
293 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
294 internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments);
295 [DllImport("kernel32.dll")]
296 internal static extern int GetACP();
297 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
298 internal static extern int GetCalendarInfo(int Locale, int Calendar, int CalType, StringBuilder lpCalData, int cchData, IntPtr lpValue);
299 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
300 internal static extern int GetComputerName(StringBuilder nameBuffer, ref int bufferSize);
301 [DllImport("kernel32.dll")]
302 internal static extern uint GetConsoleCP();
303 [DllImport("kernel32.dll", SetLastError=true)]
304 internal static extern bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO cci);
305 [DllImport("kernel32.dll", SetLastError=true)]
306 internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int mode);
307 [DllImport("kernel32.dll")]
308 internal static extern uint GetConsoleOutputCP();
309 [DllImport("kernel32.dll", SetLastError=true)]
310 internal static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
311 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
312 internal static extern int GetConsoleTitle(StringBuilder sb, int capacity);
313 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
314 internal static extern int GetCurrentDirectory(int nBufferLength, StringBuilder lpBuffer);
315 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
316 internal static extern IntPtr GetCurrentProcess();
317 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
318 internal static extern uint GetCurrentProcessId();
319 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
320 internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);
321 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
322 internal static extern int GetDriveType(string drive);
323 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
324 internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
325 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
326 internal static extern bool GetFileAttributesEx(string name, int fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
327 [DllImport("kernel32.dll", SetLastError=true)]
328 internal static extern int GetFileSize(SafeFileHandle hFile, out int highSize);
329 [DllImport("kernel32.dll")]
330 internal static extern int GetFileType(SafeFileHandle handle);
331 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
332 internal static extern int GetFullPathName([In] char[] path, int numBufferChars, [Out] char[] buffer, IntPtr mustBeZero);
333 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
334 internal static extern unsafe int GetFullPathName(char* path, int numBufferChars, char* buffer, IntPtr mustBeZero);
335 [DllImport("user32.dll")]
336 internal static extern short GetKeyState(int virtualKeyCode);
337 [DllImport("kernel32.dll", SetLastError=true)]
338 internal static extern COORD GetLargestConsoleWindowSize(IntPtr hConsoleOutput);
339 [DllImport("kernel32.dll", SetLastError=true)]
340 internal static extern int GetLogicalDrives();
341 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
342 internal static extern unsafe int GetLongPathName(char* path, char* longPathBuffer, int bufferLength);
343 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
344 internal static extern int GetLongPathName(string path, StringBuilder longPathBuffer, int bufferLength);
345 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
346 internal static extern int GetLongPathName([In] char[] path, [Out] char[] longPathBuffer, int bufferLength);
347 internal static string GetMessage(int errorCode)
348 {
349 StringBuilder lpBuffer = new StringBuilder(0x200);
350 if (FormatMessage(0x3200, NULL, errorCode, 0, lpBuffer, lpBuffer.Capacity, NULL) != 0)
351 {
352 return lpBuffer.ToString();
353 }
354 return Environment.GetResourceString("UnknownError_Num", new object[] { errorCode });
355 }
356
357 [DllImport("user32.dll", ExactSpelling=true)]
358 internal static extern IntPtr GetProcessWindowStation();
359 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
360 internal static extern uint GetSecurityDescriptorLength(IntPtr byteArray);
361 [DllImport("advapi32.dll", EntryPoint="GetSecurityInfo", CharSet=CharSet.Unicode, SetLastError=true)]
362 internal static extern uint GetSecurityInfoByHandle(SafeHandle handle, uint objectType, uint securityInformation, out IntPtr sidOwner, out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
363 [DllImport("advapi32.dll", EntryPoint="GetNamedSecurityInfoW", CharSet=CharSet.Unicode, SetLastError=true)]
364 internal static extern uint GetSecurityInfoByName(string name, uint objectType, uint securityInformation, out IntPtr sidOwner, out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
365 [DllImport("kernel32.dll", SetLastError=true)]
366 internal static extern IntPtr GetStdHandle(int nStdHandle);
367 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
368 internal static extern int GetSystemDirectory(StringBuilder sb, int length);
369 [DllImport("kernel32.dll", SetLastError=true)]
370 internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
371 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
372 internal static extern uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
373 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
374 internal static extern uint GetTempPath(int bufferLen, StringBuilder buffer);
375 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
376 internal static extern bool GetTokenInformation([In] SafeTokenHandle TokenHandle, [In] uint TokenInformationClass, [In] SafeLocalAllocHandle TokenInformation, [In] uint TokenInformationLength, out uint ReturnLength);
377 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
378 internal static extern bool GetTokenInformation([In] IntPtr TokenHandle, [In] uint TokenInformationClass, [In] SafeLocalAllocHandle TokenInformation, [In] uint TokenInformationLength, out uint ReturnLength);
379 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
380 internal static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);
381 [DllImport("secur32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
382 internal static extern byte GetUserNameEx(int format, StringBuilder domainName, ref int domainNameLen);
383 [DllImport("user32.dll", SetLastError=true)]
384 internal static extern bool GetUserObjectInformation(IntPtr hObj, int nIndex, [MarshalAs(UnmanagedType.LPStruct)] USEROBJECTFLAGS pvBuffer, int nLength, ref int lpnLengthNeeded);
385 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
386 internal static extern bool GetVersionEx([In, Out] OSVERSIONINFO ver);
387 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
388 internal static extern bool GetVersionEx([In, Out] OSVERSIONINFOEX ver);
389 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
390 internal static extern bool GetVolumeInformation(string drive, StringBuilder volumeName, int volumeNameBufLen, out int volSerialNumber, out int maxFileNameLen, out int fileSystemFlags, StringBuilder fileSystemName, int fileSystemNameBufLen);
391 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
392 internal static extern int GetWindowsAccountDomainSid(byte[] sid, [Out] byte[] resultSid, ref uint resultSidLength);
393 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
394 internal static extern int GetWindowsDirectory(StringBuilder sb, int length);
395 [DllImport("kernel32.dll", SetLastError=true)]
396 internal static extern bool GlobalMemoryStatus([In, Out] MEMORYSTATUS buffer);
397 [DllImport("kernel32.dll", SetLastError=true)]
398 internal static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX buffer);
399 [DllImport("advapi32.dll", EntryPoint="EqualDomainSid", CharSet=CharSet.Unicode, SetLastError=true)]
400 internal static extern int IsEqualDomainSid(byte[] sid1, byte[] sid2, out bool result);
401 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
402 internal static extern int IsWellKnownSid(byte[] sid, int type);
403 [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
404 internal static extern unsafe int LCMapStringW(int lcid, int flags, char* src, int cchSrc, char* target, int cchTarget);
405 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
406 internal static extern SafeLocalAllocHandle LocalAlloc([In] int uFlags, [In] IntPtr sizetdwBytes);
407 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", EntryPoint="LocalAlloc")]
408 internal static extern IntPtr LocalAlloc_NoSafeHandle(int uFlags, IntPtr sizetdwBytes);
409 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
410 internal static extern IntPtr LocalFree(IntPtr handle);
411 [DllImport("kernel32.dll")]
412 internal static extern IntPtr LocalReAlloc(IntPtr handle, IntPtr sizetcbBytes, int uFlags);
413 [DllImport("kernel32.dll", SetLastError=true)]
414 internal static extern bool LockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
415 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
416 internal static extern bool LookupAccountName(string machineName, string accountName, byte[] sid, ref int sidLen, StringBuilder domainName, ref int domainNameLen, out int peUse);
417 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", EntryPoint="LookupPrivilegeValueW", CharSet=CharSet.Auto, SetLastError=true)]
418 internal static extern bool LookupPrivilegeValue([In] string lpSystemName, [In] string lpName, [In, Out] ref LUID Luid);
419 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", SetLastError=true)]
420 internal static extern int LsaClose(IntPtr handle);
421 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
422 internal static extern int LsaConnectUntrusted([In, Out] ref SafeLsaLogonProcessHandle LsaHandle);
423 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("secur32.dll", SetLastError=true)]
424 internal static extern int LsaDeregisterLogonProcess(IntPtr handle);
425 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", SetLastError=true)]
426 internal static extern int LsaFreeMemory(IntPtr handle);
427 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("secur32.dll", SetLastError=true)]
428 internal static extern int LsaFreeReturnBuffer(IntPtr handle);
429 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
430 internal static extern int LsaGetLogonSessionData([In] ref LUID LogonId, [In, Out] ref SafeLsaReturnBufferHandle ppLogonSessionData);
431 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
432 internal static extern int LsaLogonUser([In] SafeLsaLogonProcessHandle LsaHandle, [In] ref UNICODE_INTPTR_STRING OriginName, [In] uint LogonType, [In] uint AuthenticationPackage, [In] IntPtr AuthenticationInformation, [In] uint AuthenticationInformationLength, [In] IntPtr LocalGroups, [In] ref TOKEN_SOURCE SourceContext, [In, Out] ref SafeLsaReturnBufferHandle ProfileBuffer, [In, Out] ref uint ProfileBufferLength, [In, Out] ref LUID LogonId, [In, Out] ref SafeTokenHandle Token, [In, Out] ref QUOTA_LIMITS Quotas, [In, Out] ref int SubStatus);
433 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
434 internal static extern int LsaLookupAuthenticationPackage([In] SafeLsaLogonProcessHandle LsaHandle, [In] ref UNICODE_INTPTR_STRING PackageName, [In, Out] ref uint AuthenticationPackage);
435 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
436 internal static extern uint LsaLookupNames(SafeLsaPolicyHandle handle, int count, UNICODE_STRING[] names, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle sids);
437 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
438 internal static extern uint LsaLookupNames2(SafeLsaPolicyHandle handle, int flags, int count, UNICODE_STRING[] names, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle sids);
439 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
440 internal static extern uint LsaLookupSids(SafeLsaPolicyHandle handle, int count, IntPtr[] sids, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle names);
441 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
442 internal static extern int LsaNtStatusToWinError([In] int status);
443 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
444 internal static extern uint LsaOpenPolicy(string systemName, ref LSA_OBJECT_ATTRIBUTES attributes, int accessMask, out SafeLsaPolicyHandle handle);
445 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
446 internal static extern int LsaRegisterLogonProcess([In] ref UNICODE_INTPTR_STRING LogonProcessName, [In, Out] ref SafeLsaLogonProcessHandle LsaHandle, [In, Out] ref IntPtr SecurityMode);
447 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
448 internal static extern IntPtr lstrcpy(IntPtr dst, string src);
449 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
450 internal static extern IntPtr lstrcpy(StringBuilder dst, IntPtr src);
451 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
452 internal static extern int lstrlen(IntPtr ptr);
453 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
454 internal static extern int lstrlen(sbyte[] ptr);
455 [DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
456 internal static extern int lstrlenA(IntPtr ptr);
457 [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
458 internal static extern int lstrlenW(IntPtr ptr);
459 internal static int MakeHRFromErrorCode(int errorCode)
460 {
461 return (-2147024896 | errorCode);
462 }
463
464 [DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
465 internal static extern SafeViewOfFileHandle MapViewOfFile(SafeFileMappingHandle handle, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumerOfBytesToMap);
466 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
467 internal static extern bool MoveFile(string src, string dst);
468 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
469 internal static extern SafeWaitHandle OpenEvent(int desiredAccess, bool inheritHandle, string name);
470 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
471 internal static extern SafeWaitHandle OpenMutex(int desiredAccess, bool inheritHandle, string name);
472 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
473 internal static extern bool OpenProcessToken([In] IntPtr ProcessToken, [In] TokenAccessLevels DesiredAccess, [In, Out] ref SafeTokenHandle TokenHandle);
474 [DllImport("kernel32.dll", SetLastError=true)]
475 internal static extern bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
476 [DllImport("kernel32.dll", SetLastError=true)]
477 internal static extern bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
478 [DllImport("kernel32.dll", SetLastError=true)]
479 internal static extern unsafe bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion);
480 [DllImport("kernel32.dll", SetLastError=true)]
481 internal static extern unsafe int ReadFile(SafeFileHandle handle, byte* bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped* overlapped);
482 [DllImport("kernel32.dll", SetLastError=true)]
483 internal static extern unsafe int ReadFile(SafeFileHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);
484 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
485 internal static extern int RegConnectRegistry(string machineName, SafeRegistryHandle key, out SafeRegistryHandle result);
486 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
487 internal static extern int RegCreateKeyEx(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition);
488 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
489 internal static extern int RegDeleteKey(SafeRegistryHandle hKey, string lpSubKey);
490 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
491 internal static extern int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName);
492 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
493 internal static extern int RegEnumKeyEx(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpName, out int lpcbName, int[] lpReserved, StringBuilder lpClass, int[] lpcbClass, long[] lpftLastWriteTime);
494 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
495 internal static extern int RegEnumValue(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData);
496 [DllImport("advapi32.dll", CharSet=CharSet.Ansi)]
497 internal static extern int RegEnumValueA(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData);
498 [DllImport("advapi32.dll")]
499 internal static extern int RegFlushKey(SafeRegistryHandle hKey);
500 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
501 internal static extern int RegOpenKeyEx(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
502 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
503 internal static extern int RegQueryInfoKey(SafeRegistryHandle hKey, StringBuilder lpClass, int[] lpcbClass, IntPtr lpReserved_MustBeZero, ref int lpcSubKeys, int[] lpcbMaxSubKeyLen, int[] lpcbMaxClassLen, ref int lpcValues, int[] lpcbMaxValueNameLen, int[] lpcbMaxValueLen, int[] lpcbSecurityDescriptor, int[] lpftLastWriteTime);
504 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
505 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, StringBuilder lpData, ref int lpcbData);
506 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
507 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] char[] lpData, ref int lpcbData);
508 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
509 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, ref int lpData, ref int lpcbData);
510 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
511 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] byte[] lpData, ref int lpcbData);
512 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
513 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, ref long lpData, ref int lpcbData);
514 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
515 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, ref int lpData, int cbData);
516 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
517 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, byte[] lpData, int cbData);
518 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
519 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, string lpData, int cbData);
520 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
521 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, ref long lpData, int cbData);
522 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
523 internal static extern bool ReleaseMutex(SafeWaitHandle handle);
524 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
525 internal static extern bool RemoveDirectory(string path);
526 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
527 internal static extern bool ReplaceFile(string replacedFileName, string replacementFileName, string backupFileName, int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved);
528 [DllImport("kernel32.dll", SetLastError=true)]
529 internal static extern bool ResetEvent(SafeWaitHandle handle);
530 internal static SafeFileHandle SafeCreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
531 {
532 SafeFileHandle handle = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
533 if (!handle.IsInvalid && (GetFileType(handle) != 1))
534 {
535 handle.Dispose();
536 throw new NotSupportedException(Environment.GetResourceString("NotSupported_FileStreamOnNonFiles"));
537 }
538 return handle;
539 }
540
541 [DllImport("user32.dll", SetLastError=true)]
542 internal static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);
543 [DllImport("kernel32.dll", SetLastError=true)]
544 internal static extern bool SetConsoleCP(uint codePage);
545 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
546 internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandlerRoutine handler, bool addOrRemove);
547 [DllImport("kernel32.dll", SetLastError=true)]
548 internal static extern bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref CONSOLE_CURSOR_INFO cci);
549 [DllImport("kernel32.dll", SetLastError=true)]
550 internal static extern bool SetConsoleCursorPosition(IntPtr hConsoleOutput, COORD cursorPosition);
551 [DllImport("kernel32.dll", SetLastError=true)]
552 internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
553 [DllImport("kernel32.dll", SetLastError=true)]
554 internal static extern bool SetConsoleOutputCP(uint codePage);
555 [DllImport("kernel32.dll", SetLastError=true)]
556 internal static extern bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, COORD size);
557 [DllImport("kernel32.dll", SetLastError=true)]
558 internal static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, short attributes);
559 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
560 internal static extern bool SetConsoleTitle(string title);
561 [DllImport("kernel32.dll", SetLastError=true)]
562 internal static extern unsafe bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool absolute, SMALL_RECT* consoleWindow);
563 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
564 internal static extern bool SetCurrentDirectory(string path);
565 [DllImport("kernel32.dll", SetLastError=true)]
566 internal static extern bool SetEndOfFile(SafeFileHandle hFile);
567 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
568 internal static extern bool SetEnvironmentVariable(string lpName, string lpValue);
569 [DllImport("kernel32.dll")]
570 internal static extern int SetErrorMode(int newMode);
571 [DllImport("kernel32.dll", SetLastError=true)]
572 internal static extern bool SetEvent(SafeWaitHandle handle);
573 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
574 internal static extern bool SetFileAttributes(string name, int attr);
575 internal static unsafe long SetFilePointer(SafeFileHandle handle, long offset, SeekOrigin origin, out int hr)
576 {
577 hr = 0;
578 int lo = (int) offset;
579 int hi = (int) (offset >> 0x20);
580 lo = SetFilePointerWin32(handle, lo, &hi, (int) origin);
581 if ((lo == -1) && ((hr = Marshal.GetLastWin32Error()) != 0))
582 {
583 return -1L;
584 }
585 return (long) ((((ulong) hi) << 0x20) | ((ulong) lo));
586 }
587
588 [DllImport("kernel32.dll", EntryPoint="SetFilePointer", SetLastError=true)]
589 private static extern unsafe int SetFilePointerWin32(SafeFileHandle handle, int lo, int* hi, int origin);
590 [DllImport("kernel32.dll", SetLastError=true)]
591 internal static extern unsafe bool SetFileTime(SafeFileHandle hFile, FILE_TIME* creationTime, FILE_TIME* lastAccessTime, FILE_TIME* lastWriteTime);
592 [DllImport("kernel32.dll", SetLastError=true)]
593 internal static extern void SetLastError(int errorCode);
594 [DllImport("advapi32.dll", EntryPoint="SetSecurityInfo", CharSet=CharSet.Unicode, SetLastError=true)]
595 internal static extern uint SetSecurityInfoByHandle(SafeHandle handle, uint objectType, uint securityInformation, byte[] owner, byte[] group, byte[] dacl, byte[] sacl);
596 [DllImport("advapi32.dll", EntryPoint="SetNamedSecurityInfoW", CharSet=CharSet.Unicode, SetLastError=true)]
597 internal static extern uint SetSecurityInfoByName(string name, uint objectType, uint securityInformation, byte[] owner, byte[] group, byte[] dacl, byte[] sacl);
598 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
599 internal static extern bool SetVolumeLabel(string driveLetter, string volumeName);
600 [DllImport("shfolder.dll", CharSet=CharSet.Auto)]
601 internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
602 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("oleaut32.dll", CharSet=CharSet.Unicode)]
603 internal static extern IntPtr SysAllocStringLen(string src, int len);
604 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("oleaut32.dll")]
605 internal static extern void SysFreeString(IntPtr bstr);
606 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("oleaut32.dll")]
607 internal static extern int SysStringLen(IntPtr bstr);
608 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
609 internal static extern int SystemFunction040([In, Out] SafeBSTRHandle pDataIn, [In] uint cbDataIn, [In] uint dwFlags);
610 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
611 internal static extern int SystemFunction041([In, Out] SafeBSTRHandle pDataIn, [In] uint cbDataIn, [In] uint dwFlags);
612 [DllImport("kernel32.dll", SetLastError=true)]
613 internal static extern bool UnlockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
614 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", ExactSpelling=true)]
615 internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
616 internal static SafeFileHandle UnsafeCreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
617 {
618 return CreateFile(lpFileName, dwDesiredAccess, dwShareMode, securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
619 }
620
621 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
622 internal static extern unsafe void* VirtualAlloc(void* address, UIntPtr numBytes, int commitOrReserve, int pageProtectionMode);
623 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
624 internal static extern unsafe bool VirtualFree(void* address, UIntPtr numBytes, int pageFreeMode);
625 [DllImport("kernel32.dll", SetLastError=true)]
626 internal static extern unsafe IntPtr VirtualQuery(void* address, ref MEMORY_BASIC_INFORMATION buffer, IntPtr sizeOfBuffer);
627 [DllImport("kernel32.dll")]
628 internal static extern unsafe int WideCharToMultiByte(uint cp, uint flags, char* pwzSource, int cchSource, byte* pbDestBuffer, int cbDestBuffer, IntPtr null1, IntPtr null2);
629 [DllImport("kernel32.dll", SetLastError=true)]
630 internal static extern unsafe bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
631 [DllImport("kernel32.dll", SetLastError=true)]
632 internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
633 [DllImport("kernel32.dll", SetLastError=true)]
634 internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
635 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
636 internal static extern void ZeroMemory(IntPtr handle, uint length);
637
638 [StructLayout(LayoutKind.Sequential)]
639 internal struct CHAR_INFO
640 {
641 private ushort charData;
642 private short attributes;
643 }
644
645 [Serializable, Flags]
646 internal enum Color : short
647 {
648 BackgroundBlue = 0x10,
649 BackgroundGreen = 0x20,
650 BackgroundIntensity = 0x80,
651 BackgroundMask = 240,
652 BackgroundRed = 0x40,
653 BackgroundYellow = 0x60,
654 Black = 0,
655 ColorMask = 0xff,
656 ForegroundBlue = 1,
657 ForegroundGreen = 2,
658 ForegroundIntensity = 8,
659 ForegroundMask = 15,
660 ForegroundRed = 4,
661 ForegroundYellow = 6
662 }
663
664 [StructLayout(LayoutKind.Sequential)]
665 internal struct CONSOLE_CURSOR_INFO
666 {
667 internal int dwSize;
668 internal bool bVisible;
669 }
670
671 [StructLayout(LayoutKind.Sequential)]
672 internal struct CONSOLE_SCREEN_BUFFER_INFO
673 {
674 internal Win32Native.COORD dwSize;
675 internal Win32Native.COORD dwCursorPosition;
676 internal short wAttributes;
677 internal Win32Native.SMALL_RECT srWindow;
678 internal Win32Native.COORD dwMaximumWindowSize;
679 }
680
681 internal delegate bool ConsoleCtrlHandlerRoutine(int controlType);
682
683 [StructLayout(LayoutKind.Sequential)]
684 internal struct COORD
685 {
686 internal short X;
687 internal short Y;
688 }
689
690 [StructLayout(LayoutKind.Sequential)]
691 internal struct FILE_TIME
692 {
693 internal uint ftTimeLow;
694 internal uint ftTimeHigh;
695 public FILE_TIME(long fileTime)
696 {
697 this.ftTimeLow = (uint) fileTime;
698 this.ftTimeHigh = (uint) (fileTime >> 0x20);
699 }
700
701 public long ToTicks()
702 {
703 return (long) ((this.ftTimeHigh << 0x20) + this.ftTimeLow);
704 }
705 }
706
707 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
708 internal struct InputRecord
709 {
710 internal short eventType;
711 internal Win32Native.KeyEventRecord keyEvent;
712 }
713
714 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
715 internal struct KERB_S4U_LOGON
716 {
717 internal uint MessageType;
718 internal uint Flags;
719 internal Win32Native.UNICODE_INTPTR_STRING ClientUpn;
720 internal Win32Native.UNICODE_INTPTR_STRING ClientRealm;
721 }
722
723 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
724 internal struct KeyEventRecord
725 {
726 internal bool keyDown;
727 internal short repeatCount;
728 internal short virtualKeyCode;
729 internal short virtualScanCode;
730 internal char uChar;
731 internal int controlKeyState;
732 }
733
734 [StructLayout(LayoutKind.Sequential)]
735 internal struct LSA_OBJECT_ATTRIBUTES
736 {
737 internal int Length;
738 internal IntPtr RootDirectory;
739 internal IntPtr ObjectName;
740 internal int Attributes;
741 internal IntPtr SecurityDescriptor;
742 internal IntPtr SecurityQualityOfService;
743 }
744
745 [StructLayout(LayoutKind.Sequential)]
746 internal struct LSA_REFERENCED_DOMAIN_LIST
747 {
748 internal int Entries;
749 internal IntPtr Domains;
750 }
751
752 [StructLayout(LayoutKind.Sequential)]
753 internal struct LSA_TRANSLATED_NAME
754 {
755 internal int Use;
756 internal Win32Native.UNICODE_INTPTR_STRING Name;
757 internal int DomainIndex;
758 }
759
760 [StructLayout(LayoutKind.Sequential)]
761 internal struct LSA_TRANSLATED_SID
762 {
763 internal int Use;
764 internal uint Rid;
765 internal int DomainIndex;
766 }
767
768 [StructLayout(LayoutKind.Sequential)]
769 internal struct LSA_TRANSLATED_SID2
770 {
771 internal int Use;
772 internal IntPtr Sid;
773 internal int DomainIndex;
774 private uint Flags;
775 }
776
777 [StructLayout(LayoutKind.Sequential)]
778 internal struct LSA_TRUST_INFORMATION
779 {
780 internal Win32Native.UNICODE_INTPTR_STRING Name;
781 internal IntPtr Sid;
782 }
783
784 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
785 internal struct LUID
786 {
787 internal uint LowPart;
788 internal uint HighPart;
789 }
790
791 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
792 internal struct LUID_AND_ATTRIBUTES
793 {
794 internal Win32Native.LUID Luid;
795 internal uint Attributes;
796 }
797
798 [StructLayout(LayoutKind.Sequential)]
799 internal struct MEMORY_BASIC_INFORMATION
800 {
801 internal unsafe void* BaseAddress;
802 internal unsafe void* AllocationBase;
803 internal uint AllocationProtect;
804 internal UIntPtr RegionSize;
805 internal uint State;
806 internal uint Protect;
807 internal uint Type;
808 }
809
810 [StructLayout(LayoutKind.Sequential)]
811 internal class MEMORYSTATUS
812 {
813 internal int length;
814 internal int memoryLoad;
815 internal uint totalPhys;
816 internal uint availPhys;
817 internal uint totalPageFile;
818 internal uint availPageFile;
819 internal uint totalVirtual;
820 internal uint availVirtual;
821 internal MEMORYSTATUS()
822 {
823 this.length = Marshal.SizeOf(this);
824 }
825 }
826
827 [StructLayout(LayoutKind.Sequential)]
828 internal class MEMORYSTATUSEX
829 {
830 internal int length;
831 internal int memoryLoad;
832 internal ulong totalPhys;
833 internal ulong availPhys;
834 internal ulong totalPageFile;
835 internal ulong availPageFile;
836 internal ulong totalVirtual;
837 internal ulong availVirtual;
838 internal ulong availExtendedVirtual;
839 internal MEMORYSTATUSEX()
840 {
841 this.length = Marshal.SizeOf(this);
842 }
843 }
844
845 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
846 internal class OSVERSIONINFO
847 {
848 internal int OSVersionInfoSize;
849 internal int MajorVersion;
850 internal int MinorVersion;
851 internal int BuildNumber;
852 internal int PlatformId;
853 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
854 internal string CSDVersion;
855 internal OSVERSIONINFO()
856 {
857 this.OSVersionInfoSize = Marshal.SizeOf(this);
858 }
859 }
860
861 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
862 internal class OSVERSIONINFOEX
863 {
864 internal int OSVersionInfoSize;
865 internal int MajorVersion;
866 internal int MinorVersion;
867 internal int BuildNumber;
868 internal int PlatformId;
869 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
870 internal string CSDVersion;
871 internal ushort ServicePackMajor;
872 internal ushort ServicePackMinor;
873 internal short SuiteMask;
874 internal byte ProductType;
875 internal byte Reserved;
876 public OSVERSIONINFOEX()
877 {
878 this.OSVersionInfoSize = Marshal.SizeOf(this);
879 }
880 }
881
882 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
883 internal struct QUOTA_LIMITS
884 {
885 internal IntPtr PagedPoolLimit;
886 internal IntPtr NonPagedPoolLimit;
887 internal IntPtr MinimumWorkingSetSize;
888 internal IntPtr MaximumWorkingSetSize;
889 internal IntPtr PagefileLimit;
890 internal IntPtr TimeLimit;
891 }
892
893 [StructLayout(LayoutKind.Sequential)]
894 internal class SECURITY_ATTRIBUTES
895 {
896 internal int nLength;
897 internal unsafe byte* pSecurityDescriptor = null;
898 internal int bInheritHandle;
899 }
900
901 internal enum SECURITY_IMPERSONATION_LEVEL
902 {
903 Anonymous,
904 Identification,
905 Impersonation,
906 Delegation
907 }
908
909 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
910 internal struct SECURITY_LOGON_SESSION_DATA
911 {
912 internal uint Size;
913 internal Win32Native.LUID LogonId;
914 internal Win32Native.UNICODE_INTPTR_STRING UserName;
915 internal Win32Native.UNICODE_INTPTR_STRING LogonDomain;
916 internal Win32Native.UNICODE_INTPTR_STRING AuthenticationPackage;
917 internal uint LogonType;
918 internal uint Session;
919 internal IntPtr Sid;
920 internal long LogonTime;
921 }
922
923 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
924 internal struct SID_AND_ATTRIBUTES
925 {
926 internal IntPtr Sid;
927 internal uint Attributes;
928 }
929
930 [StructLayout(LayoutKind.Sequential)]
931 internal struct SMALL_RECT
932 {
933 internal short Left;
934 internal short Top;
935 internal short Right;
936 internal short Bottom;
937 }
938
939 [StructLayout(LayoutKind.Sequential)]
940 internal struct SYSTEM_INFO
941 {
942 internal int dwOemId;
943 internal int dwPageSize;
944 internal IntPtr lpMinimumApplicationAddress;
945 internal IntPtr lpMaximumApplicationAddress;
946 internal IntPtr dwActiveProcessorMask;
947 internal int dwNumberOfProcessors;
948 internal int dwProcessorType;
949 internal int dwAllocationGranularity;
950 internal short wProcessorLevel;
951 internal short wProcessorRevision;
952 }
953
954 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
955 internal struct TOKEN_GROUPS
956 {
957 internal uint GroupCount;
958 internal Win32Native.SID_AND_ATTRIBUTES Groups;
959 }
960
961 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
962 internal struct TOKEN_PRIVILEGE
963 {
964 internal uint PrivilegeCount;
965 internal Win32Native.LUID_AND_ATTRIBUTES Privilege;
966 }
967
968 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
969 internal struct TOKEN_SOURCE
970 {
971 private const int TOKEN_SOURCE_LENGTH = 8;
972 [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
973 internal char[] Name;
974 internal Win32Native.LUID SourceIdentifier;
975 }
976
977 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
978 internal struct TOKEN_STATISTICS
979 {
980 internal Win32Native.LUID TokenId;
981 internal Win32Native.LUID AuthenticationId;
982 internal long ExpirationTime;
983 internal uint TokenType;
984 internal uint ImpersonationLevel;
985 internal uint DynamicCharged;
986 internal uint DynamicAvailable;
987 internal uint GroupCount;
988 internal uint PrivilegeCount;
989 internal Win32Native.LUID ModifiedId;
990 }
991
992 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
993 internal struct TOKEN_USER
994 {
995 internal Win32Native.SID_AND_ATTRIBUTES User;
996 }
997
998 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
999 internal struct UNICODE_INTPTR_STRING
1000 {
1001 internal ushort Length;
1002 internal ushort MaxLength;
1003 internal IntPtr Buffer;
1004 internal UNICODE_INTPTR_STRING(int length, int maximumLength, IntPtr buffer)
1005 {
1006 this.Length = (ushort) length;
1007 this.MaxLength = (ushort) maximumLength;
1008 this.Buffer = buffer;
1009 }
1010 }
1011
1012 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
1013 internal struct UNICODE_STRING
1014 {
1015 internal ushort Length;
1016 internal ushort MaximumLength;
1017 [MarshalAs(UnmanagedType.LPWStr)]
1018 internal string Buffer;
1019 }
1020
1021 [StructLayout(LayoutKind.Sequential)]
1022 internal class USEROBJECTFLAGS
1023 {
1024 internal int fInherit;
1025 internal int fReserved;
1026 internal int dwFlags;
1027 }
1028
1029 [Serializable, StructLayout(LayoutKind.Sequential)]
1030 internal struct WIN32_FILE_ATTRIBUTE_DATA
1031 {
1032 internal int fileAttributes;
1033 internal uint ftCreationTimeLow;
1034 internal uint ftCreationTimeHigh;
1035 internal uint ftLastAccessTimeLow;
1036 internal uint ftLastAccessTimeHigh;
1037 internal uint ftLastWriteTimeLow;
1038 internal uint ftLastWriteTimeHigh;
1039 internal int fileSizeHigh;
1040 internal int fileSizeLow;
1041 }
1042
1043 [Serializable, StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto), BestFitMapping(false)]
1044 internal class WIN32_FIND_DATA
1045 {
1046 internal int dwFileAttributes;
1047 internal int ftCreationTime_dwLowDateTime;
1048 internal int ftCreationTime_dwHighDateTime;
1049 internal int ftLastAccessTime_dwLowDateTime;
1050 internal int ftLastAccessTime_dwHighDateTime;
1051 internal int ftLastWriteTime_dwLowDateTime;
1052 internal int ftLastWriteTime_dwHighDateTime;
1053 internal int nFileSizeHigh;
1054 internal int nFileSizeLow;
1055 internal int dwReserved0;
1056 internal int dwReserved1;
1057 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
1058 internal string cFileName;
1059 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=14)]
1060 internal string cAlternateFileName;
1061 }
1062 }
1063 }
1064
2 namespace Microsoft.Win32
3 {
4 using Microsoft.Win32.SafeHandles;
5 using System;
6 using System.IO;
7 using System.Runtime.CompilerServices;
8 using System.Runtime.ConstrainedExecution;
9 using System.Runtime.InteropServices;
10 using System.Security;
11 using System.Security.Principal;
12 using System.Text;
13 using System.Threading;
14
15 [SuppressUnmanagedCodeSecurity]
16 internal static class Win32Native
17 {
18 internal const string ADVAPI32 = "advapi32.dll";
19 internal const uint ANONYMOUS_LOGON_LUID = 0x3e6;
20 internal const string CRYPT32 = "crypt32.dll";
21 internal const uint CRYPTPROTECTMEMORY_BLOCK_SIZE = 0x10;
22 internal const uint CRYPTPROTECTMEMORY_CROSS_PROCESS = 1;
23 internal const uint CRYPTPROTECTMEMORY_SAME_LOGON = 2;
24 internal const uint CRYPTPROTECTMEMORY_SAME_PROCESS = 0;
25 internal const int CSIDL_APPDATA = 0x1a;
26 internal const int CSIDL_COMMON_APPDATA = 0x23;
27 internal const int CSIDL_COOKIES = 0x21;
28 internal const int CSIDL_DESKTOP = 0;
29 internal const int CSIDL_DESKTOPDIRECTORY = 0x10;
30 internal const int CSIDL_DRIVES = 0x11;
31 internal const int CSIDL_FAVORITES = 6;
32 internal const int CSIDL_HISTORY = 0x22;
33 internal const int CSIDL_INTERNET_CACHE = 0x20;
34 internal const int CSIDL_LOCAL_APPDATA = 0x1c;
35 internal const int CSIDL_MYMUSIC = 13;
36 internal const int CSIDL_MYPICTURES = 0x27;
37 internal const int CSIDL_PERSONAL = 5;
38 internal const int CSIDL_PROGRAM_FILES = 0x26;
39 internal const int CSIDL_PROGRAM_FILES_COMMON = 0x2b;
40 internal const int CSIDL_PROGRAMS = 2;
41 internal const int CSIDL_RECENT = 8;
42 internal const int CSIDL_SENDTO = 9;
43 internal const int CSIDL_STARTMENU = 11;
44 internal const int CSIDL_STARTUP = 7;
45 internal const int CSIDL_SYSTEM = 0x25;
46 internal const int CSIDL_TEMPLATES = 0x15;
47 internal const int CTRL_BREAK_EVENT = 1;
48 internal const int CTRL_C_EVENT = 0;
49 internal const int CTRL_CLOSE_EVENT = 2;
50 internal const int CTRL_LOGOFF_EVENT = 5;
51 internal const int CTRL_SHUTDOWN_EVENT = 6;
52 internal const int DOMAIN_USER_RID_GUEST = 0x1f5;
53 internal const uint DUPLICATE_CLOSE_SOURCE = 1;
54 internal const uint DUPLICATE_SAME_ACCESS = 2;
55 internal const uint DUPLICATE_SAME_ATTRIBUTES = 4;
56 internal const int ENABLE_ECHO_INPUT = 4;
57 internal const int ENABLE_LINE_INPUT = 2;
58 internal const int ENABLE_PROCESSED_INPUT = 1;
59 internal const int ERROR_ACCESS_DENIED = 5;
60 internal const int ERROR_ALREADY_EXISTS = 0xb7;
61 internal const int ERROR_BAD_IMPERSONATION_LEVEL = 0x542;
62 internal const int ERROR_BAD_LENGTH = 0x18;
63 internal const int ERROR_BAD_PATHNAME = 0xa1;
64 internal const int ERROR_CALL_NOT_IMPLEMENTED = 120;
65 internal const int ERROR_CANT_OPEN_ANONYMOUS = 0x543;
66 internal const int ERROR_DLL_INIT_FAILED = 0x45a;
67 internal const int ERROR_ENVVAR_NOT_FOUND = 0xcb;
68 internal const int ERROR_FILE_EXISTS = 80;
69 internal const int ERROR_FILE_NOT_FOUND = 2;
70 internal const int ERROR_FILENAME_EXCED_RANGE = 0xce;
71 internal const int ERROR_INSUFFICIENT_BUFFER = 0x7a;
72 internal const int ERROR_INVALID_ACL = 0x538;
73 internal const int ERROR_INVALID_DATA = 13;
74 internal const int ERROR_INVALID_DRIVE = 15;
75 internal const int ERROR_INVALID_FUNCTION = 1;
76 internal const int ERROR_INVALID_HANDLE = 6;
77 internal const int ERROR_INVALID_NAME = 0x7b;
78 internal const int ERROR_INVALID_OWNER = 0x51b;
79 internal const int ERROR_INVALID_PARAMETER = 0x57;
80 internal const int ERROR_INVALID_PRIMARY_GROUP = 0x51c;
81 internal const int ERROR_INVALID_SECURITY_DESCR = 0x53a;
82 internal const int ERROR_INVALID_SID = 0x539;
83 internal const int ERROR_MORE_DATA = 0xea;
84 internal const int ERROR_NO_DATA = 0xe8;
85 internal const int ERROR_NO_MORE_FILES = 0x12;
86 internal const int ERROR_NO_SECURITY_ON_OBJECT = 0x546;
87 internal const int ERROR_NO_SUCH_PRIVILEGE = 0x521;
88 internal const int ERROR_NO_TOKEN = 0x3f0;
89 internal const int ERROR_NON_ACCOUNT_SID = 0x4e9;
90 internal const int ERROR_NONE_MAPPED = 0x534;
91 internal const int ERROR_NOT_ALL_ASSIGNED = 0x514;
92 internal const int ERROR_NOT_ENOUGH_MEMORY = 8;
93 internal const int ERROR_NOT_READY = 0x15;
94 internal const int ERROR_NOT_SUPPORTED = 50;
95 internal const int ERROR_OPERATION_ABORTED = 0x3e3;
96 internal const int ERROR_PATH_NOT_FOUND = 3;
97 internal const int ERROR_PIPE_NOT_CONNECTED = 0xe9;
98 internal const int ERROR_PRIVILEGE_NOT_HELD = 0x522;
99 internal const int ERROR_SHARING_VIOLATION = 0x20;
100 internal const int ERROR_SUCCESS = 0;
101 internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6fd;
102 internal const int ERROR_UNKNOWN_REVISION = 0x519;
103 internal const int EVENT_MODIFY_STATE = 2;
104 internal const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
105 internal const int FILE_ATTRIBUTE_READONLY = 1;
106 internal const int FILE_ATTRIBUTE_REPARSE_POINT = 0x400;
107 internal const int FILE_TYPE_CHAR = 2;
108 internal const int FILE_TYPE_DISK = 1;
109 internal const int FILE_TYPE_PIPE = 3;
110 internal const int FIND_ENDSWITH = 0x200000;
111 internal const int FIND_FROMEND = 0x800000;
112 internal const int FIND_FROMSTART = 0x400000;
113 internal const int FIND_STARTSWITH = 0x100000;
114 private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000;
115 private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
116 private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;
117 internal const int HWND_BROADCAST = 0xffff;
118 internal const int INVALID_FILE_SIZE = -1;
119 internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
120 internal const int IO_REPARSE_TAG_MOUNT_POINT = -1610612733;
121 internal const string KERNEL32 = "kernel32.dll";
122 internal const int KEY_CREATE_LINK = 0x20;
123 internal const int KEY_CREATE_SUB_KEY = 4;
124 internal const int KEY_ENUMERATE_SUB_KEYS = 8;
125 internal const short KEY_EVENT = 1;
126 internal const int KEY_NOTIFY = 0x10;
127 internal const int KEY_QUERY_VALUE = 1;
128 internal const int KEY_READ = 0x20019;
129 internal const int KEY_SET_VALUE = 2;
130 internal const int KEY_WRITE = 0x20006;
131 internal const int LCID_SUPPORTED = 2;
132 internal const int LCMAP_SORTKEY = 0x400;
133 internal const int LMEM_FIXED = 0;
134 internal const int LMEM_ZEROINIT = 0x40;
135 internal const int LPTR = 0x40;
136 internal const string LSTRCPY = "lstrcpy";
137 internal const string LSTRCPYN = "lstrcpyn";
138 internal const string LSTRLEN = "lstrlen";
139 internal const string LSTRLENA = "lstrlenA";
140 internal const string LSTRLENW = "lstrlenW";
141 internal const int MEM_COMMIT = 0x1000;
142 internal const int MEM_FREE = 0x10000;
143 internal const int MEM_RELEASE = 0x8000;
144 internal const int MEM_RESERVE = 0x2000;
145 internal const string MICROSOFT_KERBEROS_NAME = "Kerberos";
146 internal const string MOVEMEMORY = "RtlMoveMemory";
147 internal const string MSCORWKS = "mscorwks.dll";
148 internal const int MUTEX_ALL_ACCESS = 0x1f0001;
149 internal const int MUTEX_MODIFY_STATE = 1;
150 internal const int NameSamCompatible = 2;
151 internal static readonly IntPtr NULL = IntPtr.Zero;
152 internal const string OLE32 = "ole32.dll";
153 internal const string OLEAUT32 = "oleaut32.dll";
154 internal const int PAGE_READWRITE = 4;
155 internal const int READ_CONTROL = 0x20000;
156 internal const int REG_BINARY = 3;
157 internal const int REG_DWORD = 4;
158 internal const int REG_DWORD_BIG_ENDIAN = 5;
159 internal const int REG_DWORD_LITTLE_ENDIAN = 4;
160 internal const int REG_EXPAND_SZ = 2;
161 internal const int REG_FULL_RESOURCE_DESCRIPTOR = 9;
162 internal const int REG_LINK = 6;
163 internal const int REG_MULTI_SZ = 7;
164 internal const int REG_NONE = 0;
165 internal const int REG_QWORD = 11;
166 internal const int REG_RESOURCE_LIST = 8;
167 internal const int REG_RESOURCE_REQUIREMENTS_LIST = 10;
168 internal const int REG_SZ = 1;
169 internal const int REPLACEFILE_IGNORE_MERGE_ERRORS = 2;
170 internal const int REPLACEFILE_WRITE_THROUGH = 1;
171 internal const uint SE_GROUP_ENABLED = 4;
172 internal const uint SE_GROUP_ENABLED_BY_DEFAULT = 2;
173 internal const uint SE_GROUP_LOGON_ID = 0xc0000000;
174 internal const uint SE_GROUP_MANDATORY = 1;
175 internal const uint SE_GROUP_OWNER = 8;
176 internal const uint SE_GROUP_RESOURCE = 0x20000000;
177 internal const uint SE_GROUP_USE_FOR_DENY_ONLY = 0x10;
178 internal const uint SE_PRIVILEGE_DISABLED = 0;
179 internal const uint SE_PRIVILEGE_ENABLED = 2;
180 internal const uint SE_PRIVILEGE_ENABLED_BY_DEFAULT = 1;
181 internal const uint SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
182 internal const string SECUR32 = "secur32.dll";
183 internal const int SECURITY_ANONYMOUS = 0;
184 internal const int SECURITY_ANONYMOUS_LOGON_RID = 7;
185 internal const int SECURITY_AUTHENTICATED_USER_RID = 11;
186 internal const int SECURITY_BUILTIN_DOMAIN_RID = 0x20;
187 internal const int SECURITY_LOCAL_SYSTEM_RID = 0x12;
188 internal const int SECURITY_SQOS_PRESENT = 0x100000;
189 internal const int SEM_FAILCRITICALERRORS = 1;
190 internal const int SEMAPHORE_MODIFY_STATE = 2;
191 internal const string SHFOLDER = "shfolder.dll";
192 internal const int SHGFP_TYPE_CURRENT = 0;
193 internal const string SHIM = "mscoree.dll";
194 internal const int STANDARD_RIGHTS_READ = 0x20000;
195 internal const int STANDARD_RIGHTS_WRITE = 0x20000;
196 internal const uint STATUS_ACCESS_DENIED = 0xc0000022;
197 internal const int STATUS_ACCOUNT_RESTRICTION = -1073741714;
198 internal const uint STATUS_INSUFFICIENT_RESOURCES = 0xc000009a;
199 internal const uint STATUS_NO_MEMORY = 0xc0000017;
200 internal const uint STATUS_NONE_MAPPED = 0xc0000073;
201 internal const uint STATUS_SOME_NOT_MAPPED = 0x107;
202 internal const int STD_ERROR_HANDLE = -12;
203 internal const int STD_INPUT_HANDLE = -10;
204 internal const int STD_OUTPUT_HANDLE = -11;
205 internal const int SYNCHRONIZE = 0x100000;
206 internal const int UOI_FLAGS = 1;
207 internal const string USER32 = "user32.dll";
208 internal const int VER_PLATFORM_WIN32_NT = 2;
209 internal const int VER_PLATFORM_WIN32_WINDOWS = 1;
210 internal const int VER_PLATFORM_WIN32s = 0;
211 internal const int VER_PLATFORM_WINCE = 3;
212 internal const int WM_SETTINGCHANGE = 0x1a;
213 internal const int WSF_VISIBLE = 1;
214
215 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
216 internal static extern bool AdjustTokenPrivileges([In] SafeTokenHandle TokenHandle, [In] bool DisableAllPrivileges, [In] ref TOKEN_PRIVILEGE NewState, [In] uint BufferLength, [In, Out] ref TOKEN_PRIVILEGE PreviousState, [In, Out] ref uint ReturnLength);
217 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
218 internal static extern bool AllocateLocallyUniqueId([In, Out] ref LUID Luid);
219 [DllImport("kernel32.dll", SetLastError=true)]
220 internal static extern bool Beep(int frequency, int duration);
221 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
222 internal static extern bool CheckTokenMembership([In] SafeTokenHandle TokenHandle, [In] byte[] SidToCheck, [In, Out] ref bool IsMember);
223 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
224 internal static extern bool CloseHandle(IntPtr handle);
225 [DllImport("advapi32.dll", EntryPoint="ConvertSecurityDescriptorToStringSecurityDescriptorW", CharSet=CharSet.Unicode, SetLastError=true)]
226 internal static extern int ConvertSdToStringSd(byte[] securityDescriptor, uint requestedRevision, uint securityInformation, out IntPtr resultString, ref uint resultStringLength);
227 [DllImport("advapi32.dll", EntryPoint="ConvertStringSecurityDescriptorToSecurityDescriptorW", CharSet=CharSet.Unicode, SetLastError=true)]
228 internal static extern int ConvertStringSdToSd(string stringSd, uint stringSdRevision, out IntPtr resultSd, ref uint resultSdLength);
229 [DllImport("advapi32.dll", EntryPoint="ConvertStringSidToSidW", CharSet=CharSet.Unicode, SetLastError=true)]
230 internal static extern int ConvertStringSidToSid(string stringSid, out IntPtr ByteArray);
231 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
232 internal static extern bool CopyFile(string src, string dst, bool failIfExists);
233 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Ansi)]
234 internal static extern void CopyMemoryAnsi(IntPtr pdst, string psrc, IntPtr sizetcb);
235 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Ansi)]
236 internal static extern void CopyMemoryAnsi(StringBuilder pdst, IntPtr psrc, IntPtr sizetcb);
237 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Unicode)]
238 internal static extern void CopyMemoryUni(IntPtr pdst, string psrc, IntPtr sizetcb);
239 [DllImport("kernel32.dll", EntryPoint="RtlMoveMemory", CharSet=CharSet.Unicode)]
240 internal static extern void CopyMemoryUni(StringBuilder pdst, IntPtr psrc, IntPtr sizetcb);
241 [DllImport("ole32.dll")]
242 internal static extern IntPtr CoTaskMemAlloc(int cb);
243 [DllImport("ole32.dll")]
244 internal static extern void CoTaskMemFree(IntPtr ptr);
245 [DllImport("ole32.dll")]
246 internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, int cb);
247 [DllImport("mscorwks.dll", CharSet=CharSet.Auto)]
248 internal static extern int CreateAssemblyEnum(out IAssemblyEnum ppEnum, IApplicationContext pAppCtx, IAssemblyName pName, uint dwFlags, IntPtr pvReserved);
249 [DllImport("mscorwks.dll", CharSet=CharSet.Unicode)]
250 internal static extern int CreateAssemblyNameObject(out IAssemblyName ppEnum, string szAssemblyName, uint dwFlags, IntPtr pvReserved);
251 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
252 internal static extern bool CreateDirectory(string path, SECURITY_ATTRIBUTES lpSecurityAttributes);
253 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
254 internal static extern SafeWaitHandle CreateEvent(SECURITY_ATTRIBUTES lpSecurityAttributes, bool isManualReset, bool initialState, string name);
255 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
256 private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
257 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
258 internal static extern SafeFileMappingHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, uint fProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
259 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
260 internal static extern SafeWaitHandle CreateMutex(SECURITY_ATTRIBUTES lpSecurityAttributes, bool initialOwner, string name);
261 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
262 internal static extern int CreateWellKnownSid(int sidType, byte[] domainSid, [Out] byte[] resultSid, ref uint resultSidLength);
263 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
264 internal static extern bool DecryptFile(string path, int reservedMustBeZero);
265 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
266 internal static extern bool DeleteFile(string path);
267 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
268 internal static extern bool DeleteVolumeMountPoint(string mountPoint);
269 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
270 internal static extern bool DuplicateHandle([In] IntPtr hSourceProcessHandle, [In] SafeTokenHandle hSourceHandle, [In] IntPtr hTargetProcessHandle, [In, Out] ref SafeTokenHandle lpTargetHandle, [In] uint dwDesiredAccess, [In] bool bInheritHandle, [In] uint dwOptions);
271 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
272 internal static extern bool DuplicateHandle([In] IntPtr hSourceProcessHandle, [In] IntPtr hSourceHandle, [In] IntPtr hTargetProcessHandle, [In, Out] ref SafeTokenHandle lpTargetHandle, [In] uint dwDesiredAccess, [In] bool bInheritHandle, [In] uint dwOptions);
273 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
274 internal static extern bool DuplicateTokenEx([In] SafeTokenHandle ExistingTokenHandle, [In] TokenAccessLevels DesiredAccess, [In] IntPtr TokenAttributes, [In] SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, [In] System.Security.Principal.TokenType TokenType, [In, Out] ref SafeTokenHandle DuplicateTokenHandle);
275 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
276 internal static extern bool DuplicateTokenEx([In] SafeTokenHandle hExistingToken, [In] uint dwDesiredAccess, [In] IntPtr lpTokenAttributes, [In] uint ImpersonationLevel, [In] uint TokenType, [In, Out] ref SafeTokenHandle phNewToken);
277 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
278 internal static extern bool EncryptFile(string path);
279 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
280 internal static extern int ExpandEnvironmentStrings(string lpSrc, StringBuilder lpDst, int nSize);
281 [DllImport("kernel32.dll", SetLastError=true)]
282 internal static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, short wColorAttribute, int numCells, COORD startCoord, out int pNumBytesWritten);
283 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
284 internal static extern bool FillConsoleOutputCharacter(IntPtr hConsoleOutput, char character, int nLength, COORD dwWriteCoord, out int pNumCharsWritten);
285 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")]
286 internal static extern bool FindClose(IntPtr handle);
287 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
288 internal static extern SafeFindHandle FindFirstFile(string fileName, [In, Out] WIN32_FIND_DATA data);
289 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
290 internal static extern bool FindNextFile(SafeFindHandle hndFindFile, [In, Out, MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATA lpFindFileData);
291 [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
292 internal static extern unsafe int FindNLSString(int Locale, int dwFindFlags, char* lpStringSource, int cchSource, char* lpStringValue, int cchValue, IntPtr pcchFound);
293 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
294 internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments);
295 [DllImport("kernel32.dll")]
296 internal static extern int GetACP();
297 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
298 internal static extern int GetCalendarInfo(int Locale, int Calendar, int CalType, StringBuilder lpCalData, int cchData, IntPtr lpValue);
299 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
300 internal static extern int GetComputerName(StringBuilder nameBuffer, ref int bufferSize);
301 [DllImport("kernel32.dll")]
302 internal static extern uint GetConsoleCP();
303 [DllImport("kernel32.dll", SetLastError=true)]
304 internal static extern bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO cci);
305 [DllImport("kernel32.dll", SetLastError=true)]
306 internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int mode);
307 [DllImport("kernel32.dll")]
308 internal static extern uint GetConsoleOutputCP();
309 [DllImport("kernel32.dll", SetLastError=true)]
310 internal static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
311 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
312 internal static extern int GetConsoleTitle(StringBuilder sb, int capacity);
313 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
314 internal static extern int GetCurrentDirectory(int nBufferLength, StringBuilder lpBuffer);
315 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
316 internal static extern IntPtr GetCurrentProcess();
317 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
318 internal static extern uint GetCurrentProcessId();
319 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
320 internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);
321 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
322 internal static extern int GetDriveType(string drive);
323 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
324 internal static extern int GetEnvironmentVariable(string lpName, StringBuilder lpValue, int size);
325 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
326 internal static extern bool GetFileAttributesEx(string name, int fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
327 [DllImport("kernel32.dll", SetLastError=true)]
328 internal static extern int GetFileSize(SafeFileHandle hFile, out int highSize);
329 [DllImport("kernel32.dll")]
330 internal static extern int GetFileType(SafeFileHandle handle);
331 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
332 internal static extern int GetFullPathName([In] char[] path, int numBufferChars, [Out] char[] buffer, IntPtr mustBeZero);
333 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
334 internal static extern unsafe int GetFullPathName(char* path, int numBufferChars, char* buffer, IntPtr mustBeZero);
335 [DllImport("user32.dll")]
336 internal static extern short GetKeyState(int virtualKeyCode);
337 [DllImport("kernel32.dll", SetLastError=true)]
338 internal static extern COORD GetLargestConsoleWindowSize(IntPtr hConsoleOutput);
339 [DllImport("kernel32.dll", SetLastError=true)]
340 internal static extern int GetLogicalDrives();
341 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
342 internal static extern unsafe int GetLongPathName(char* path, char* longPathBuffer, int bufferLength);
343 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
344 internal static extern int GetLongPathName(string path, StringBuilder longPathBuffer, int bufferLength);
345 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
346 internal static extern int GetLongPathName([In] char[] path, [Out] char[] longPathBuffer, int bufferLength);
347 internal static string GetMessage(int errorCode)
348 {
349 StringBuilder lpBuffer = new StringBuilder(0x200);
350 if (FormatMessage(0x3200, NULL, errorCode, 0, lpBuffer, lpBuffer.Capacity, NULL) != 0)
351 {
352 return lpBuffer.ToString();
353 }
354 return Environment.GetResourceString("UnknownError_Num", new object[] { errorCode });
355 }
356
357 [DllImport("user32.dll", ExactSpelling=true)]
358 internal static extern IntPtr GetProcessWindowStation();
359 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
360 internal static extern uint GetSecurityDescriptorLength(IntPtr byteArray);
361 [DllImport("advapi32.dll", EntryPoint="GetSecurityInfo", CharSet=CharSet.Unicode, SetLastError=true)]
362 internal static extern uint GetSecurityInfoByHandle(SafeHandle handle, uint objectType, uint securityInformation, out IntPtr sidOwner, out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
363 [DllImport("advapi32.dll", EntryPoint="GetNamedSecurityInfoW", CharSet=CharSet.Unicode, SetLastError=true)]
364 internal static extern uint GetSecurityInfoByName(string name, uint objectType, uint securityInformation, out IntPtr sidOwner, out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
365 [DllImport("kernel32.dll", SetLastError=true)]
366 internal static extern IntPtr GetStdHandle(int nStdHandle);
367 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
368 internal static extern int GetSystemDirectory(StringBuilder sb, int length);
369 [DllImport("kernel32.dll", SetLastError=true)]
370 internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);
371 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
372 internal static extern uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
373 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
374 internal static extern uint GetTempPath(int bufferLen, StringBuilder buffer);
375 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
376 internal static extern bool GetTokenInformation([In] SafeTokenHandle TokenHandle, [In] uint TokenInformationClass, [In] SafeLocalAllocHandle TokenInformation, [In] uint TokenInformationLength, out uint ReturnLength);
377 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
378 internal static extern bool GetTokenInformation([In] IntPtr TokenHandle, [In] uint TokenInformationClass, [In] SafeLocalAllocHandle TokenInformation, [In] uint TokenInformationLength, out uint ReturnLength);
379 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
380 internal static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);
381 [DllImport("secur32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
382 internal static extern byte GetUserNameEx(int format, StringBuilder domainName, ref int domainNameLen);
383 [DllImport("user32.dll", SetLastError=true)]
384 internal static extern bool GetUserObjectInformation(IntPtr hObj, int nIndex, [MarshalAs(UnmanagedType.LPStruct)] USEROBJECTFLAGS pvBuffer, int nLength, ref int lpnLengthNeeded);
385 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
386 internal static extern bool GetVersionEx([In, Out] OSVERSIONINFO ver);
387 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
388 internal static extern bool GetVersionEx([In, Out] OSVERSIONINFOEX ver);
389 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
390 internal static extern bool GetVolumeInformation(string drive, StringBuilder volumeName, int volumeNameBufLen, out int volSerialNumber, out int maxFileNameLen, out int fileSystemFlags, StringBuilder fileSystemName, int fileSystemNameBufLen);
391 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
392 internal static extern int GetWindowsAccountDomainSid(byte[] sid, [Out] byte[] resultSid, ref uint resultSidLength);
393 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
394 internal static extern int GetWindowsDirectory(StringBuilder sb, int length);
395 [DllImport("kernel32.dll", SetLastError=true)]
396 internal static extern bool GlobalMemoryStatus([In, Out] MEMORYSTATUS buffer);
397 [DllImport("kernel32.dll", SetLastError=true)]
398 internal static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX buffer);
399 [DllImport("advapi32.dll", EntryPoint="EqualDomainSid", CharSet=CharSet.Unicode, SetLastError=true)]
400 internal static extern int IsEqualDomainSid(byte[] sid1, byte[] sid2, out bool result);
401 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
402 internal static extern int IsWellKnownSid(byte[] sid, int type);
403 [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
404 internal static extern unsafe int LCMapStringW(int lcid, int flags, char* src, int cchSrc, char* target, int cchTarget);
405 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
406 internal static extern SafeLocalAllocHandle LocalAlloc([In] int uFlags, [In] IntPtr sizetdwBytes);
407 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", EntryPoint="LocalAlloc")]
408 internal static extern IntPtr LocalAlloc_NoSafeHandle(int uFlags, IntPtr sizetdwBytes);
409 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
410 internal static extern IntPtr LocalFree(IntPtr handle);
411 [DllImport("kernel32.dll")]
412 internal static extern IntPtr LocalReAlloc(IntPtr handle, IntPtr sizetcbBytes, int uFlags);
413 [DllImport("kernel32.dll", SetLastError=true)]
414 internal static extern bool LockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
415 [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
416 internal static extern bool LookupAccountName(string machineName, string accountName, byte[] sid, ref int sidLen, StringBuilder domainName, ref int domainNameLen, out int peUse);
417 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("advapi32.dll", EntryPoint="LookupPrivilegeValueW", CharSet=CharSet.Auto, SetLastError=true)]
418 internal static extern bool LookupPrivilegeValue([In] string lpSystemName, [In] string lpName, [In, Out] ref LUID Luid);
419 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", SetLastError=true)]
420 internal static extern int LsaClose(IntPtr handle);
421 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
422 internal static extern int LsaConnectUntrusted([In, Out] ref SafeLsaLogonProcessHandle LsaHandle);
423 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("secur32.dll", SetLastError=true)]
424 internal static extern int LsaDeregisterLogonProcess(IntPtr handle);
425 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", SetLastError=true)]
426 internal static extern int LsaFreeMemory(IntPtr handle);
427 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("secur32.dll", SetLastError=true)]
428 internal static extern int LsaFreeReturnBuffer(IntPtr handle);
429 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
430 internal static extern int LsaGetLogonSessionData([In] ref LUID LogonId, [In, Out] ref SafeLsaReturnBufferHandle ppLogonSessionData);
431 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
432 internal static extern int LsaLogonUser([In] SafeLsaLogonProcessHandle LsaHandle, [In] ref UNICODE_INTPTR_STRING OriginName, [In] uint LogonType, [In] uint AuthenticationPackage, [In] IntPtr AuthenticationInformation, [In] uint AuthenticationInformationLength, [In] IntPtr LocalGroups, [In] ref TOKEN_SOURCE SourceContext, [In, Out] ref SafeLsaReturnBufferHandle ProfileBuffer, [In, Out] ref uint ProfileBufferLength, [In, Out] ref LUID LogonId, [In, Out] ref SafeTokenHandle Token, [In, Out] ref QUOTA_LIMITS Quotas, [In, Out] ref int SubStatus);
433 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
434 internal static extern int LsaLookupAuthenticationPackage([In] SafeLsaLogonProcessHandle LsaHandle, [In] ref UNICODE_INTPTR_STRING PackageName, [In, Out] ref uint AuthenticationPackage);
435 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
436 internal static extern uint LsaLookupNames(SafeLsaPolicyHandle handle, int count, UNICODE_STRING[] names, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle sids);
437 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
438 internal static extern uint LsaLookupNames2(SafeLsaPolicyHandle handle, int flags, int count, UNICODE_STRING[] names, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle sids);
439 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
440 internal static extern uint LsaLookupSids(SafeLsaPolicyHandle handle, int count, IntPtr[] sids, ref SafeLsaMemoryHandle referencedDomains, ref SafeLsaMemoryHandle names);
441 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
442 internal static extern int LsaNtStatusToWinError([In] int status);
443 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
444 internal static extern uint LsaOpenPolicy(string systemName, ref LSA_OBJECT_ATTRIBUTES attributes, int accessMask, out SafeLsaPolicyHandle handle);
445 [DllImport("secur32.dll", CharSet=CharSet.Auto, SetLastError=true)]
446 internal static extern int LsaRegisterLogonProcess([In] ref UNICODE_INTPTR_STRING LogonProcessName, [In, Out] ref SafeLsaLogonProcessHandle LsaHandle, [In, Out] ref IntPtr SecurityMode);
447 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
448 internal static extern IntPtr lstrcpy(IntPtr dst, string src);
449 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
450 internal static extern IntPtr lstrcpy(StringBuilder dst, IntPtr src);
451 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
452 internal static extern int lstrlen(IntPtr ptr);
453 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
454 internal static extern int lstrlen(sbyte[] ptr);
455 [DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
456 internal static extern int lstrlenA(IntPtr ptr);
457 [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
458 internal static extern int lstrlenW(IntPtr ptr);
459 internal static int MakeHRFromErrorCode(int errorCode)
460 {
461 return (-2147024896 | errorCode);
462 }
463
464 [DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
465 internal static extern SafeViewOfFileHandle MapViewOfFile(SafeFileMappingHandle handle, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumerOfBytesToMap);
466 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
467 internal static extern bool MoveFile(string src, string dst);
468 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
469 internal static extern SafeWaitHandle OpenEvent(int desiredAccess, bool inheritHandle, string name);
470 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
471 internal static extern SafeWaitHandle OpenMutex(int desiredAccess, bool inheritHandle, string name);
472 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
473 internal static extern bool OpenProcessToken([In] IntPtr ProcessToken, [In] TokenAccessLevels DesiredAccess, [In, Out] ref SafeTokenHandle TokenHandle);
474 [DllImport("kernel32.dll", SetLastError=true)]
475 internal static extern bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
476 [DllImport("kernel32.dll", SetLastError=true)]
477 internal static extern bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
478 [DllImport("kernel32.dll", SetLastError=true)]
479 internal static extern unsafe bool ReadConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* pBuffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT readRegion);
480 [DllImport("kernel32.dll", SetLastError=true)]
481 internal static extern unsafe int ReadFile(SafeFileHandle handle, byte* bytes, int numBytesToRead, IntPtr numBytesRead_mustBeZero, NativeOverlapped* overlapped);
482 [DllImport("kernel32.dll", SetLastError=true)]
483 internal static extern unsafe int ReadFile(SafeFileHandle handle, byte* bytes, int numBytesToRead, out int numBytesRead, IntPtr mustBeZero);
484 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
485 internal static extern int RegConnectRegistry(string machineName, SafeRegistryHandle key, out SafeRegistryHandle result);
486 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
487 internal static extern int RegCreateKeyEx(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition);
488 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
489 internal static extern int RegDeleteKey(SafeRegistryHandle hKey, string lpSubKey);
490 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
491 internal static extern int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName);
492 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
493 internal static extern int RegEnumKeyEx(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpName, out int lpcbName, int[] lpReserved, StringBuilder lpClass, int[] lpcbClass, long[] lpftLastWriteTime);
494 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
495 internal static extern int RegEnumValue(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData);
496 [DllImport("advapi32.dll", CharSet=CharSet.Ansi)]
497 internal static extern int RegEnumValueA(SafeRegistryHandle hKey, int dwIndex, StringBuilder lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData);
498 [DllImport("advapi32.dll")]
499 internal static extern int RegFlushKey(SafeRegistryHandle hKey);
500 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
501 internal static extern int RegOpenKeyEx(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
502 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
503 internal static extern int RegQueryInfoKey(SafeRegistryHandle hKey, StringBuilder lpClass, int[] lpcbClass, IntPtr lpReserved_MustBeZero, ref int lpcSubKeys, int[] lpcbMaxSubKeyLen, int[] lpcbMaxClassLen, ref int lpcValues, int[] lpcbMaxValueNameLen, int[] lpcbMaxValueLen, int[] lpcbSecurityDescriptor, int[] lpftLastWriteTime);
504 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
505 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, StringBuilder lpData, ref int lpcbData);
506 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
507 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] char[] lpData, ref int lpcbData);
508 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
509 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, ref int lpData, ref int lpcbData);
510 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
511 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] byte[] lpData, ref int lpcbData);
512 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
513 internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, ref long lpData, ref int lpcbData);
514 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
515 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, ref int lpData, int cbData);
516 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
517 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, byte[] lpData, int cbData);
518 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
519 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, string lpData, int cbData);
520 [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
521 internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, RegistryValueKind dwType, ref long lpData, int cbData);
522 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
523 internal static extern bool ReleaseMutex(SafeWaitHandle handle);
524 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
525 internal static extern bool RemoveDirectory(string path);
526 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
527 internal static extern bool ReplaceFile(string replacedFileName, string replacementFileName, string backupFileName, int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved);
528 [DllImport("kernel32.dll", SetLastError=true)]
529 internal static extern bool ResetEvent(SafeWaitHandle handle);
530 internal static SafeFileHandle SafeCreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
531 {
532 SafeFileHandle handle = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
533 if (!handle.IsInvalid && (GetFileType(handle) != 1))
534 {
535 handle.Dispose();
536 throw new NotSupportedException(Environment.GetResourceString("NotSupported_FileStreamOnNonFiles"));
537 }
538 return handle;
539 }
540
541 [DllImport("user32.dll", SetLastError=true)]
542 internal static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);
543 [DllImport("kernel32.dll", SetLastError=true)]
544 internal static extern bool SetConsoleCP(uint codePage);
545 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
546 internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandlerRoutine handler, bool addOrRemove);
547 [DllImport("kernel32.dll", SetLastError=true)]
548 internal static extern bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref CONSOLE_CURSOR_INFO cci);
549 [DllImport("kernel32.dll", SetLastError=true)]
550 internal static extern bool SetConsoleCursorPosition(IntPtr hConsoleOutput, COORD cursorPosition);
551 [DllImport("kernel32.dll", SetLastError=true)]
552 internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
553 [DllImport("kernel32.dll", SetLastError=true)]
554 internal static extern bool SetConsoleOutputCP(uint codePage);
555 [DllImport("kernel32.dll", SetLastError=true)]
556 internal static extern bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, COORD size);
557 [DllImport("kernel32.dll", SetLastError=true)]
558 internal static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, short attributes);
559 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
560 internal static extern bool SetConsoleTitle(string title);
561 [DllImport("kernel32.dll", SetLastError=true)]
562 internal static extern unsafe bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool absolute, SMALL_RECT* consoleWindow);
563 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
564 internal static extern bool SetCurrentDirectory(string path);
565 [DllImport("kernel32.dll", SetLastError=true)]
566 internal static extern bool SetEndOfFile(SafeFileHandle hFile);
567 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
568 internal static extern bool SetEnvironmentVariable(string lpName, string lpValue);
569 [DllImport("kernel32.dll")]
570 internal static extern int SetErrorMode(int newMode);
571 [DllImport("kernel32.dll", SetLastError=true)]
572 internal static extern bool SetEvent(SafeWaitHandle handle);
573 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
574 internal static extern bool SetFileAttributes(string name, int attr);
575 internal static unsafe long SetFilePointer(SafeFileHandle handle, long offset, SeekOrigin origin, out int hr)
576 {
577 hr = 0;
578 int lo = (int) offset;
579 int hi = (int) (offset >> 0x20);
580 lo = SetFilePointerWin32(handle, lo, &hi, (int) origin);
581 if ((lo == -1) && ((hr = Marshal.GetLastWin32Error()) != 0))
582 {
583 return -1L;
584 }
585 return (long) ((((ulong) hi) << 0x20) | ((ulong) lo));
586 }
587
588 [DllImport("kernel32.dll", EntryPoint="SetFilePointer", SetLastError=true)]
589 private static extern unsafe int SetFilePointerWin32(SafeFileHandle handle, int lo, int* hi, int origin);
590 [DllImport("kernel32.dll", SetLastError=true)]
591 internal static extern unsafe bool SetFileTime(SafeFileHandle hFile, FILE_TIME* creationTime, FILE_TIME* lastAccessTime, FILE_TIME* lastWriteTime);
592 [DllImport("kernel32.dll", SetLastError=true)]
593 internal static extern void SetLastError(int errorCode);
594 [DllImport("advapi32.dll", EntryPoint="SetSecurityInfo", CharSet=CharSet.Unicode, SetLastError=true)]
595 internal static extern uint SetSecurityInfoByHandle(SafeHandle handle, uint objectType, uint securityInformation, byte[] owner, byte[] group, byte[] dacl, byte[] sacl);
596 [DllImport("advapi32.dll", EntryPoint="SetNamedSecurityInfoW", CharSet=CharSet.Unicode, SetLastError=true)]
597 internal static extern uint SetSecurityInfoByName(string name, uint objectType, uint securityInformation, byte[] owner, byte[] group, byte[] dacl, byte[] sacl);
598 [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
599 internal static extern bool SetVolumeLabel(string driveLetter, string volumeName);
600 [DllImport("shfolder.dll", CharSet=CharSet.Auto)]
601 internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
602 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("oleaut32.dll", CharSet=CharSet.Unicode)]
603 internal static extern IntPtr SysAllocStringLen(string src, int len);
604 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("oleaut32.dll")]
605 internal static extern void SysFreeString(IntPtr bstr);
606 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("oleaut32.dll")]
607 internal static extern int SysStringLen(IntPtr bstr);
608 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
609 internal static extern int SystemFunction040([In, Out] SafeBSTRHandle pDataIn, [In] uint cbDataIn, [In] uint dwFlags);
610 [DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
611 internal static extern int SystemFunction041([In, Out] SafeBSTRHandle pDataIn, [In] uint cbDataIn, [In] uint dwFlags);
612 [DllImport("kernel32.dll", SetLastError=true)]
613 internal static extern bool UnlockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh);
614 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", ExactSpelling=true)]
615 internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
616 internal static SafeFileHandle UnsafeCreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
617 {
618 return CreateFile(lpFileName, dwDesiredAccess, dwShareMode, securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
619 }
620
621 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
622 internal static extern unsafe void* VirtualAlloc(void* address, UIntPtr numBytes, int commitOrReserve, int pageProtectionMode);
623 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), DllImport("kernel32.dll", SetLastError=true)]
624 internal static extern unsafe bool VirtualFree(void* address, UIntPtr numBytes, int pageFreeMode);
625 [DllImport("kernel32.dll", SetLastError=true)]
626 internal static extern unsafe IntPtr VirtualQuery(void* address, ref MEMORY_BASIC_INFORMATION buffer, IntPtr sizeOfBuffer);
627 [DllImport("kernel32.dll")]
628 internal static extern unsafe int WideCharToMultiByte(uint cp, uint flags, char* pwzSource, int cchSource, byte* pbDestBuffer, int cbDestBuffer, IntPtr null1, IntPtr null2);
629 [DllImport("kernel32.dll", SetLastError=true)]
630 internal static extern unsafe bool WriteConsoleOutput(IntPtr hConsoleOutput, CHAR_INFO* buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
631 [DllImport("kernel32.dll", SetLastError=true)]
632 internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
633 [DllImport("kernel32.dll", SetLastError=true)]
634 internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
635 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", SetLastError=true)]
636 internal static extern void ZeroMemory(IntPtr handle, uint length);
637
638 [StructLayout(LayoutKind.Sequential)]
639 internal struct CHAR_INFO
640 {
641 private ushort charData;
642 private short attributes;
643 }
644
645 [Serializable, Flags]
646 internal enum Color : short
647 {
648 BackgroundBlue = 0x10,
649 BackgroundGreen = 0x20,
650 BackgroundIntensity = 0x80,
651 BackgroundMask = 240,
652 BackgroundRed = 0x40,
653 BackgroundYellow = 0x60,
654 Black = 0,
655 ColorMask = 0xff,
656 ForegroundBlue = 1,
657 ForegroundGreen = 2,
658 ForegroundIntensity = 8,
659 ForegroundMask = 15,
660 ForegroundRed = 4,
661 ForegroundYellow = 6
662 }
663
664 [StructLayout(LayoutKind.Sequential)]
665 internal struct CONSOLE_CURSOR_INFO
666 {
667 internal int dwSize;
668 internal bool bVisible;
669 }
670
671 [StructLayout(LayoutKind.Sequential)]
672 internal struct CONSOLE_SCREEN_BUFFER_INFO
673 {
674 internal Win32Native.COORD dwSize;
675 internal Win32Native.COORD dwCursorPosition;
676 internal short wAttributes;
677 internal Win32Native.SMALL_RECT srWindow;
678 internal Win32Native.COORD dwMaximumWindowSize;
679 }
680
681 internal delegate bool ConsoleCtrlHandlerRoutine(int controlType);
682
683 [StructLayout(LayoutKind.Sequential)]
684 internal struct COORD
685 {
686 internal short X;
687 internal short Y;
688 }
689
690 [StructLayout(LayoutKind.Sequential)]
691 internal struct FILE_TIME
692 {
693 internal uint ftTimeLow;
694 internal uint ftTimeHigh;
695 public FILE_TIME(long fileTime)
696 {
697 this.ftTimeLow = (uint) fileTime;
698 this.ftTimeHigh = (uint) (fileTime >> 0x20);
699 }
700
701 public long ToTicks()
702 {
703 return (long) ((this.ftTimeHigh << 0x20) + this.ftTimeLow);
704 }
705 }
706
707 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
708 internal struct InputRecord
709 {
710 internal short eventType;
711 internal Win32Native.KeyEventRecord keyEvent;
712 }
713
714 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
715 internal struct KERB_S4U_LOGON
716 {
717 internal uint MessageType;
718 internal uint Flags;
719 internal Win32Native.UNICODE_INTPTR_STRING ClientUpn;
720 internal Win32Native.UNICODE_INTPTR_STRING ClientRealm;
721 }
722
723 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
724 internal struct KeyEventRecord
725 {
726 internal bool keyDown;
727 internal short repeatCount;
728 internal short virtualKeyCode;
729 internal short virtualScanCode;
730 internal char uChar;
731 internal int controlKeyState;
732 }
733
734 [StructLayout(LayoutKind.Sequential)]
735 internal struct LSA_OBJECT_ATTRIBUTES
736 {
737 internal int Length;
738 internal IntPtr RootDirectory;
739 internal IntPtr ObjectName;
740 internal int Attributes;
741 internal IntPtr SecurityDescriptor;
742 internal IntPtr SecurityQualityOfService;
743 }
744
745 [StructLayout(LayoutKind.Sequential)]
746 internal struct LSA_REFERENCED_DOMAIN_LIST
747 {
748 internal int Entries;
749 internal IntPtr Domains;
750 }
751
752 [StructLayout(LayoutKind.Sequential)]
753 internal struct LSA_TRANSLATED_NAME
754 {
755 internal int Use;
756 internal Win32Native.UNICODE_INTPTR_STRING Name;
757 internal int DomainIndex;
758 }
759
760 [StructLayout(LayoutKind.Sequential)]
761 internal struct LSA_TRANSLATED_SID
762 {
763 internal int Use;
764 internal uint Rid;
765 internal int DomainIndex;
766 }
767
768 [StructLayout(LayoutKind.Sequential)]
769 internal struct LSA_TRANSLATED_SID2
770 {
771 internal int Use;
772 internal IntPtr Sid;
773 internal int DomainIndex;
774 private uint Flags;
775 }
776
777 [StructLayout(LayoutKind.Sequential)]
778 internal struct LSA_TRUST_INFORMATION
779 {
780 internal Win32Native.UNICODE_INTPTR_STRING Name;
781 internal IntPtr Sid;
782 }
783
784 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
785 internal struct LUID
786 {
787 internal uint LowPart;
788 internal uint HighPart;
789 }
790
791 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
792 internal struct LUID_AND_ATTRIBUTES
793 {
794 internal Win32Native.LUID Luid;
795 internal uint Attributes;
796 }
797
798 [StructLayout(LayoutKind.Sequential)]
799 internal struct MEMORY_BASIC_INFORMATION
800 {
801 internal unsafe void* BaseAddress;
802 internal unsafe void* AllocationBase;
803 internal uint AllocationProtect;
804 internal UIntPtr RegionSize;
805 internal uint State;
806 internal uint Protect;
807 internal uint Type;
808 }
809
810 [StructLayout(LayoutKind.Sequential)]
811 internal class MEMORYSTATUS
812 {
813 internal int length;
814 internal int memoryLoad;
815 internal uint totalPhys;
816 internal uint availPhys;
817 internal uint totalPageFile;
818 internal uint availPageFile;
819 internal uint totalVirtual;
820 internal uint availVirtual;
821 internal MEMORYSTATUS()
822 {
823 this.length = Marshal.SizeOf(this);
824 }
825 }
826
827 [StructLayout(LayoutKind.Sequential)]
828 internal class MEMORYSTATUSEX
829 {
830 internal int length;
831 internal int memoryLoad;
832 internal ulong totalPhys;
833 internal ulong availPhys;
834 internal ulong totalPageFile;
835 internal ulong availPageFile;
836 internal ulong totalVirtual;
837 internal ulong availVirtual;
838 internal ulong availExtendedVirtual;
839 internal MEMORYSTATUSEX()
840 {
841 this.length = Marshal.SizeOf(this);
842 }
843 }
844
845 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
846 internal class OSVERSIONINFO
847 {
848 internal int OSVersionInfoSize;
849 internal int MajorVersion;
850 internal int MinorVersion;
851 internal int BuildNumber;
852 internal int PlatformId;
853 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
854 internal string CSDVersion;
855 internal OSVERSIONINFO()
856 {
857 this.OSVersionInfoSize = Marshal.SizeOf(this);
858 }
859 }
860
861 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
862 internal class OSVERSIONINFOEX
863 {
864 internal int OSVersionInfoSize;
865 internal int MajorVersion;
866 internal int MinorVersion;
867 internal int BuildNumber;
868 internal int PlatformId;
869 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
870 internal string CSDVersion;
871 internal ushort ServicePackMajor;
872 internal ushort ServicePackMinor;
873 internal short SuiteMask;
874 internal byte ProductType;
875 internal byte Reserved;
876 public OSVERSIONINFOEX()
877 {
878 this.OSVersionInfoSize = Marshal.SizeOf(this);
879 }
880 }
881
882 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
883 internal struct QUOTA_LIMITS
884 {
885 internal IntPtr PagedPoolLimit;
886 internal IntPtr NonPagedPoolLimit;
887 internal IntPtr MinimumWorkingSetSize;
888 internal IntPtr MaximumWorkingSetSize;
889 internal IntPtr PagefileLimit;
890 internal IntPtr TimeLimit;
891 }
892
893 [StructLayout(LayoutKind.Sequential)]
894 internal class SECURITY_ATTRIBUTES
895 {
896 internal int nLength;
897 internal unsafe byte* pSecurityDescriptor = null;
898 internal int bInheritHandle;
899 }
900
901 internal enum SECURITY_IMPERSONATION_LEVEL
902 {
903 Anonymous,
904 Identification,
905 Impersonation,
906 Delegation
907 }
908
909 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
910 internal struct SECURITY_LOGON_SESSION_DATA
911 {
912 internal uint Size;
913 internal Win32Native.LUID LogonId;
914 internal Win32Native.UNICODE_INTPTR_STRING UserName;
915 internal Win32Native.UNICODE_INTPTR_STRING LogonDomain;
916 internal Win32Native.UNICODE_INTPTR_STRING AuthenticationPackage;
917 internal uint LogonType;
918 internal uint Session;
919 internal IntPtr Sid;
920 internal long LogonTime;
921 }
922
923 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
924 internal struct SID_AND_ATTRIBUTES
925 {
926 internal IntPtr Sid;
927 internal uint Attributes;
928 }
929
930 [StructLayout(LayoutKind.Sequential)]
931 internal struct SMALL_RECT
932 {
933 internal short Left;
934 internal short Top;
935 internal short Right;
936 internal short Bottom;
937 }
938
939 [StructLayout(LayoutKind.Sequential)]
940 internal struct SYSTEM_INFO
941 {
942 internal int dwOemId;
943 internal int dwPageSize;
944 internal IntPtr lpMinimumApplicationAddress;
945 internal IntPtr lpMaximumApplicationAddress;
946 internal IntPtr dwActiveProcessorMask;
947 internal int dwNumberOfProcessors;
948 internal int dwProcessorType;
949 internal int dwAllocationGranularity;
950 internal short wProcessorLevel;
951 internal short wProcessorRevision;
952 }
953
954 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
955 internal struct TOKEN_GROUPS
956 {
957 internal uint GroupCount;
958 internal Win32Native.SID_AND_ATTRIBUTES Groups;
959 }
960
961 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
962 internal struct TOKEN_PRIVILEGE
963 {
964 internal uint PrivilegeCount;
965 internal Win32Native.LUID_AND_ATTRIBUTES Privilege;
966 }
967
968 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
969 internal struct TOKEN_SOURCE
970 {
971 private const int TOKEN_SOURCE_LENGTH = 8;
972 [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
973 internal char[] Name;
974 internal Win32Native.LUID SourceIdentifier;
975 }
976
977 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
978 internal struct TOKEN_STATISTICS
979 {
980 internal Win32Native.LUID TokenId;
981 internal Win32Native.LUID AuthenticationId;
982 internal long ExpirationTime;
983 internal uint TokenType;
984 internal uint ImpersonationLevel;
985 internal uint DynamicCharged;
986 internal uint DynamicAvailable;
987 internal uint GroupCount;
988 internal uint PrivilegeCount;
989 internal Win32Native.LUID ModifiedId;
990 }
991
992 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
993 internal struct TOKEN_USER
994 {
995 internal Win32Native.SID_AND_ATTRIBUTES User;
996 }
997
998 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
999 internal struct UNICODE_INTPTR_STRING
1000 {
1001 internal ushort Length;
1002 internal ushort MaxLength;
1003 internal IntPtr Buffer;
1004 internal UNICODE_INTPTR_STRING(int length, int maximumLength, IntPtr buffer)
1005 {
1006 this.Length = (ushort) length;
1007 this.MaxLength = (ushort) maximumLength;
1008 this.Buffer = buffer;
1009 }
1010 }
1011
1012 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
1013 internal struct UNICODE_STRING
1014 {
1015 internal ushort Length;
1016 internal ushort MaximumLength;
1017 [MarshalAs(UnmanagedType.LPWStr)]
1018 internal string Buffer;
1019 }
1020
1021 [StructLayout(LayoutKind.Sequential)]
1022 internal class USEROBJECTFLAGS
1023 {
1024 internal int fInherit;
1025 internal int fReserved;
1026 internal int dwFlags;
1027 }
1028
1029 [Serializable, StructLayout(LayoutKind.Sequential)]
1030 internal struct WIN32_FILE_ATTRIBUTE_DATA
1031 {
1032 internal int fileAttributes;
1033 internal uint ftCreationTimeLow;
1034 internal uint ftCreationTimeHigh;
1035 internal uint ftLastAccessTimeLow;
1036 internal uint ftLastAccessTimeHigh;
1037 internal uint ftLastWriteTimeLow;
1038 internal uint ftLastWriteTimeHigh;
1039 internal int fileSizeHigh;
1040 internal int fileSizeLow;
1041 }
1042
1043 [Serializable, StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto), BestFitMapping(false)]
1044 internal class WIN32_FIND_DATA
1045 {
1046 internal int dwFileAttributes;
1047 internal int ftCreationTime_dwLowDateTime;
1048 internal int ftCreationTime_dwHighDateTime;
1049 internal int ftLastAccessTime_dwLowDateTime;
1050 internal int ftLastAccessTime_dwHighDateTime;
1051 internal int ftLastWriteTime_dwLowDateTime;
1052 internal int ftLastWriteTime_dwHighDateTime;
1053 internal int nFileSizeHigh;
1054 internal int nFileSizeLow;
1055 internal int dwReserved0;
1056 internal int dwReserved1;
1057 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
1058 internal string cFileName;
1059 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=14)]
1060 internal string cAlternateFileName;
1061 }
1062 }
1063 }
1064