-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathNativeMethods.Windows.cs
More file actions
272 lines (224 loc) · 6.86 KB
/
NativeMethods.Windows.cs
File metadata and controls
272 lines (224 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using ReClassNET.Extensions;
using ReClassNET.Util;
namespace ReClassNET.Native
{
internal class NativeMethodsWindows : INativeMethods
{
#region Imports
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern bool FreeLibrary(IntPtr hModule);
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x1;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct LUID
{
public uint LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
public LUID Luid;
public uint Attributes;
}
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbSizeFileInfo, uint uFlags);
[DllImport("user32.dll", ExactSpelling = true)]
private static extern int DestroyIcon(IntPtr hIcon);
[DllImport("advapi32.dll", ExactSpelling = true)]
private static extern bool OpenProcessToken(IntPtr ProcessHandle, TokenAccessLevels DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", ExactSpelling = true)]
private static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, uint Zero, IntPtr Null1, IntPtr Null2);
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode)]
private static extern int UnDecorateSymbolName(string DecoratedName, StringBuilder UnDecoratedName, int UndecoratedLength, int Flags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetProcessDPIAware();
private enum ProcessDpiAwareness : uint
{
Unaware = 0,
SystemAware = 1,
PerMonitorAware = 2
}
[DllImport("shcore.dll")]
private static extern int SetProcessDpiAwareness([MarshalAs(UnmanagedType.U4)] ProcessDpiAwareness a);
[DllImport("shell32.dll")]
private static extern void SHChangeNotify(int wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
private const int SHCNE_ASSOCCHANGED = 0x08000000;
private const uint SHCNF_IDLIST = 0x0000;
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);
private const int BCM_SETSHIELD = 0x160C;
#endregion
IntPtr INativeMethods.LoadLibrary(string fileName)
{
return LoadLibrary(fileName);
}
IntPtr INativeMethods.GetProcAddress(IntPtr handle, string name)
{
return GetProcAddress(handle, name);
}
void INativeMethods.FreeLibrary(IntPtr handle)
{
FreeLibrary(handle);
}
public Icon GetIconForFile(string path)
{
var shinfo = new SHFILEINFO();
if (!SHGetFileInfo(path, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON).IsNull())
{
var icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon;
DestroyIcon(shinfo.hIcon);
return icon;
}
return null;
}
public void EnableDebugPrivileges()
{
if (OpenProcessToken(System.Diagnostics.Process.GetCurrentProcess().Handle, TokenAccessLevels.AllAccess, out var token))
{
var privileges = new TOKEN_PRIVILEGES
{
PrivilegeCount = 1,
Luid =
{
LowPart = 0x14,
HighPart = 0
},
Attributes = 2
};
AdjustTokenPrivileges(token, false, ref privileges, 0, IntPtr.Zero, IntPtr.Zero);
CloseHandle(token);
}
}
public string UndecorateSymbolName(string name)
{
var sb = new StringBuilder(255);
if (UnDecorateSymbolName(name, sb, sb.Capacity, /*UNDNAME_NAME_ONLY*/0x1000) != 0)
{
return sb.ToString();
}
return name;
}
public void SetProcessDpiAwareness()
{
if (WinUtil.IsAtLeastWindows10)
{
SetProcessDpiAwareness(ProcessDpiAwareness.SystemAware);
}
else if (WinUtil.IsAtLeastWindowsVista)
{
SetProcessDPIAware();
}
}
public bool RegisterExtension(string fileExtension, string extensionId, string applicationPath, string applicationName)
{
try
{
var classesRoot = Registry.ClassesRoot;
using (var fileExtensionKey = classesRoot.CreateSubKey(fileExtension))
{
fileExtensionKey?.SetValue(string.Empty, extensionId, RegistryValueKind.String);
}
using (var extensionInfoKey = classesRoot.CreateSubKey(extensionId))
{
extensionInfoKey?.SetValue(string.Empty, applicationName, RegistryValueKind.String);
using (var icon = extensionInfoKey?.CreateSubKey("DefaultIcon"))
{
icon?.SetValue(string.Empty, "\"" + applicationPath + "\",0", RegistryValueKind.String);
}
using (var shellKey = extensionInfoKey?.CreateSubKey("shell"))
{
using (var openKey = shellKey?.CreateSubKey("open"))
{
openKey?.SetValue(string.Empty, $"&Open with {applicationName}", RegistryValueKind.String);
using (var commandKey = openKey?.CreateSubKey("command"))
{
commandKey?.SetValue(string.Empty, $"\"{applicationPath}\" \"%1\"", RegistryValueKind.String);
}
}
}
}
ShChangeNotify();
return true;
}
catch (Exception)
{
return false;
}
}
public void UnregisterExtension(string fileExtension, string extensionId)
{
try
{
var classesRoot = Registry.ClassesRoot;
classesRoot.DeleteSubKeyTree(fileExtension);
classesRoot.DeleteSubKeyTree(extensionId);
ShChangeNotify();
}
catch
{
// ignored
}
}
private static void ShChangeNotify()
{
try
{
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
}
catch
{
// ignored
}
}
public static void SetButtonShield(Button button, bool setShield)
{
Contract.Requires(button != null);
try
{
if (button.FlatStyle != FlatStyle.System)
{
button.FlatStyle = FlatStyle.System;
}
var h = button.Handle;
if (h == IntPtr.Zero)
{
return;
}
SendMessage(h, BCM_SETSHIELD, IntPtr.Zero, (IntPtr)(setShield ? 1 : 0));
}
catch
{
// ignored
}
}
}
}