???????????DLL??????Windows API??????LoadLibrary??GetProcAddress???FreeLibrary????????????DllImport??C#?????????????????
????[DllImport("Kernel32")]
????public static extern int GetProcAddress(int handle?? String funcname);
????[DllImport("Kernel32")]
????public static extern int LoadLibrary(String funcname);
????[DllImport("Kernel32")]
????public static extern int FreeLibrary(int handle);
????????????C++?ж??????Dll?е?????????????????????
????????DLL???????????????????????????£?
????BOOL __stdcall foo(Object &object?? LPVOID lpReserved);
????1??????????????????????
????typedef BOOL (__stdcall *PFOO)(Object &object?? LPVOID lpReserved);
????2??????LoadLibrary????dll??
????HINSTANCE hInst = ::LoadLibraryW(dllFileName);
????3??????GetProcAddress???????????ú?????????
????PFOO foo = (PFOO)GetProcAddress(hInst??"foo");
????if(foo == NULL)
????{
????FreeLibrary(hInst);
????return false;
????}
????4??????foo??????
????BOOL bRet = foo(object??(LPVOID)NULL);
????5????????????DLL??
????FreeLibrary(hInst);
?????????C#????????????????????????????????????????????C++???????????.NET Framework 2.0?????????GetDelegateForFunctionPointer??????????е??????
?????????????????????????????????C#?ж??????Dll?е???????
public class DLLWrapper
{
///<summary>
/// API LoadLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
///<summary>
/// API GetProcAddress
///</summary>
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle?? String funcname);
///<summary>
/// API FreeLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
///<summary>
///??????й?????????????????У? by jingzhongrong
///</summary>
///<param name="dllModule">???LoadLibrary????DLL???</param>
///<param name="functionName">???й??????</param>
///<param name="t">????????????</param>
///<returns>?????????????????????????????</returns>
public static Delegate GetFunctionAddress(int dllModule?? string functionName?? Type t)
{
int address = GetProcAddress(dllModule?? functionName);
if (address == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address)?? t);
}
///<summary>
///??????????????IntPtr??????????????У? by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(IntPtr address?? Type t)
{
if (address == IntPtr.Zero)
return null;
else
return Marshal.GetDelegateForFunctionPointer(address?? t);
}
///<summary>
///??????????????int???????????У?by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(int address?? Type t)
{
if (address == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address)?? t);
}
}