我有一个CE6.0R3系统,它使用.NetCF3.5并使用P/Invoke来接口几个本地C++ dll。
这些设备偶尔会崩溃,有时我们会看到一个弹出窗口,上面写着“应用程序myc#app.exe遇到严重错误,必须关闭”。
我们要做的一件事是将C#表单的句柄传递给本地C++应用程序,该应用程序使用DirectShow在C#表单上呈现视频。
我一直在研究它,并发现了几个关于使用GCHandle固定托管对象以便它们不会移动的讨论。
我添加了诊断程序来尝试检测这是否是问题所在,并添加了GCHandle.Alloc()来锁定对象。由于某些原因,对象似乎在内存中移动,即使它们应该被固定。
以下是我的代码的一部分:
Diag.Putline("_videoplay create");
_videoplay = new Form();
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before pinning Handle");
GCHandle gch = GCHandle.Alloc(_videoplay.Handle, GCHandleType.Pinned);
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before pinning Form");
GCHandle gch_videoplay = GCHandle.Alloc(_videoplay, GCHandleType.Pinned); // Pin the _videoplay object instance so it won't get moved by GC
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " after pinning");
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .Location");
_videoplay.Location = new Point(x, y);
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .Size");
_videoplay.Size = new Size(w, h);
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .BackColor");
_videoplay.BackColor = bgColor;
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .FormBorderStyle");
_videoplay.FormBorderStyle = FormBorderStyle.None;
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .Owner");
_videoplay.Owner = _mediaform;
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before .Show()");
_videoplay.Show();
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " after .Show()");
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " before directshow_connect");
Diag.Putline("directshow_connect");
// P/Invoke to our native C++ application based on this code: http://msdn.microsoft.com/en-us/library/ms899491.aspx
// Pass the handle ot our form that we want to display the video on
// the directshow HWND will be a WS_CHILD of _videoplay
directshow_connect(_videoplay.Handle);
Diag.Putline("_videoplay.Handle = " + _videoplay.Handle + " after directshow_connect");和输出:
14:59:37| _videoplay create
14:59:37| _videoplay.Handle = 1879191552 before pinning Handle
14:59:37| _videoplay.Handle = 1879191552 before pinning Form
14:59:37| _videoplay.Handle = 1879191552 after pinning
14:59:37| _videoplay.Handle = 1879191552 before .Location
14:59:37| _videoplay.Handle = 1879191552 before .Size
14:59:37| _videoplay.Handle = 1879191552 before .BackColor
14:59:37| _videoplay.Handle = 1879191552 before .FormBorderStyle
14:59:37| _videoplay.Handle = 1879191776 before .Owner
14:59:37| _videoplay.Handle = 1879192000 before .Show()
14:59:37| _videoplay.Handle = 1879192000 after .Show()
14:59:37| _videoplay.Handle = 1879192000 before directshow_connect
14:59:37| directshow_connect
14:59:39| _videoplay.Handle = 1879192000 after directshow_connect
14:59:41| _videoplay.Handle = 1879193248 (_TickTockThreadProc)
14:59:41| _videoplay.Handle = 1879193248 (_TickTockThreadProc)
14:59:41| _videoplay.Handle = 1879193248 (UpdateTimer_Tick)
14:59:41| _videoplay.Handle = 1879193248 (UpdateTimer_Tick)
14:59:41| _videoplay.Handle = 1879193248 (_TickTockThreadProc)
14:59:42| _videoplay.Handle = 1879193248 (_TickTockThreadProc)为什么手柄即使被钉住了也会改变?
发布于 2013-05-10 07:35:17
您误解了GCHandle的工作原理和使用方法。
首先,GCHandle只能用于blittable类型,所以您不能固定窗体本身。您所做的是固定句柄,这基本上是在代码中告诉GC“不要移动内存中表单地址所在的位置”。这意味着手柄本身的存储位置不能移动。没有任何东西可以阻止窗体移动,从而阻止句柄持有的值。
句柄正在改变,这有点奇怪,我怀疑一旦表单本身被创建,Native Form句柄本身就不能改变。这让我觉得你有一个伪句柄。如果是这样的话,在本机调用中使用它应该是行不通的。
我不确信这个错误是一种运动,因为我以前从来没有见过这样的行为-我更倾向于认为这是试图使用已处置表单的句柄,但您一直在进行调试,并且可能对此有更好的感觉。
无论如何,如果您确实认为失败是由于句柄更改造成的,那么最好的解决方法是使用P/Invoke (到CreateWindowEx)来创建容器表单本身。GC不能移动它,因为它对它一无所知。这将消除作为罪魁祸首的压缩(或解决问题)。
https://stackoverflow.com/questions/16472229
复制相似问题