[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();
}