-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathNativeMethods.Windows.cs
More file actions
160 lines (132 loc) · 4.26 KB
/
Copy pathNativeMethods.Windows.cs
File metadata and controls
160 lines (132 loc) · 4.26 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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using ReClassNET.Util;
namespace ReClassNET.Native
{
internal class NativeMethodsWindows : INativeMethods
{
#region Imports
[DllImport("kernel32.dll", ExactSpelling = true)]
public 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);
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
public const uint SHGFI_SMALLICON = 0x1;
[StructLayout(LayoutKind.Sequential)]
public 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)]
public struct LUID
{
public uint LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public 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);
#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()
{
IntPtr token;
if (OpenProcessToken(System.Diagnostics.Process.GetCurrentProcess().Handle, TokenAccessLevels.AllAccess, out 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();
}
}
}
}