Skip to content

Commit 5d37af1

Browse files
committed
Preparation to use ReClass.NET on unix systems.
1 parent ae2590e commit 5d37af1

File tree

12 files changed

+400
-197
lines changed

12 files changed

+400
-197
lines changed

Forms/ProcessBrowserForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void RefreshProcessList()
122122
if (!filterCheckBox.Checked || !CommonProcesses.Contains(moduleName.ToLower()))
123123
{
124124
var row = dt.NewRow();
125-
row["icon"] = ShellIcon.GetSmallIcon(data.Path);
125+
row["icon"] = NativeMethods.GetIconForFile(data.Path);
126126
row["name"] = moduleName;
127127
row["id"] = data.Id;
128128
row["path"] = data.Path;

Forms/ProcessInfoForm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Windows.Forms;
88
using ReClassNET.Memory;
9+
using ReClassNET.Native;
910
using ReClassNET.Nodes;
1011
using ReClassNET.UI;
1112

@@ -73,7 +74,7 @@ public ProcessInfoForm(RemoteProcess process, ClassNodeView classesView)
7374
delegate (Module module)
7475
{
7576
var row = modules.NewRow();
76-
row["icon"] = ShellIcon.GetSmallIcon(module.Path);
77+
row["icon"] = NativeMethods.GetIconForFile(module.Path);
7778
row["name"] = module.Name;
7879
row["address"] = module.Start.ToString(Constants.StringHexFormat);
7980
row["size"] = module.Size.ToString(Constants.StringHexFormat);

Memory/RemoteProcess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private string ReadRemoteRuntimeTypeInformation32(IntPtr address)
291291
var name = ReadRemoteUTF8StringUntilFirstNullCharacter(typeDescriptorPtr + 0x0C, 60);
292292
if (name.EndsWith("@@"))
293293
{
294-
name = NativeMethods.UnDecorateSymbolName("?" + name);
294+
name = NativeMethods.UndecorateSymbolName("?" + name);
295295
}
296296

297297
sb.Append(name);
@@ -358,7 +358,7 @@ private string ReadRemoteRuntimeTypeInformation64(IntPtr address)
358358

359359
if (name.EndsWith("@@"))
360360
{
361-
name = NativeMethods.UnDecorateSymbolName("?" + name);
361+
name = NativeMethods.UndecorateSymbolName("?" + name);
362362
}
363363

364364
sb.Append(name);

Native/INativeMethods.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Diagnostics.Contracts;
3+
using System.Drawing;
4+
5+
namespace ReClassNET.Native
6+
{
7+
internal interface INativeMethods
8+
{
9+
IntPtr LoadLibrary(string fileName);
10+
11+
IntPtr GetProcAddress(IntPtr handle, string name);
12+
13+
void FreeLibrary(IntPtr handle);
14+
15+
Icon GetIconForFile(string path);
16+
17+
void EnableDebugPrivileges();
18+
19+
string UndecorateSymbolName(string name);
20+
21+
void SetProcessDpiAwareness();
22+
}
23+
24+
[ContractClassFor(typeof(INativeMethods))]
25+
internal abstract class IINativeMethodsContract : INativeMethods
26+
{
27+
public IntPtr LoadLibrary(string fileName)
28+
{
29+
Contract.Requires(fileName != null);
30+
31+
throw new NotImplementedException();
32+
}
33+
34+
public IntPtr GetProcAddress(IntPtr handle, string name)
35+
{
36+
Contract.Requires(name != null);
37+
38+
throw new NotImplementedException();
39+
}
40+
41+
public void FreeLibrary(IntPtr handle)
42+
{
43+
throw new NotImplementedException();
44+
}
45+
46+
public Icon GetIconForFile(string path)
47+
{
48+
Contract.Requires(path != null);
49+
50+
throw new NotImplementedException();
51+
}
52+
53+
public void EnableDebugPrivileges()
54+
{
55+
throw new NotImplementedException();
56+
}
57+
58+
public string UndecorateSymbolName(string name)
59+
{
60+
Contract.Requires(name != null);
61+
Contract.Ensures(Contract.Result<string>() != null);
62+
63+
throw new NotImplementedException();
64+
}
65+
66+
public void SetProcessDpiAwareness()
67+
{
68+
throw new NotImplementedException();
69+
}
70+
}
71+
}

Native/NativeMethods.Unix.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Runtime.InteropServices;
4+
5+
namespace ReClassNET.Native
6+
{
7+
internal class NativeMethodsUnix : INativeMethods
8+
{
9+
#region Imports
10+
11+
private const int RTLD_NOW = 2;
12+
13+
[DllImport("libdl.so")]
14+
private static extern IntPtr dlopen(string fileName, int flags);
15+
16+
[DllImport("libdl.so")]
17+
private static extern IntPtr dlsym(IntPtr handle, string symbol);
18+
19+
[DllImport("libdl.so")]
20+
private static extern int dlclose(IntPtr handle);
21+
22+
#endregion
23+
24+
public IntPtr LoadLibrary(string fileName)
25+
{
26+
return dlopen(fileName, RTLD_NOW);
27+
}
28+
29+
public IntPtr GetProcAddress(IntPtr handle, string name)
30+
{
31+
// Warning: dlsym could return IntPtr.Zero to a valid function.
32+
// Error checking with dlerror is needed but we treat IntPtr.Zero as error value...
33+
34+
return dlsym(handle, name);
35+
}
36+
37+
public void FreeLibrary(IntPtr handle)
38+
{
39+
dlclose(handle);
40+
}
41+
42+
public Icon GetIconForFile(string path)
43+
{
44+
return null;
45+
}
46+
47+
public void EnableDebugPrivileges()
48+
{
49+
50+
}
51+
52+
public string UndecorateSymbolName(string name)
53+
{
54+
return name;
55+
}
56+
57+
public void SetProcessDpiAwareness()
58+
{
59+
60+
}
61+
}
62+
}

Native/NativeMethods.Windows.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)