Provide a Callback Function Pointer to My DLL Using LabVIEW

Updated Aug 7, 2018

Reported In

Software

  • LabVIEW Base

Issue Details

I have a DLL with a function prototype that includes a pointer to a callback function that I need to call in LabVIEW.  I would like to have my callback function pointer reference a VI in memory.  How can I call this DLL in LabVIEW and pass a pointer to a VI as the callback function parameter?

 

Solution

LabVIEW cannot natively pass a pointer to a VI for use as a callback function in a DLL.  However, a C-based wrapper can be used in this scenario as a workaround.  The C wrapper is used to provide an interface between LabVIEW's API and the DLL's parameters. 
  1. Create a User Event in LabVIEW and register it with an Event Structure, which executes the desired callback VI upon assertion of the registered User Event. 
    • The wrapper DLL should obtain this event refnum from LabVIEW via the Call Library Function Node VI, and then implement a method (e.g. myWrapperMethod) that uses this event refnum to call the C function, PostLVUserEvent
  2. Inside myWrapperMethodPostLVUserEvent receives two input parameters, ref (LabVIEW event refnum) and data (pointer to void), where data can be any data output from the original DLL that should be passed along to the VI callback function.
    • NOTE: This may require typecasting and/or parsing of data in the wrapper layer in order to convert the DLL's output to a LabVIEW-compatible data type.  For further information on this, please consult your DLL's header file, as this will be application specific. 
  3. Add a line in the main function of your wrapper to call the original DLL and pass a pointer to your wrapper method, myWrapperMethod, as the DLL's callback function parameter. 
  4. When the original DLL sets the call stack's pointer to the callback function address, the wrapper function, myWrapperMethod, will be invoked and trigger the specified user event in LabVIEW via PostLVUserEvent.
    • This will, in turn, execute the event case associated with the intended callback VI and pass back any relevant data via the data parameter to LabVIEW.

WAS THIS ARTICLE HELPFUL?

Not Helpful