C # get a list of unmanaged exports C dll

I have a C dll with exported functions

I can use the dumpbin.exe / EXPORTS command line tool to retrieve a list of exported functions, and then use them in my C # code to (successfully) call these functions.

Is there a way to get this list of exported functions directly from .NET without using an external command line tool?

thanks

+4
source share
1 answer

As far as I know, there is no class in the .NET Framework. provides the necessary information.

However, you can use the platform invocation services (PInvoke) of the .Net platform to use the Win32 dll library functions dbghelp.dll . This DLL is part of the Debugging Tools for the Windows platform. The dbghelp dll provides a function called SymEnumerateSymbols64 that allows you to list all exported symbols of a dynamic link library. There is also a newer feature called SymEnumSymbols , which also allows you to list exported characters.

The code below provides a simple example of using the SymEnumerateSymbols64 function.

 [DllImport("dbghelp.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SymInitialize(IntPtr hProcess, string UserSearchPath, [MarshalAs(UnmanagedType.Bool)]bool fInvadeProcess); [DllImport("dbghelp.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SymCleanup(IntPtr hProcess); [DllImport("dbghelp.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern ulong SymLoadModuleEx(IntPtr hProcess, IntPtr hFile, string ImageName, string ModuleName, long BaseOfDll, int DllSize, IntPtr Data, int Flags); [DllImport("dbghelp.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SymEnumerateSymbols64(IntPtr hProcess, ulong BaseOfDll, SymEnumerateSymbolsProc64 EnumSymbolsCallback, IntPtr UserContext); public delegate bool SymEnumerateSymbolsProc64(string SymbolName, ulong SymbolAddress, uint SymbolSize, IntPtr UserContext); public static bool EnumSyms(string name, ulong address, uint size, IntPtr context) { Console.Out.WriteLine(name); return true; } static void Main(string[] args) { IntPtr hCurrentProcess = Process.GetCurrentProcess().Handle; ulong baseOfDll; bool status; // Initialize sym. // Please read the remarks on MSDN for the hProcess // parameter. status = SymInitialize(hCurrentProcess, null, false); if (status == false) { Console.Out.WriteLine("Failed to initialize sym."); return; } // Load dll. baseOfDll = SymLoadModuleEx(hCurrentProcess, IntPtr.Zero, "c:\\windows\\system32\\user32.dll", null, 0, 0, IntPtr.Zero, 0); if (baseOfDll == 0) { Console.Out.WriteLine("Failed to load module."); SymCleanup(hCurrentProcess); return; } // Enumerate symbols. For every symbol the // callback method EnumSyms is called. if (SymEnumerateSymbols64(hCurrentProcess, BaseOfDll, EnumSyms, IntPtr.Zero) == false) { Console.Out.WriteLine("Failed to enum symbols."); } // Cleanup. SymCleanup(hCurrentProcess); } 

To simplify the example, I did not use SymEnumSymbols . I also made an example without using classes such as the SafeHandle .Net framework class. If you need an example for SymEnumSymbols , just let me know.

+4
source

Source: https://habr.com/ru/post/1497191/


All Articles