2010-09-09 16 views
1

J'essaie d'accéder à la fonction GetProcAddress dans kernel32.dll à partir de mon application C# .NET 2.0. Les sites MSDN montre son prototype C commeDéclarations de fonctions externes DLL en C#

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

Comment puis-je convertir en C#? Je pense que j'ai la plus grande partie:

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

Cependant, je pense que l'utilisation du mot-clé delegate est erroné. Que devrais-je changer pour pouvoir accéder à cette fonction depuis mon application? Merci.

Répondre

2

PInvoke.Net a un bon échantillon pour cette 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 Lien

+0

Merci, cela a été très utile! –

Questions connexes