|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Security.Principal; |
| 5 | +using System.Text; |
| 6 | +using ReClassNET.Util; |
| 7 | + |
| 8 | +namespace ReClassNET.Native |
| 9 | +{ |
| 10 | + internal class NativeMethodsWindows : INativeMethods |
| 11 | + { |
| 12 | + #region Imports |
| 13 | + |
| 14 | + [DllImport("kernel32.dll", ExactSpelling = true)] |
| 15 | + public static extern bool CloseHandle(IntPtr hObject); |
| 16 | + |
| 17 | + [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] |
| 18 | + private static extern IntPtr LoadLibrary(string lpFileName); |
| 19 | + |
| 20 | + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] |
| 21 | + private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); |
| 22 | + |
| 23 | + [DllImport("kernel32.dll", ExactSpelling = true)] |
| 24 | + private static extern bool FreeLibrary(IntPtr hModule); |
| 25 | + |
| 26 | + public const uint SHGFI_ICON = 0x100; |
| 27 | + public const uint SHGFI_LARGEICON = 0x0; |
| 28 | + public const uint SHGFI_SMALLICON = 0x1; |
| 29 | + |
| 30 | + [StructLayout(LayoutKind.Sequential)] |
| 31 | + public struct SHFILEINFO |
| 32 | + { |
| 33 | + public IntPtr hIcon; |
| 34 | + public IntPtr iIcon; |
| 35 | + public uint dwAttributes; |
| 36 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] |
| 37 | + public string szDisplayName; |
| 38 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] |
| 39 | + public string szTypeName; |
| 40 | + }; |
| 41 | + |
| 42 | + [StructLayout(LayoutKind.Sequential, Pack = 1)] |
| 43 | + public struct LUID |
| 44 | + { |
| 45 | + public uint LowPart; |
| 46 | + public int HighPart; |
| 47 | + } |
| 48 | + |
| 49 | + [StructLayout(LayoutKind.Sequential, Pack = 1)] |
| 50 | + public struct TOKEN_PRIVILEGES |
| 51 | + { |
| 52 | + public uint PrivilegeCount; |
| 53 | + public LUID Luid; |
| 54 | + public uint Attributes; |
| 55 | + } |
| 56 | + |
| 57 | + [DllImport("shell32.dll")] |
| 58 | + private static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbSizeFileInfo, uint uFlags); |
| 59 | + |
| 60 | + [DllImport("user32.dll", ExactSpelling = true)] |
| 61 | + private static extern int DestroyIcon(IntPtr hIcon); |
| 62 | + |
| 63 | + [DllImport("advapi32.dll", ExactSpelling = true)] |
| 64 | + private static extern bool OpenProcessToken(IntPtr ProcessHandle, TokenAccessLevels DesiredAccess, out IntPtr TokenHandle); |
| 65 | + |
| 66 | + [DllImport("advapi32.dll", ExactSpelling = true)] |
| 67 | + private static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, uint Zero, IntPtr Null1, IntPtr Null2); |
| 68 | + |
| 69 | + [DllImport("dbghelp.dll", CharSet = CharSet.Unicode)] |
| 70 | + private static extern int UnDecorateSymbolName(string DecoratedName, StringBuilder UnDecoratedName, int UndecoratedLength, int Flags); |
| 71 | + |
| 72 | + [DllImport("user32.dll")] |
| 73 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 74 | + private static extern bool SetProcessDPIAware(); |
| 75 | + |
| 76 | + private enum ProcessDpiAwareness : uint |
| 77 | + { |
| 78 | + Unaware = 0, |
| 79 | + SystemAware = 1, |
| 80 | + PerMonitorAware = 2 |
| 81 | + } |
| 82 | + |
| 83 | + [DllImport("shcore.dll")] |
| 84 | + private static extern int SetProcessDpiAwareness([MarshalAs(UnmanagedType.U4)] ProcessDpiAwareness a); |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + IntPtr INativeMethods.LoadLibrary(string fileName) |
| 89 | + { |
| 90 | + return LoadLibrary(fileName); |
| 91 | + } |
| 92 | + |
| 93 | + IntPtr INativeMethods.GetProcAddress(IntPtr handle, string name) |
| 94 | + { |
| 95 | + return GetProcAddress(handle, name); |
| 96 | + } |
| 97 | + |
| 98 | + void INativeMethods.FreeLibrary(IntPtr handle) |
| 99 | + { |
| 100 | + FreeLibrary(handle); |
| 101 | + } |
| 102 | + |
| 103 | + public Icon GetIconForFile(string path) |
| 104 | + { |
| 105 | + var shinfo = new SHFILEINFO(); |
| 106 | + if (!SHGetFileInfo(path, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON).IsNull()) |
| 107 | + { |
| 108 | + var icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon; |
| 109 | + DestroyIcon(shinfo.hIcon); |
| 110 | + return icon; |
| 111 | + } |
| 112 | + |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + public void EnableDebugPrivileges() |
| 117 | + { |
| 118 | + IntPtr token; |
| 119 | + if (OpenProcessToken(System.Diagnostics.Process.GetCurrentProcess().Handle, TokenAccessLevels.AllAccess, out token)) |
| 120 | + { |
| 121 | + var privileges = new TOKEN_PRIVILEGES(); |
| 122 | + privileges.PrivilegeCount = 1; |
| 123 | + privileges.Luid.LowPart = 0x14; |
| 124 | + privileges.Luid.HighPart = 0; |
| 125 | + privileges.Attributes = 2; |
| 126 | + |
| 127 | + AdjustTokenPrivileges(token, false, ref privileges, 0, IntPtr.Zero, IntPtr.Zero); |
| 128 | + |
| 129 | + CloseHandle(token); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public string UndecorateSymbolName(string name) |
| 134 | + { |
| 135 | + var sb = new StringBuilder(255); |
| 136 | + if (UnDecorateSymbolName(name, sb, sb.Capacity, /*UNDNAME_NAME_ONLY*/0x1000) != 0) |
| 137 | + { |
| 138 | + return sb.ToString(); |
| 139 | + } |
| 140 | + return name; |
| 141 | + } |
| 142 | + |
| 143 | + public void SetProcessDpiAwareness() |
| 144 | + { |
| 145 | + if (WinUtil.IsAtLeastWindows10) |
| 146 | + { |
| 147 | + SetProcessDpiAwareness(ProcessDpiAwareness.SystemAware); |
| 148 | + } |
| 149 | + else if (WinUtil.IsAtLeastWindowsVista) |
| 150 | + { |
| 151 | + SetProcessDPIAware(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments