如何防止委托重新定位CLR堆 [英] How to prevent a delegate from relocating on the CLR heap

查看:69
本文介绍了如何防止委托重新定位CLR堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我正在尝试用C#和C ++ / CLI编写全局钩子。如果没有这个小委托问题,我几乎已经完成了



我得到一个代表的函数指针并将其传递给我

hook.dll。代理被调用几秒钟,但代表

似乎在CLR堆上重新定位。

如果尝试使用GCHandle,但据我所知只有

防止垃圾收集器收集我的代表并且不会停止

搬迁。

然后就是pin_ptr。但是一个固定指针只能工作直到它b / b
超出范围并且它只能在本地声明,所以当我得到函数指针后立即发生这种情况

。顺便说一下,我用......


proc =

(HOOKPROC)(Marshal :: GetFunctionPointerForDelegate(innerEventHandler).ToPointer());


...获取函数指针。

如果有人知道如何锁定代表,只要我需要

从非托管代码指向它或有任何想法,变通方法等请

让我知道!

非常感谢您提前!

问候,

弗洛里安

解决方案

Florian,

我正在尝试在C#和C ++ / CLI中编写全局钩子。如果没有这个小委托问题,我几乎已经完成了。
我得到了一个委托的函数指针并将其传递给我的
hook.dll。代理被调用几秒钟,但代表
似乎在CLR堆上重新定位。
如果尝试使用GCHandle但据我所知只能
防止垃圾收集我的代表的收藏家并没有停止搬迁。




你走在正确的轨道上。你实际上可以告诉一个GCHandle你想要它所引用的句柄固定在堆中,所以它不会在

GC期间被移动。为此,使用GCHandle :: Alloc()的重载,它接受一个

GCHandleType参数并传入GCHandleType :: Pinned到它。

-

Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/


Thomas,

你走在正确的轨道上。实际上,您可以告诉GCHandle您希望它所引用的句柄固定在堆中,以便在GC期间不会移动它。为此,使用GCHandle :: Alloc()的重载,它接受一个
GCHandleType参数并传入GCHandleType :: Pinned到它。




谢谢你的答复,但我担心它不起作用。 Dll项目

编译得很好,但是一旦我运行我的C#测试应用程序,我就会得到一个ArgumentException

(对象包含非原始或非blittable数据。 )。好像是

GCHandle :: Alloc()我的代表有问题...

有什么建议吗?


Florian


我想你可以在你的应用程序运行之前固定你的代理并将它转换为它的本机类型并将它存储在你的应用程序的成员变量中

class。


" Florian A." <人***************** @ directbox.com> D'è?óê?t

新闻:eF ************** @ TK2MSFTNGP10.phx.gbl ...

你好大家好!

我正在尝试用C#和C ++ / CLI编写全局钩子。如果没有这个小委托问题,我几乎已经完成了

我得到了一个委托的函数指针并将其传递给我的
hook.dll。代理被调用几秒钟,但代表
似乎在CLR堆上重新定位。
如果尝试使用GCHandle但据我所知只能
防止垃圾收集我的代表的收藏家并没有
停止搬迁。
然后是pin_ptr。但是一个固定指针只能工作到
它超出范围而且它只能在本地声明,所以当我得到函数指针后立即发生这种情况。顺便说一句,我使用...

proc =

(HOOKPROC)(Marshal :: GetFunctionPointerForDelegate(innerEventHandler).ToPoint

er( ));
...获取函数指针。
如果有人知道如何锁定代理,只要我需要
从非托管代码指向它或有任何想法,变通办法等请
让我知道!
非常感谢你提前!

问候,
Florian


Hi everyone!

I''m trying to write a global hook in C# and C++/CLI. I''m almost
done if there wasn''t this little delegate problem.
I get the function pointer to a delegate and pass it to my
hook.dll. The delegate gets called for a few seconds but then the delegate
seems to be relocated on the CLR heap.
If tried using a GCHandle but as far as I can tell this only
prevents the garbage collector from collecting my delegate and doesn''t stop
relocation.
Then there is pin_ptr. But a pinning pointer only works till it
goes out of scope and it can only be declared locally so this happens
immediately after I get the function pointer. By the way, I use ...

proc =
(HOOKPROC)(Marshal::GetFunctionPointerForDelegate( innerEventHandler).ToPointer());

... to get the function pointer.
If anyone knows how to pin the delegate for as long as I need to
point to it from unmanaged code or has any ideas, workarounds, etc. please
let me know!
Thank you very much in advance!

Regards,
Florian

解决方案

Florian,

I''m trying to write a global hook in C# and C++/CLI. I''m almost
done if there wasn''t this little delegate problem.
I get the function pointer to a delegate and pass it to my
hook.dll. The delegate gets called for a few seconds but then the delegate
seems to be relocated on the CLR heap.
If tried using a GCHandle but as far as I can tell this only
prevents the garbage collector from collecting my delegate and doesn''t
stop relocation.



You are on the right track. You can actually tell a GCHandle that you want
the handle it references to be pinned in the heap so it is not moved during
GC. For this, use the overload of GCHandle::Alloc() that takes a
GCHandleType argument and pass in GCHandleType::Pinned to it.
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/


Thomas,

You are on the right track. You can actually tell a GCHandle that you want
the handle it references to be pinned in the heap so it is not moved
during GC. For this, use the overload of GCHandle::Alloc() that takes a
GCHandleType argument and pass in GCHandleType::Pinned to it.



thank you for your reply but I''m afraid it doesn''t work. The Dll project
compiles fine but as soon as I run my C# test app I get an ArgumentException
("Object contains non-primitive or non-blittable data."). Seems like
GCHandle::Alloc() has a problem with my delegate ...
Any suggestions?

Florian


I guess you can pin your delegate before your application runs and cast it
to its native type and store it in a member variable in your application
class.

"Florian A." <al*****************@directbox.com> D′è?óê?t
news:eF**************@TK2MSFTNGP10.phx.gbl...

Hi everyone!

I''m trying to write a global hook in C# and C++/CLI. I''m almost done if there wasn''t this little delegate problem.
I get the function pointer to a delegate and pass it to my
hook.dll. The delegate gets called for a few seconds but then the delegate
seems to be relocated on the CLR heap.
If tried using a GCHandle but as far as I can tell this only
prevents the garbage collector from collecting my delegate and doesn''t stop relocation.
Then there is pin_ptr. But a pinning pointer only works till it goes out of scope and it can only be declared locally so this happens
immediately after I get the function pointer. By the way, I use ...

proc =
(HOOKPROC)(Marshal::GetFunctionPointerForDelegate( innerEventHandler).ToPoint
er());
... to get the function pointer.
If anyone knows how to pin the delegate for as long as I need to point to it from unmanaged code or has any ideas, workarounds, etc. please
let me know!
Thank you very much in advance!

Regards,
Florian



这篇关于如何防止委托重新定位CLR堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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