C#调用C++DLL中的回调函数(异步的情况,DLL中会在事件触发时调用函数)
来源:互联网 发布:中国软件杯东软睿道 编辑:程序博客网 时间:2024/05/16 09:35
我们在使用c#托管代码时,内存地址和GC回收那不是我们关心的,CLR已经给我们暗箱操作。
但是如果我们在c#中调用了一个非托管代码,比如vc的DLL,而且他有个回调函数,需要引用c#中的某个对象并操作,
这时候你就得要小心了。
要是非托管代码中用到得托管代码那个对象被GC给回收了,这时候就会报内存错误。
所以我们就要把那个对象“钉”住(pin),让它的内存地址固定,而不被垃圾回收掉,然后最后我们自己管理,自己释放内存,这时候就需要GCHandle,来看个msdn上的例子:
public delegate bool CallBack(int handle, IntPtr param);
public class LibWrap
{
[DllImport("user32.dll")]
public static extern bool EnumWindows(CallBack cb, IntPtr param);
}
class Program
{
static void Main(string[] args)
{
TextWriter tw = System.Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
LibWrap.EnumWindows(cewp, (IntPtr)gch);
gch.Free();
Console.Read();
}
private static bool CaptureEnumWindowsProc(int handle, IntPtr param)
{
GCHandle gch = (GCHandle)param;
TextWriter tw = (TextWriter)gch.Target;
tw.WriteLine(handle);
return true;
}
}
对上面的代码,略加解释:gch 会钉住(pin)tw这个对象,使其不受GC管理,告诉它,以后你崩管我,我也不用给你上税,其实管理权已经给gch,通过free来释放内存。
这种情况主要用在托管和非托管代码交互的时候,防止内存泄露来使用GCHandle。
另也可以使用GC.KeepAlive 方法(引用msdn)
KeepAlive 方法的目的是确保对对象的引用存在,该对象有被垃圾回收器过早回收的危险。这种现象可能发生的一种常见情形是,当在托管代码或数据中已没有对该对象的引用,但该对象仍然在非托管代码(如 Win32 API、非托管 DLL 或使用 COM 的方法)中使用。
下面是例子:
using System;
using System.Threading;
using System.Runtime.InteropServices;
// A simple class that exposes two static Win32 functions.
// One is a delegate type and the other is an enumerated type.
public class MyWin32
{
// Declare the SetConsoleCtrlHandler function
// as external and receiving a delegate.
[DllImport("Kernel32")]
public static extern Boolean SetConsoleCtrlHandler(HandlerRoutine Handler,
Boolean Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate Boolean HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
}
public class MyApp
{
// A private static handler function in the MyApp class.
static Boolean Handler(MyWin32.CtrlTypes CtrlType)
{
String message = "This message should never be seen!";
// A switch to handle the event type.
switch (CtrlType)
{
case MyWin32.CtrlTypes.CTRL_C_EVENT:
message = "A CTRL_C_EVENT was raised by the user.";
break;
case MyWin32.CtrlTypes.CTRL_BREAK_EVENT:
message = "A CTRL_BREAK_EVENT was raised by the user.";
break;
case MyWin32.CtrlTypes.CTRL_CLOSE_EVENT:
message = "A CTRL_CLOSE_EVENT was raised by the user.";
break;
case MyWin32.CtrlTypes.CTRL_LOGOFF_EVENT:
message = "A CTRL_LOGOFF_EVENT was raised by the user.";
break;
case MyWin32.CtrlTypes.CTRL_SHUTDOWN_EVENT:
message = "A CTRL_SHUTDOWN_EVENT was raised by the user.";
break;
}
// Use interop to display a message for the type of event.
Console.WriteLine(message);
return true;
}
public static void Main()
{
// Use interop to set a console control handler.
MyWin32.HandlerRoutine hr = new MyWin32.HandlerRoutine(Handler);
MyWin32.SetConsoleCtrlHandler(hr, true);
// Give the user some time to raise a few events.
Console.WriteLine("Waiting 30 seconds for console ctrl events");
// The object hr is not referred to again.
// The garbage collector can detect that the object has no
// more managed references and might clean it up here while
// the unmanaged SetConsoleCtrlHandler method is still using it.
// Force a garbage collection to demonstrate how the hr
// object will be handled.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Thread.Sleep(10000);
// Display a message to the console when the unmanaged method
// has finished its work.
Console.WriteLine("Finished!");
// Call GC.KeepAlive(hr) at this point to maintain a reference to hr.
// This will prevent the garbage collector from collecting the
// object during the execution of the SetConsoleCtrlHandler method.
GC.KeepAlive(hr);
Console.Read();
}
}
- C#调用C++DLL中的回调函数(异步的情况,DLL中会在事件触发时调用函数)
- C#中的委托(为什么C#调用dll的回调函数用委托)
- 关于C#中调用C++dll传递回调函数的问题
- C#调用dll,dll中执行C#端的回调函数
- jna调用c 的dll(包含回调函数)
- Matlab中的指针和在Matlab中注册c++dll的回调函数
- C#调用c++dll时,关于回调函数
- c#调用c++dll接口及回调函数
- C# 调用dll 回调函数
- 在C#中调用C++Dll函数的方法
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程
- C#调用C++DLL,及回调函数、string参数传递的总结
- 使用vs调用DLL在运行时调用的方法,使用delph调用dll
- Window输出窗口运行时调试信息中没有显示调用的dll情况
- 在C#里面调用带有回调函数和自定义结构体的DLL的例程——转载
- C# 调用 C dll char* callback 设置回调函数不定参数
- DLL函数调用以及回调函数设置
- 可视化排序程序
- 解决MySql Command Line Client查询结果乱码问题
- 杨洋(161)-计算机是如何工作的(实验一)
- debian wheezy 修改默认配置
- Roman To Integer
- C#调用C++DLL中的回调函数(异步的情况,DLL中会在事件触发时调用函数)
- 黑马程序员-JAVASE入门(组成部分1变量,函数等)
- unity3D 输出FPS
- 三星I9100如何鉴别翻新机
- [2013-05-15]安装centos6.3-x86_64
- 项目经验分享——Java常用工具类集合
- [2013-05-15]CentOS 6.3-x86_64挂载读写NTFS分区(ntfs-3g)
- [2013-05-15]CentOS6.3 x86_64 安装中文输入法
- Longest Common Prefix