C#调用C++的DLL函数另一则(delegate) z
使用DLLImport进行导入函数的事. C#调用C++的函数其实不止这一种方法, 还有一种方法是用delegate申明函数委托进行调用,这种方法略显麻烦,但是可以进行回调并应用指针.
在C#中,首先先要定义一个类,用来把DLL中函数地址转换成委托:
public class DLLWrapper
{
///<summary>
/// API LoadLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname); ///<summary>
/// API GetProcAddress
///</summary>
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname); ///<summary>
/// API FreeLibrary
///</summary>
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle); ///<summary>
///通过非托管函数名转换为对应的委托, by jingzhongrong
///</summary>
///<param name="dllModule">Get DLL handle by LoadLibrary</param>
///<param name="functionName">Unmanaged function name</param>
///<param name="t">ManageR type对应的委托类型</param>
///<returns>委托实例,可强制转换为适当的委托类型</returns>
public static Delegate GetFunctionAddress(int dllModule, string functionName, Type t)
{
int address = GetProcAddress(dllModule, functionName);
if (address == )
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
} ///<summary>
///将表示函数地址的IntPtr实例转换成对应的委托, by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
{
if (address == IntPtr.Zero)
return null;
else
return Marshal.GetDelegateForFunctionPointer(address, t);
} ///<summary>
///将表示函数地址的int转换成对应的委托,by jingzhongrong
///</summary>
public static Delegate GetDelegateFromIntPtr(int address, Type t)
{
if (address == )
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
}
}
然后, 用delegate声明函数:
delegate void _amDBRSetThermoModel(int mid, ref int errid);
再然后, 自己写个private的函数封装DLL中的函数, hModule()函数的作用是取得DLL的地址,用在多个输出函数中
private int hModule()
{
int _hModule = DLLWrapper.LoadLibrary(DLLPATH);
if (_hModule == )
{
return ;
}
return _hModule;
} private void amDBRInitialize()
{
try
{
_amDBRInitialize amf = (_amDBRInitialize)DLLWrapper.GetFunctionAddress(hModule(), "amDBRInitialize", typeof(_amDBRInitialize));
amf();
}
catch (Exception e)
{
throw e;
}
}
C#调用C++的DLL函数另一则(delegate) z的更多相关文章
- 在 C++Builder 工程里调用 DLL 函数
调用 Visual C++ DLL 给 C++Builder 程序员提出了一些独特的挑战.在我们试图解决 Visual C++ 生成的 DLL 之前,回顾一下如何调用一个 C++Builder 创建的 ...
- C#中可直接调用WIN32的API函数--USER32.DLL
Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)
C#调用C++编写的DLL函数, 以及各种类型的参数传递 1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int ...
- C#动态调用C++编写的DLL函数
C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...
- 一个简洁通用的调用DLL函数的帮助类
本次介绍一种调用dll函数的通用简洁的方法,消除了原来调用方式的重复与繁琐,使得我们调用dll函数的方式更加方便简洁.用过dll的人会发现c++中调用dll中的函数有点繁琐,调用过程是这样的:在加载d ...
- 动态调用DLL函数有时正常,有时报Access violation的异常
动态调用DLL函数有时正常,有时报Access violation的异常 typedef int (add *)(int a,int b); void test() { hInst=LoadL ...
- c++ 调用DLL函数,出现错误
c++ 调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a funct ...
- (原创)一个简洁通用的调用DLL函数的帮助类
本次介绍一种调用dll函数的通用简洁的方法,消除了原来调用方式的重复与繁琐,使得我们调用dll函数的方式更加方便简洁.用过dll的人会发现c++中调用dll中的函数有点繁琐,调用过程是这样的:在加载d ...
- Unity C# 调用 C++ DLL 并在 DLL 中调用 C# 的回调函数
Unity C# 调用 C++ DLL 并在 DLL 中调用 C# 的回调函数~~~ 呵呵... 看着有点晕.. 再解释一下就是 在Unity中 使用 C# 调用 C++ 写的 DLL, 但是在 ...
随机推荐
- HTMLImageElement类型的简便利用
这个是我在复习书籍的时候看见的,当时一个同学想通过页面发送请求,但是数据量不是太大,所以用的get方式,但是页面用表单提交请求的话会让页面进行跳转,当时我在网上查了一点资料,发现基本上都是通过ajax ...
- OpenCV2学习笔记04:图像的读取与显示
1. 图像读取:imread() Mat imread( ) 参数介绍: filename: 待加载的文件名称. flags: 此标志用来指定被加载图像的颜色类型(color type).这个标志的取 ...
- C#实现登录窗口(不用隐藏)
C#登录窗口的实现,特点就是不用隐藏,感兴趣的朋友不要错过 (1).在程序入口处,打开登录窗口 复制代码代码如下: static void Main() { Application.EnableV ...
- PHP LINUX Notice: undefined $_GET完美解决方法
PHP Notice: undefined 平时用$_GET[‘xx’] 取得参数值时,如果之前不加判断在未传进参数时会出现这样的警告: PHP Notice: undefined index xxx ...
- Delphi中的GetEnumName和GetEnumValue的使用方法
利用TypInfo单元的GetEnumName和GetEnumValue可以遍历任意枚举类型,并获取其名称和值.下面是示例Demo. uses TypInfo; ... procedure TForm ...
- 基于等待队列及poll机制的按键驱动代码分析和测试代码
按键驱动分析: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> ...
- 六,WPF的Application类
Application.ShutdownMode属性:通过,只要有一个窗口还没有关闭,Application类就保持应用程序处于有效状态,如果这不是所期望的行为,就可以调整该属性. 应用程序事件 Ap ...
- hadoop基本命令1
(大讲台——国内首个it在线混合式自适应学习平台,轻量级的高薪就业和技能提升解决方案) 1.列出所有Hadoop Shell支持的命令$ bin/hadoop fs -help2.显示关于某个命令的详 ...
- Memcached(五)Memcached的并发实例
package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAddress; i ...
- py2exe把python程序转换exe
1.首先下载py2exe:https://sourceforge.net/projects/py2exe/ 2.假设要打包的python 文件放在C:\packet路径下 如 果你有一个名为myscr ...