开发者

DLL External Function Declarations in C#

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.


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

  • http://pinvoke.net/default.aspx/kernel32/GetProcAddress.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜