0
点赞
收藏
分享

微信扫一扫

c#网站调用c++写的dll库

河南妞 2022-07-27 阅读 57


c++写的类库,在winfrm中可以使用路径来dllimport,在网站中不太可能会有服务器的操作权限,因此需要换个方式来取得dll的路径。

 

使用这样一个类:

 

public class DllInvoke
{
#region P/Invoke helpers 导入dll声明
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
#endregion

IntPtr dll;

public DllInvoke(string dllpath)
{
dll = LoadLibrary(dllpath);

}

~DllInvoke()
{
FreeLibrary(dll);
}

public Delegate Invoke(String APIName,Type t)
  {
   IntPtr dllMethod = GetProcAddress(dll, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(dllMethod, t);
  }



}

需引用空间:using System.Runtime.InteropServices;

 

使用方法:

//定义委托
public delegate int add(int a,int b);//add是类库中的方法名
protected void Page_Load(object sender, EventArgs e)
{

string dllname = System.Web.HttpContext.Current.Server.MapPath("bin")+"/cdt.dll";

DllInvoke dll = new DllInvoke(dllname);
add d = (add) dll.Invoke("add", typeof(add));

object ee = d(5, 4);
Response.Write(ee);


}

 


举报

相关推荐

0 条评论