[IronPython] Using callback function

Dino Viehland dinov at exchange.microsoft.com
Tue Mar 4 20:24:56 CET 2008


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



More information about the Ironpython-users mailing list