LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement callback function when DLL file is called

 

.h file is as follows:

IMVS_RegisterImageResultCallBack(IN void* handle,
void(__stdcall* cbGetImageResult)(char* pchType, unsigned int nFilterID, unsigned int nFrameID, char* pchResult, int nDataLen, int nImageWidth, int nImageHeight, int nImageFormat, void* pUser),
void* pUser);

 

I want to call this DLL file with CLN, but I don't know how to deal with the callback

0 Kudos
Message 1 of 6
(3,570 Views)

The callback function is provided by it's pointer. The only (decent) way to get a pointer to a function is to put the function in a dll and use LoadLibrary and GetProcAddress to get the pointer to the function. The dll can be made in LabVIEW, but since LV2011(?), the dll's will run in a separate context. So you'll need another function to get the information back into LabVIEW, that was stored by the callback function (in a functional global for instance). It's tedious, since iterated builds of the .dll require the dll to be removed from memory (or the file will be locked)...

 

A wrapper .dll might be a better option.

0 Kudos
Message 2 of 6
(3,546 Views)

Thanks for your answer, but I still don't understand it. Is there a similar example?

0 Kudos
Message 3 of 6
(3,541 Views)

What does wrapper.dll mean and how can it be implemented?

0 Kudos
Message 4 of 6
(3,537 Views)

A wrapper dll (say  w.dll) is a dll that you write, that encapsulates calls to the original dll (o.dll). You do this mainly to abstract some difficulties to call directly o.dll. For instance, LabVIEW does not like complex nested C structures so it's common to split the structure in w.dll so LabVIEW calls functions which parameters are simple (integers, strings). Also, LabVIEW does not know pointers, and especially not function pointers, and the usual workaround is this: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000P8XJSA0

 

Note you should have some C knowledge to go down that road (or find someone who does).

 

--Eric

Eric M. - Senior Software Engineer
Certified LabVIEW Architect - Certified LabVIEW Embedded Systems Developer - Certified LabWindows™/CVI Developer
Neosoft Technologies inc.

0 Kudos
Message 5 of 6
(3,532 Views)

@basler_Giga-E_camera wrote:

Thanks for your answer, but I still don't understand it. Is there a similar example?


Your trying to do something that is very difficult to do in LabVIEW. Without a background in C\C++ it's hard to understand. But if you want to use this API in LabVIEW, you'll have to learn.

 

The callback needs a pointer to a function. A wrapper would be able to handle that. But you can also make the function in LabVIEW. LabVIEW does not return a pointer to the VI though, and the only way to get a pointer to the function is to compile it in a dll. So you'll need to:

 

a) Make a callback VI with the correct inputs

b) Build it into a .dll with the correct prototype (all inputs\outputs need to be correct)

c) use LoadLibrary\GetProcAddress to get a pointer to the callback function in the dll

 

A wrapper would do more or less the same:

a) Make a callback function with the correct inputs

b) Build it into a .dll with a function that LabVIEW can call

c) The function calls the dll with a pointer to the callback function

 

Other options:

1) Contact the creators of the dll, maybe they can provide more LabVIEW friendly API.

2) Explore the dll, maybe there are more LabVIEW friendly functions (often callbacks are used to enable advanced synchronization mechanisms).

3) Look for another library that has a more LabVIEW friendly API.

 

What library are you using anyway?

0 Kudos
Message 6 of 6
(3,513 Views)