在 C++ 中我们能够通过 LoadLibrary, GetProcAddress 来动态调用 dll 的导出函数. 将非托管函数指针转换为委托。 [DllImport(“Kernel32”)] private static Delegate GetAddress(int dllModule, string functionname, Type t) private void button1_Click(object sender, EventArgs e) 链接地址: http://rick.cnblogs.com/archive/2006/07/13/apicall.html
在 C# 中也能够用这样的方式吗?
在 DotNet 2.0 里面这样是可以的, 这完全得益于 2.0新增的一个函数
Marshal.GetDelegateForFunctionPointer 方法
此方法在 .NET Framework 2.0 版中是新增的。
实例代码如下:
public delegate int MsgBox(int hwnd,string msg,string cpp,int ok);
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);
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
{
int huser32 = 0;
huser32 = LoadLibrary(“user32.dll”);
MsgBox mymsg = (MsgBox)GetAddress(huser32, “MessageBoxA”, typeof(MsgBox));
mymsg(this.Handle.ToInt32(), txtmsg.Text, txttitle.Text , 64);
FreeLibrary(huser32);
}
在c#中动态调用native dll的导出函数_c#应用
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在c#中动态调用native dll的导出函数_c#应用
相关推荐
- 暂无文章
