LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement a callback function using LabView's Call Library Function Node?

Solved!
Go to solution
I am trying to call a fuction from a SDK.dll library using the Call Library Function Node. The SDK was provided to

me and I do not have the source code, just the .dll and .h files.


The SdkSetPropertyEventHandler function has a callback fuction as one of its parameters. How do I implement the

callback using the CLF node? I am a good LabView programmer but this is my first time using the Call Library

Function Node. I have read all the info I can find on NI's web site and the discussion board but cannot figure

this one out. I am using LabView 8.6.



The SDK.h deacribes the function as:

//  Function:   SdkSetPropertyEventHandler


SdkError SDKAPI SdkSetPropertyEventHandler(
            SdkCameraRef                    inCameraRef,
            SdkPropertyEvent                inEvnet,          
            SdkPropertyEventHandler         inPropertyEventHandler,
            SdkVoid*                        inContext );


//             
//  Description:
//       Registers a callback function for receiving status
//          change notification events for property states on a camera.
//
//  Parameters:
//       In:    inCameraRef - Designate the camera object.
//              inEvent - Designate one or all events to be supplemented.
//              inPropertyEventHandler - Designate the pointer to the callback
//                      function for receiving property-related camera events.
//              inContext - Designate application information to be passed by
//                      means of the callback function. Any data needed for
//                      your application can be passed.
//      Out:    None
//
//  Returns:    Any of the sdk errors.
-----------------------------------------------------------------------------*/




A separate header file called SDKTypes.h contains the following data:


typedef  SdkUInt32  SdkPropertyEvent;
typedef  SdkUInt32  SdkPropertyID;
typedef  void       SdkVoid;

typedef  struct __SdkObject*    SdkBaseRef;
typedef  SdkBaseRef    SdkCameraRef;



/*-----------------------------------------------------------------------------
 SdkPropertyEventHandler
-----------------------------------------------------------------------------*/
typedef SdkError ( SDKCALLBACK *SdkPropertyEventHandler )(
                    SdkPropertyEvent        inEvent,
                    SdkPropertyID           inPropertyID,
                    SdkUInt32               inParam,
                    SdkVoid *               inContext );






Thanks for your help.
Alejandro
Message 1 of 24
(11,863 Views)

alejandroandreatta wrote:
I am trying to call a fuction from a SDK.dll library using the Call Library Function Node. The SDK was provided to

me and I do not have the source code, just the .dll and .h files.


The SdkSetPropertyEventHandler function has a callback fuction as one of its parameters. How do I implement the

callback using the CLF node? I am a good LabView programmer but this is my first time using the Call Library

Function Node. I have read all the info I can find on NI's web site and the discussion board but cannot figure

this one out. I am using LabView 8.6.


Basically you do not do that. LabVIEW does not know pointers and certainly not function pointers. What you should do instead is writing a C DLL that implements the callback and also exports a function to be called by LabVIEW that translates between the callback and a LabVIEW user event. Look for PostLVUserEvent() here on the NI site to find examples how to do that.

 

Rolf Kalbermatter

Message Edited by rolfk on 02-11-2009 08:00 PM
Rolf Kalbermatter
My Blog
Message 2 of 24
(11,847 Views)

Hello,

 

All you have to do is point the call library function at your dll and input the correct parameters.  The correct inputs would be inCameraRef,inEvnet,inPropertyEventHandler,inContext . There are no outputs the the SdkSetPropertyEventHandler. Try running and looking at this vi to see how the Call Library Function calls functions from a dll. Make sure you find out what calling convention your dll uses. If you don't know off hand you may just want to change it, if your call does not work correctly.

 

National Instruments
RIO Embedded Hardware PSE

CompactRIO Developers Guide
0 Kudos
Message 3 of 24
(11,821 Views)

Andrew_E. wrote:

Hello,

 

All you have to do is point the call library function at your dll and input the correct parameters.  The correct inputs would be inCameraRef,inEvnet,inPropertyEventHandler,inContext . There are no outputs the the SdkSetPropertyEventHandler. Try running and looking at this vi to see how the Call Library Function calls functions from a dll. Make sure you find out what calling convention your dll uses. If you don't know off hand you may just want to change it, if your call does not work correctly.

 


inPropertyEventHandler is a callback function pointer. Nice trick if you get the Call Library configuration dialog to create the right parameter for that!!!!!!

 

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
Message 4 of 24
(11,816 Views)

Hi Rolfk,

 

You are completely right. Does this article describe what you were talking about? I am just trying to get as much information on this thread as possible so the solution is easier to find for the future. Thanks.

National Instruments
RIO Embedded Hardware PSE

CompactRIO Developers Guide
0 Kudos
Message 5 of 24
(11,794 Views)
Solution
Accepted by topic author alejandroandreatta

Andrew_E. wrote:

Hi Rolfk,

 

You are completely right. Does this article describe what you were talking about? I am just trying to get as much information on this thread as possible so the solution is easier to find for the future. Thanks.


Yes, but I find the idea to use .Net to call a LabVIEW VI as C callback function a bit like using a steam roller to iron your pants. Why do it? Well the steam roller has some extra safe guards that make it less likely that you crash your pants in the process but it is not elegant IMHO.

 

Writing a C DLL that translates between a C callback and a LabVIEW user event using the PostLVUserEvent() interface function from the LabVIEW runtime kernel seems so much eleganter to me. Yes it is a bit of C programming but hey!

 

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
Message 6 of 24
(11,784 Views)

Rolf and Andrew,

 

Thank you for the advice. I will look for a C programmer to write the wrapper to the DLL and make it more accessible to LabView.

 

 

Alejandro

0 Kudos
Message 7 of 24
(11,747 Views)

Hi Alejandro

did you succeed?

 

I've been having a very similar problem; I got to solve part of it.

http://forums.ni.com/ni/board/message?board.id=170&message.id=416425&query.id=1271894#M416425

 

I ended up writing the callback function in C, and then using Wiebe's subVIs to get the function pointer.

 

 

Maybe it helps. 

 

 

At first I was so confused about function pointers, found a decent tutorial here

 

 

-Javier 

0 Kudos
Message 8 of 24
(11,421 Views)

Rolf,

 

I've been searching for info on how to use the PostLVUserEvent(), but have only found one page on the NI site that gives a brief explanation

 

http://zone.ni.com/reference/en-XX/help/371361F-01/lvexcode/postlvuserevent/

 

I can't it anywhere in my LV directory (LV2009). Can you point me to some information on how to use this call?

 

George

0 Kudos
Message 9 of 24
(11,000 Views)
If I go to the top of this page and enter the name of the function in the search box I do get 40 links. That is about 4000% more than what you claim to be able to find. Googling that name gives even more hits elsewhere on the net.
Rolf Kalbermatter
My Blog
0 Kudos
Message 10 of 24
(10,994 Views)