C#???????C++??д??DLL????
???????????? ???????[ 2014/10/13 10:55:42 ] ??????????????? C++ C#
???????????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);
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11