LabVIEW cannot natively pass a pointer to a VI for use as a callback function in a DLL.
However, an e.g. 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.
- 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, e.g. called PostLVUserEvent.
- Inside myWrapperMethod, PostLVUserEvent 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.
- 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.
- 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.