You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8 KiB
C#

using HuizhongLibrary.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace HuizhongLibrary
{
public class DllInvoke
{
[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 DllInvoke(String DLLPath)
{
if (System.IO.File.Exists(DLLPath) == false)
{
ErrorFollow.TraceWrite("加载动态库错误", DLLPath, "文件不存在");
return;
}
try
{
hLib = LoadLibrary(DLLPath);
}
catch (Exception ex)
{
ErrorFollow.TraceWrite("加载动态库错误", DLLPath, ex.Message);
}
}
~DllInvoke()
{
FreeLibrary(hLib);
}
public int GetHandle()
{
return hLib.ToInt32();
}
//将要执行的函数转换为委托
public Delegate Invoke(String APIName, Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
public delegate int Compile(String command, StringBuilder inf);
public void test()
{
//编译
DllInvoke dll = new DllInvoke("~/Bin/Judge.dll");
Compile compile = (Compile)dll.Invoke("Compile", typeof(Compile));
//compile();//这里就是调用我的DLL里定义的Compile函数
}
}
}