2

I want to get the function pointer (ie IntPtr) for an extern method such as:

[DllImport("DbgHelp.dll")]
static extern void SymFunctionTableAccess64(IntPtr process, ulong addrBase);

which, then also be used as a parameter for an extern method such as:

[DllImport("DbgHelp.dll")]
static extern int StackWalk64(......., IntPtr FunctionTableAccessRoutine, ....);

I know I can use LoadLibrary and then GetProcAddress, or wrap the function in a C# method and then use Marshal.GetFunctionPointerForDelegate for a function pointer. I wonder if we can retrieve the function pointer directly from P/Invoke mechanism, because, during runtime, functions will already be loaded via DllImport. Just to note, my problem is not specific to StackWalk64 or any DbgHelp.dll functions.

zahir
  • 1,298
  • 2
  • 15
  • 35
  • Take a look at this article. [Marshalling structures containing function pointers][1] [1]: http://stackoverflow.com/questions/1969049/c-sharp-p-invoke-marshalling-structures-containing-function-pointers?rq=1 Maybe it helps. – Trx Dec 18 '14 at 10:30
  • [This question](http://stackoverflow.com/questions/5235445/pinvoke-c-function-takes-pointer-to-function-as-argument) could help you as well. – Kilazur Dec 18 '14 at 10:44
  • "...during runtime, functions will already be loaded via `DllImport`..." Actually, this won't happen until the function is called from managed code for the first time, which loads the DLL dynamically. So if you're not actually calling it, `LoadLibrary` is definitely necessary. – Jeroen Mostert Dec 18 '14 at 12:54
  • I don't know what you're up to with the `StackWalk64` and other such APIs. But just in case you want to write some sort of managed debugger for native apps (including just grabbing stacktraces for running processes), you might be better off encapsulating all that logic in a native DLL by itself, providing "easiy" DllImport-entrypoints for your managed code (been there, done that :-|). – Christian.K Dec 18 '14 at 12:57
  • The return type is IntPtr, not void. Don't use pinvoke for this, and especially not GetProcAddress(), there are [friendly wrappers](http://msdn.microsoft.com/en-us/library/x93ctkx8.aspx) for the DbgHelp api. – Hans Passant Dec 18 '14 at 12:59

1 Answers1

1

You need to use GetProcAddress to get the address of an external unmanaged function.

There's no problem doing this. Functions are not loaded as such. Modules are loaded. Once a module has been loaded, GetProcAddress just performs a lookup in the export table of the module. If you know that the module is already loaded then you don't need to call LoadLibrary again. You can use GetModuleHandle. Not that a call to LoadLibrary presents any problem either really. If the module is already loaded, then a call to LoadLibrary simply increases the reference count of the module, and returns its handle.

Mind you, what you are attempting doesn't look suited to p/invoke. Native code is surely more appropriate.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490