-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionEnumerator.cs
52 lines (49 loc) · 2.05 KB
/
FunctionEnumerator.cs
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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace FunctionEnumerator
{
public class FunctionEnumerator
{
[DllExport("EnumerateFunctions", CallingConvention = CallingConvention.StdCall)]
public static IntPtr EnumerateFunctions(string assemblyPath)
{
var results = new List<string>();
try
{
// Load the .NET assembly
Assembly assembly = Assembly.LoadFrom(assemblyPath);
foreach (var type in assembly.GetTypes())
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
{
try
{
// Force JIT compilation by invoking PrepareMethod
RuntimeHelpers.PrepareMethod(method.MethodHandle);
IntPtr methodAddress = method.MethodHandle.GetFunctionPointer();
if (methodAddress != IntPtr.Zero)
{
// Format: MethodName:0xMemoryAddress
results.Add($"{method.DeclaringType.FullName}.{method.Name}:0x{methodAddress.ToInt64():X}");
}
}
catch (Exception e)
{
// Handle invalid program exceptions gracefully
results.Add($"{method.DeclaringType.FullName}.{method.Name}:Error: {e.Message}");
}
}
}
}
catch (Exception e)
{
return Marshal.StringToHGlobalAnsi($"Error: {e.Message}");
}
// Join results and return as a single string
return Marshal.StringToHGlobalAnsi(string.Join("|", results));
}
}
}