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

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

问题描述

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

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

简单来说,我有一个C ++本地DLL(没有CLR /托管支持),需要从C#代码回调。本地端存储由C#提供的stdcall回调函数。我一直认为回调METHOD(在C#)必须是静态的,但非静态和lambda表达式BOTH工作JUST FINE! this指针是如何从本地代码编组的?我总是认为本地代码只存储非实例函数指针?

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?

现在,我发现一篇文章,一些人发射IL代码在本机和非静态管理回调。我也注意到这个折旧的方法:Marshal.GetUnmanagedThunkForManagedMethodPtr()。

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?

问题摘要:


  1. 现在thunking通过发布IL代码本地构建成.NET?

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

在Mono中支持隐式的thunking吗?

Is implicit "thunking" supported in Mono as well?

当为管理回调发出IL时,当
thunk指向的实例被删除时会发生什么?是否删除IL,或
可能会导致内存泄漏,这么说?

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?

感谢。

推荐答案


现在Thunking通过发布IL代码本地构建到.NET如果是这样,在什么版本的.NET,这成为本机支持?

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

没有IL参与thunk,本地代码 - 一个蹦床,它重新排列参数以满足.NET的调用约定,包括在关闭的代理的情况下保存的指针,然后执行尾调用.NET方法本身。

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.


Mono也支持隐式的thunking?

Is implicit "thunking" supported in Mono as well?

这称为反向p /调用,应该使它很容易在Mono文档中查找。

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


当为托管回调发出IL时,当thunk引用的实例被删除时会发生什么?

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天全站免登陆