[IronPython] Using callback function

Harri Vartiainen harri.vartiainen at iki.fi
Tue Mar 4 21:36:58 CET 2008


 Thank you for fast answer.

 Unfortunately the "fosdk" library is from vendor and I can't change
that. The values of parameters are non-issue, I was lost with the
declaration of callback function and passing the function to
FOSDKSetCallback.

 I defined my own delegate (or copied from example I found from documentation):

Imports System
Imports System.Runtime.InteropServices

public class MyDeleg

Public Delegate Function MyCallback(ByVal pCBContext As Integer, ByVal
SampleTime As Double, _
                                          ByVal pBuf As IntPtr, ByVal
lBufferSize As Integer) As Integer
End Class

 Compiled it and all seemed go OK until the conversion to Int64, so I
got to this:

from System.Runtime.InteropServices import Marshal

FOSDKSetCallback(0,
Marshal.GetFunctionPointerForDelegate(MyDelegate(myCallback)).ToInt64(),
0)

 Seems pretty ok for now, next I've to figure out how to get the whole
system working and start receiving callbacks, but that's different
problem..

 Some more VB.NET samples:


'Delgate for callback function
Public Delegate Function MyCallback(ByVal pCBContext As Integer, ByVal
SampleTime As Double, _
                                          ByVal pBuf As IntPtr, ByVal
lBufferSize As Integer) As Integer

<snip>

    Private cb As MyCallback    'If the callback is called
asynchronously, you have to ensure that the
    'callback delegate is alive as long as the function poitner is needed.

<snip>

cb = AddressOf MyCallbackFunc

<snip>

FosdkApi.FOSDKSetCallback(g_iCrntCam, cb, 12)

<snip>

    Public Function MyCallbackFunc(ByVal pCBContext As Integer, ByVal
SampleTime As Double, _
                                          ByVal pBuf As IntPtr, ByVal
lBufferSize As Integer) As Integer


        sTime = SampleTime
        bSize = lBufferSize

        ThreadPool.QueueUserWorkItem(AddressOf ShowCallbackMsg)     '
show messages in a seperate thread

        Return 1
    End Function



On Tue, Mar 4, 2008 at 9:24 PM, Dino Viehland
<dinov at exchange.microsoft.com> wrote:
> Calling it isn't really the interesting part, the interesting part is what arguments do you pass to it.
>
>  To call it you just need to import the class that the function is defined in.  You can do that using:
>
>  import clr
>  clr.AddReference('VBAssemblyName')
>  from VBNamespaceName import *
>
>  where VBAssemblyName is the name of the VB assembly and VBNamespaceName is the name of the namespace this type lives in.  From there you should be able to do:
>
>  VBTypeName.FOSDKSetCallback(...)
>
>  Then the question becomes what do you pass for nCamIndex, pCallbackFunc, and pCBContext.
>
>  But before we go there there's a few interesting things about the signatures.  First is that they're declared as taking Int64's instead of IntPtr's.  If this is your own DLL then you probably want to change these to IntPtrs so they're passing the right size data on both 32-bit and 64-bit platforms.  Then your callback function would look something like:
>
>  Public Declare Function FOSDKSetCallback Lib "FOSDK" (ByVal nCamIndex As IntPtr, ByVal pCallbackFunc As Long, ByVal pCBContext As IntPtr) As Long
>
>  The 2nd oddity is that pCallbackFunc is not taking a delegate.  That means you'll need to manually call Marshal.GetFunctionPointerForDelegate on a delegate object.  Which also means you're going to need to manually construct the delegate object.  Again if you can change the signature you probably want to have this taking a delegate typed something like (using the C# syntax here):
>
>  delegate int MyDelegate(IntPtr pContext, double sampleTime, IntPtr pBuf, int lBufferSize);
>
>  Then your VB function would end up looking something like:
>
>  Public Declare Function FOSDKSetCallback Lib "FOSDK" (ByVal nCamIndex As Long, ByVal pCallbackFunc As MyDelegate, ByVal pCBContext As IntPtr) As Long
>
>  >From there you should be able to do:
>
>  def myCallback(context, sampleTime, buffer, bufferSize):
>         print context, sampleTime, buffer, bufferSize
>
>  from System import IntPtr
>  FOSDKSetCallback(0, myCallback, IntPtr.Zero)
>
>  The only interesting thing then is what you need to pass in for nCamIndex, hopefully the library documentation will explain that :)
>
>  If you can't change the signature you'll still need to define the delegate type somewhere (which you can't do from IronPython).  Then your call will probably end up looking like:
>
>  from System import Marshal, Int64
>  FOSDKSetCallback(0, Int64(Marshal.GetFunctionPointerForDelegate(MyDelegate(myCallback))), 0)
>
>
>
>
>  -----Original Message-----
>  From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Harri Vartiainen
>  Sent: Tuesday, March 04, 2008 9:24 AM
>  To: users at lists.ironpython.com
>  Subject: [IronPython] Using callback function
>
>  Hi,
>
>   I'm trying to use callback function of dll. The API has VB.NET
>  definition for callback:
>
>  Public Class FosdkApi
>  ...
>    ' set callback function
>     'FOSDKLIB_API BOOL WINAPI FOSDKSetCallback(IN INT nCamIndex,
>     '                                          IN INT (CALLBACK
>  *pCallbackFunc)(VOID *pContext, DOUBLE SampleTime, BYTE *pBuf, LONG
>  lBufferSize)
>     '
>                 IN VOID *pCBContext);
>
>     Public Declare Function FOSDKSetCallback Lib "FOSDK" (ByVal
>  nCamIndex As Long, ByVal pCallbackFunc As Long, ByVal pCBContext As
>  Long) As Long
>
>   Help(FosdkApi.FOSDKSetCallback) prints:
>
>  Help on built-in function FOSDKSetCallback
>
>   |  FOSDKSetCallback(...)
>   |          Int64 FOSDKSetCallback(Int64 nCamIndex, Int64
>  pCallbackFunc, Int64 pCBContext)
>
>   So how I can use that?
>  _______________________________________________
>  Users mailing list
>  Users at lists.ironpython.com
>  http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>  _______________________________________________
>  Users mailing list
>  Users at lists.ironpython.com
>  http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>



More information about the Ironpython-users mailing list