tags:

views:

27

answers:

1

I am trying to access the GetProcAddress function in the kernel32.dll from my C# .NET 2.0 application. The MSDN sites shows its C prototype as

FARPROC WINAPI GetProcAddress(
  __in  HMODULE hModule,
  __in  LPCSTR lpProcName
);

How do I convert this to C#? I think I've got most of it:

private static extern delegate GetProcAddress(IntPtr hModule, string lpProcName);

However, I think use of the delegate keyword is wrong. What should I change, so that I can access this function from my application? Thanks.

+2  A: 

PInvoke.Net has a good sample for this API

internal static class UnsafeNativeMethods {
   [DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
   internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName );
}

internal delegate int DllRegisterServerInvoker();

// code snippet in a method somewhere, error checking omitted
IntPtr fptr = UnsafeNativeMethods.GetProcAddress( hModule, "DllRegisterServer" );
DllRegisterServerInvoker drs = (DllRegisterServerInvoker) Marshal.GetDelegateForFunctionPointer( fptr, typeof(DllRegisterServerInvoker) );
drs(); // call via a function pointer

Article Link

JaredPar
Thank you, this was very helpful!
Jim Fell