如何非静态回调从本机代码工作? [英] How do non-static callbacks work from native code?

查看:139
本文介绍了如何非静态回调从本机代码工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有点奇怪问这个问题,因为我有喜欢它似乎不应该工作的代码,但它确实,虽然我不是在抱怨,我想确认一下为什么? LOL



简单地说,我有一个C ++本机DLL(没有CLR /管理支持的话)这需要从C#代码中的回调。本机端存储一个STDCALL回调函数,它是由C#侧供给。我一直以为回调方法(在C#)必须是静态的,但非静态和lambda表达式都工作就好了!?如何将this指针正在从本地代码封?我始终认为,本地代码只存储非实例函数指针?



现在,我发现那里的一些人发出的IL代码的文章,以本地和非之间的桥梁 -static管理回调。我也注意到了这个折旧方法:Marshal.GetUnmanagedThunkForManagedMethodPtr()。该方法不再支持,这我假设意味着它是建在



问题总结:




  1. 时的thunk现在原生内置到.NET所散发的IL代码?如果是这样,在什么版本的.NET中没有此成为原生支持?


  2. 是隐式的thunk单声道支持呢?


  3. 在白细胞介素发出的管理回调,当
    上的实例在thunk是指被删除时会发生什么?是IL删除或
    可能这导致了内存泄漏可以这么说?




谢谢你。


解决方案

时的thunk现在原生内置到.NET所散发的IL代码?如果是这样,在什么版本的.NET中没有此成为原生支持?




没有IL在参与在thunk,它发生通过发射本机代码 - 重新排列参数,以满足.NET的调用约定,包括这个这是保存在密闭代表下的指针,然后蹦床执行尾部调用.NET方法本身。




时隐的thunk单声道支持呢?




这被称为反向的p / Invoke,应该可以很容易地查找在黑白文档。




在白细胞介素发出的管理回调,当实例的thunk是指被删除时会发生什么?




当垃圾回收委托,由蹦床使用的内存也被释放。所以,你需要保持委托只要活着,本机代码的指针指向蹦床。


It's a bit odd asking this question, because I have code that seems like it shouldn't work, but it does, and although I'm not complaining, I'd like to confirm why? LOL

Simply, I have a C++ native DLL (no CLR/managed support at all) that takes a call-back from C# code. The native side stores an stdcall callback function, which is supplied by the C# side. I always thought the callback METHOD (in C#) had to be static, but non-static and lambda expression BOTH work JUST FINE!? How is the "this" pointer being marshaled from native code? I always thought that native code only stores non-instance function pointers?

Now, I did find an article where some guy emitted IL code to "bridge" between native and non-static managed callbacks. I also noticed this depreciated method: "Marshal.GetUnmanagedThunkForManagedMethodPtr()". The method is no longer supported, which I'm assuming means it is built in?

Question summary:

  1. Is thunking now built natively into .NET by emitting IL code? If so, at what version of .NET did this become natively supported?

  2. Is implicit "thunking" supported in Mono as well?

  3. When the IL is emitted for the managed callbacks, what happens when the instance the thunk refers to is deleted? Is the IL removed, or might this lead to a memory "leak" so to speak?

Thanks.

解决方案

Is thunking now built natively into .NET by emitting IL code? If so, at what version of .NET did this become natively supported?

No IL in involved in the thunk, it happens by emitting native code -- a trampoline that rearranges arguments to meet .NET's calling convention, including the this pointer which is saved in case of closed delegates, and then performs a tail call to the .NET method itself.

Is implicit "thunking" supported in Mono as well?

This is called "reverse p/invoke", that should make it easy to look for in the Mono docs.

When the IL is emitted for the managed callbacks, what happens when the instance the thunk refers to is deleted?

When the delegate is garbage collected, memory used by the trampoline is also freed. So you need to keep the delegate alive as long as the native code has a pointer to the trampoline.

这篇关于如何非静态回调从本机代码工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆