Friday, 15 January 2010

vb.net - Function from a referenced library which has function pointers as parameters -



vb.net - Function from a referenced library which has function pointers as parameters -

update: changed question match updated code , more specific problem.

i have no experience working vb.net or visual studio, , limited experience c. have been trying larn function pointers , delegates still don't understand them.

i writing project in vb.net makes calls methods dll file.

c code dll file:

typedef unsigned char byte; typedef unsigned short word; typedef unsigned long dword; typedef void (__stdcall *fp_setbaud)(word); typedef short (__stdcall *fp_get)(word); typedef void (__stdcall *fp_put)(byte); typedef void (__stdcall *fp_flush)(void); typedef void (__stdcall *fp_delay)(word); byte __stdcall initrelay(fp_setbaud _setbaud, fp_get _get, fp_put _put, fp_flush _flush, fp_delay _delay) { ... } byte __stdcall readrelay(void){ ... }

vb.net module code:

declare function initrelay lib "z:\devel\relayapi\debug\relayapi.dll" (byval setbaud action(of short), byval getit func(of short, short), byval putit action(of short), byval flushit action, byval delay action(of short)) byte declare function readrelay lib "z:\devel\relayapi\debug\relayapi.dll" () byte public sub setbaud(byval baud short) ... end sub public function getit(byval timeout short) short ... end function public sub putit(byval dat short) ... end sub public sub flushit() ... end sub public function delayms(byval ms short) short ... end function

vb.net form code:

dim byte phone call initrelay(addressof modulecode.setbaud, addressof modulecode.getit, addressof modulecode.putit, addressof modulecode.flushit, addressof modulecode.delayms) = readrelay()

the error getting when phone call a = readrelay() within vb.net code. error appears on each 1 of parameters , says following:

an unhandled exception of type 'system.accessviolationexception' occurred

the readrelay function uses of functions passed initrelay function. initrelay called prior readrelay , gives me no errors. assuming error still has how passing function pointers initrelay function.

i have been using this website seek figure out type conversions still don't know do.

does have info on should doing phone call these functions?

edit

new delegate declarations suggested below:

private setbauddelegate new action(of short)(addressof modcommstuff.setbaud) private getitdelegate new func(of short, short)(addressof modcommstuff.getit) private putitdelegate new action(of short)(addressof modcommstuff.putit) private flushitdelegate new action(addressof modcommstuff.flushit) private delayitdelegate new action(of short)(addressof modcommstuff.delayms) .... phone call initrelay(setbauddelegate, getitdelegate, putitdelegate, flushitdelegate, delayitdelegate)

i think need pin delegates not garbage collected.

dim handle gchandle = gchandle.alloc(objecttopin, gchandletype.pinned)

don't forget free object after done using .free

handle.free()

more info available @ website. http://manski.net/2012/06/pinvoke-tutorial-pinning-part-4/

update 1

after reading more looks pinning delegates not allowed, still have maintain delegate in memory. seek creating instance variables delegates , keeping them in fields defined in form's code. should plenty maintain delegates getting garbage collected , maintain unmanaged "stubs" getting cleaned up.

along same lines, managed delegates can marshaled unmanaged code, exposed unmanaged function pointers. calls on pointers perform unmanaged managed transition; alter in calling convention; entry right appdomain; , necessary argument marshaling. unmanaged function pointer must refer fixed address. disaster if gc relocating that! leads many applications create pinning handle delegate. unnecessary. unmanaged function pointer refers native code stub dynamically generate perform transition & marshaling. stub exists in fixed memory outside of gc heap.

however, application responsible somehow extending lifetime of delegate until no more calls occur unmanaged code. lifetime of native code stub straight related lifetime of delegate. 1 time delegate collected, subsequent calls via unmanaged function pointer crash or otherwise corrupt process.

update 2 here illustration actual code. did takes single argument, if need adjusted additional arguments allow me know.

private sub initrelay(d1 action(of short)) 'this sub represents initrelay function exported library. wouldn't have straight in code. end sub private sub setbaud(baud short) 'this setbaud sub in module end sub 'this in form code, @ class level (outside subs) private setbauddelegate new action(of short)(addressof setbaud) 'this sub whatever sub in code calls initrelay private sub test() initrelay(setbauddelegate) end sub

vb.net delegates visual-studio-2013 function-pointers

No comments:

Post a Comment