Re: callback from C DLL returning an int [SEC=PERSONAL]
"Andrew MacIntyre" <[email protected]> Fri, 15 Jul 2011 00:52:55 +0000
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <A1E4E9D08D14924498E135FB4441EE8D07196D2D@act01exmbx01vp.internal.govt> |
From: RayS
> My ctypes attempt is crashing the wxPython app.
>
> This is how SetCallback is defined in the DLL:
>
> extern "C" void __declspec(dllexport) SetCallback( long lAddress )
> {
> s_frontend.set_callback((FUNCPTR)lAddress );
> }
> //The function takes in a 32-bit long pointer to a function.
> //The function when defined in a calling C program should take in one parameter such as:
>
> void ASLocalCode::call_back( int iMessage )
>
> //This is fired when a new block of data is available. iMessage integer value is always 6.
>
>
> In Python I did this:
> ______________________________________
> ...
> class myADC:
> def __init__:
> ...
> self.driver = cdll.dataDll
> ...
> def data_ready_func(r):
> print r
>
> CB_TYPE = CFUNCTYPE(None, c_int)
> self.data_ready_ref = CB_TYPE(data_ready_func)
> print CB_TYPE, self.data_ready_ref
> self.driver.SetCallback(self.data_ready_ref)
>
> This prints:
> data_ready_func 6
> then XP pops up a notice "python.exe has encountered a problem" (the app is still running!), then it all closes with the XP dialog "don't send" button.
> What is wrong here?
The only thing that looks to me like a potential problem is that you don't keep an explicit reference to the callback function - the "def" just creates it at runtime in the scope of the __init__().
I'd try adding something like
self.callback = data_ready_func
just after defining the callback, to test this theory.
-------------------------> "These thoughts are mine alone!" <---------
Andrew MacIntyre Operations Branch
tel: +61 2 6219 5356 Communications Infrastructure Division
fax: +61 2 6253 3277 Australian Communications & Media Authority
email: [email protected] http://www.acma.gov.au/
NOTICE: This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all
copies of the original message.
------------------------------------------------------------------------------
AppSumo Presents a FREE Video for the SourceForge Community by Eric
Ries, the creator of the Lean Startup Methodology on "Lean Startup
Secrets Revealed." This video shows you how to validate your ideas,
optimize your ideas and identify your business strategy.
http://p.sf.net/sfu/appsumosfdev2dev