c# - Trouble getting GCHandle.Alloc() to pin a form so handle can be used by native code -


i have ce6.0r3 system uses .netcf3.5 , uses p/invoke interface several native c++ dlls.

these devices crash occassionally , see popup says "application myc#app.exe encountered serious error , must shut down".

one of things pass handle c# form native c++ application uses directshow render videos on c# form.

i've been researching , have found several discussions using gchandle pin managed objects don't move.

i added diagnostics try , detect if problem added gchandle.alloc()s pin objects. reason, objects seem move in memory though should pinned.

here portion of code:

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 _videoplay object instance won't moved 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 our native c++ application based on code: http://msdn.microsoft.com/en-us/library/ms899491.aspx // pass handle ot our form want display video on // directshow hwnd ws_child of _videoplay directshow_connect(_videoplay.handle); diag.putline("_videoplay.handle = " + _videoplay.handle + " after directshow_connect"); 

and output:

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) 

why handle change though pinned?

you misunderstand how gchandle works , how it's used.

first, gchandle can used on blittable type, can't pin form itself. you're doing pinning handle, saying gc in code "do not move location have address of form in memory." meaning storage location of handle cannot move. there's nothing prevent form moving, , thereby value handle holds.

it's bit odd handle changing, suspect native form handle can't change once form created. makes me think have pseudo-handle. if case, using in native calls shouldn't work.

i'm not firmly convinced error movement because i'd never seen such behavior before - i'd more inclined think it's attempt use handle of disposed form, you've been doing debugging , have better feel it.

at rate, if believe failure due handle change best workaround use p/invoke (to createwindowex) create container form itself. gc can't move then, because doesn't know it. eliminate compaction culprit (or fix problem).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -