Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
3
4internal static class Interop
5{
6 internal static class Kernel32
7 {
8 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "GetDriveTypeW", SetLastError = true)]
9 internal static extern int GetDriveType(string drive);
10
11 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeInformationW", SetLastError = true)]
12 internal unsafe static extern bool GetVolumeInformation(string drive, char* volumeName, int volumeNameBufLen, int* volSerialNumber, int* maxFileNameLen, out int fileSystemFlags, char* fileSystemName, int fileSystemNameBufLen);
13
14 [DllImport("kernel32.dll", SetLastError = true)]
15 internal static extern int GetLogicalDrives();
16
17 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "GetDiskFreeSpaceExW", SetLastError = true)]
18 internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);
19
20 [DllImport("kernel32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "SetVolumeLabelW", SetLastError = true)]
21 internal static extern bool SetVolumeLabel(string driveLetter, string volumeName);
22
23 [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
24 [SuppressGCTransition]
25 internal static extern bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode);
26
27 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
28 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
29
30 internal static string GetMessage(int errorCode)
31 {
32 return GetMessage(errorCode, IntPtr.Zero);
33 }
34
35 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
36 {
37 int num = 12800;
38 if (moduleHandle != IntPtr.Zero)
39 {
40 num |= 0x800;
41 }
42 Span<char> span = stackalloc char[256];
43 fixed (char* lpBuffer = span)
44 {
45 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
46 if (num2 > 0)
47 {
48 return GetAndTrimString(span.Slice(0, num2));
49 }
50 }
51 if (Marshal.GetLastWin32Error() == 122)
52 {
53 IntPtr intPtr = default(IntPtr);
54 try
55 {
56 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
57 if (num3 > 0)
58 {
59 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
60 }
61 }
62 finally
63 {
64 Marshal.FreeHGlobal(intPtr);
65 }
66 }
67 return $"Unknown error (0x{errorCode:x})";
68 }
69
70 private static string GetAndTrimString(Span<char> buffer)
71 {
72 int num = buffer.Length;
73 while (num > 0 && buffer[num - 1] <= ' ')
74 {
75 num--;
76 }
77 return buffer.Slice(0, num).ToString();
78 }
79 }
80}
static unsafe bool GetVolumeInformation(string drive, char *volumeName, int volumeNameBufLen, int *volSerialNumber, int *maxFileNameLen, out int fileSystemFlags, char *fileSystemName, int fileSystemNameBufLen)
static int GetLogicalDrives()
static bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:35
static void SetLastError(int errorCode)
static bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode)
static string GetMessage(int errorCode)
Definition Interop.cs:30
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static int GetDriveType(string drive)
static bool SetVolumeLabel(string driveLetter, string volumeName)
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70