c# 動態加載dll庫


[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(String path);

[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);

[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);

private IntPtr hLib;

public Patrol(string filepath)
{
if (hLib == null || hLib == (IntPtr)0)
{
hLib = LoadLibrary(filepath);
}
if (hLib != (IntPtr)0)
{
funcEnumDongle = (Type_EnumDongle)Invoke("EnumDongle", typeof(Type_EnumDongle));
funcDongleRead = (Type_DongleRead)Invoke("DongleRead", typeof(Type_DongleRead));
}
}

~Patrol()
{
if (hLib != null)
FreeLibrary(hLib);
}

//將要執行的函數轉換為委托
public Delegate Invoke(String APIName, Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}

private delegate UInt32 Type_EnumDongle();
private static Type_EnumDongle funcEnumDongle;
/// <summary>
/// 調用庫中事件
/// </summary>
public static UInt32 PCPSEnumDongle()
{
return funcEnumDongle();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM