Jump to content

How to successfull call a DLL function that requires callback


Recommended Posts

Hi every LabVIEW guru,

I have a bunch of DLLs that I would like to call in LabVIEW. In the quest of figuring out how to do that, I found out that I have almost all of them use function callback as a way of passing error messages. I found that there are 2 ways they do the callback:

1. There is a pair of functions called sth like RegisterCallback and UnRegisterCallback, which take a pointer to a function as an argument. I supposed these 2 functions must be called before I call any other functions in the DLL. There is the function prototypes in the header file I have.

Errorcode_t mefRegisterCallback (callbackfunc_t* const func, callbackevent_t eventtype);

Errorcode_t mefUnregisterCallback (callbackevent_t eventtype);

2. Another type I encounter is just functions which take a struct of pointers to functions, sth like:

int __stdcall DoSomething(InputStruct *In, OutputStruct *Out, MessagesCallback *Msg)

in which the MessagesCallback has the form:

struct MessagesCallback

{

void ( *pfnErrorMessageHandler )( int, int, const char *, const char * );

void ( *pfnMessageHandler )( const char * );

char szMessage[1024];

};

I played around with LabVIEW call library function node but I think it is impossible to deal with the function callback without using some tricks. There is a post talking about similar issue and someone purposed a workaround using ActiveX server here. But since the links are no longer available I still dont know what is the workaround. I also know that you can create another DLL as a wrapper to those functions but since I dont have much experience in DLL development can someone elaborate about this? Thanks in advance.

Link to comment

QUOTE (kawait @ Apr 18 2008, 02:56 PM)

Hi every LabVIEW guru,

I have a bunch of DLLs that I would like to call in LabVIEW. In the quest of figuring out how to do that, I found out that I have almost all of them use function callback as a way of passing error messages. I found that there are 2 ways they do the callback:

1. There is a pair of functions called sth like RegisterCallback and UnRegisterCallback, which take a pointer to a function as an argument. I supposed these 2 functions must be called before I call any other functions in the DLL. There is the function prototypes in the header file I have.

Errorcode_t mefRegisterCallback (callbackfunc_t* const func, callbackevent_t eventtype);

Errorcode_t mefUnregisterCallback (callbackevent_t eventtype);

2. Another type I encounter is just functions which take a struct of pointers to functions, sth like:

int __stdcall DoSomething(InputStruct *In, OutputStruct *Out, MessagesCallback *Msg)

in which the MessagesCallback has the form:

struct MessagesCallback

{

void ( *pfnErrorMessageHandler )( int, int, const char *, const char * );

void ( *pfnMessageHandler )( const char * );

char szMessage[1024];

};

I played around with LabVIEW call library function node but I think it is impossible to deal with the function callback without using some tricks. There is a post talking about similar issue and someone purposed a workaround using ActiveX server http://forums.lavag.org/callback-from-dll-function-t647.html' target="_blank">here. But since the links are no longer available I still dont know what is the workaround. I also know that you can create another DLL as a wrapper to those functions but since I dont have much experience in DLL development can someone elaborate about this? Thanks in advance.

It's not impossible but in fact to much trouble to bother with or even just explain it. All I will say here is that you would have to create a LabVIEW DLL with an exported function that matches your callback prototype and then using the Call Library Node calling LoadLibrary()/GetProcAdress() and possibly some more help functions. If you are a proficient C programmer you can come up with the exact solution but writing a wrapper DLL that translates between a callback and a LabVIEW event such as an occurrence or a user event is definitely not more complicated and a lot easier to manage and maintain. This is even true if you are not such a proficient C programmer because the LabVIEW DLL approach will still be at least as complicated to get right than writing a wrapper DLL in C and the difficulty to manage and maintain the separate LabVIEW DLL solution in LabVIEW applications will be also bigger if you do not understand DLLs and their interaction fairly good and especially the way LabVIEW runs LabVIEW DLLs in the development system versus in a separate runtime system.

Rolf Kalbermatter

Link to comment

QUOTE (rolfk @ Apr 19 2008, 03:22 AM)

It's not impossible but in fact to much trouble to bother with or even just explain it. All I will say here is that you would have to create a LabVIEW DLL with an exported function that matches your callback prototype and then using the Call Library Node calling LoadLibrary()/GetProcAdress() and possibly some more help functions. If you are a proficient C programmer you can come up with the exact solution but writing a wrapper DLL that translates between a callback and a LabVIEW event such as an occurrence or a user event is definitely not more complicated and a lot easier to manage and maintain. This is even true if you are not such a proficient C programmer because the LabVIEW DLL approach will still be at least as complicated to get right than writing a wrapper DLL in C and the difficulty to manage and maintain the separate LabVIEW DLL solution in LabVIEW applications will be also bigger if you do not understand DLLs and their interaction fairly good and especially the way LabVIEW runs LabVIEW DLLs in the development system versus in a separate runtime system.

Rolf Kalbermatter

Thank you for your detailed reply, rolf :)

Since a guru like you have said that writing a wrapping DLL is the best solution I would definitely take the time and explore it. Even though I consider myself reasonably proficient in C/C++, I don't have much experience about Windows programming and those DLL stuff. So I need time to play around with Visual Studio and figure out how to write a DLL first (and probably get some program crashes in the process since I know that calling DLLs can be such a nasty thing :blink: ). However, because of my tight schedule I would like to know if there is any resources (from here or NI website) that can teach me how to do that quickly? Especially the techniques about translating between a callback to a LabVIEW user event (which sounds pretty cool for me :thumbup: )

I will post again in this forum if I have more question. Thanks again!

kawait

Link to comment

QUOTE (kawait @ Apr 18 2008, 09:54 PM)

Thank you for your detailed reply, rolf :)

Since a guru like you have said that writing a wrapping DLL is the best solution I would definitely take the time and explore it. Even though I consider myself reasonably proficient in C/C++, I don't have much experience about Windows programming and those DLL stuff. So I need time to play around with Visual Studio and figure out how to write a DLL first (and probably get some program crashes in the process since I know that calling DLLs can be such a nasty thing :blink: ). However, because of my tight schedule I would like to know if there is any resources (from here or NI website) that can teach me how to do that quickly? Especially the techniques about translating between a callback to a LabVIEW user event (which sounds pretty cool for me :thumbup: )

I will post again in this forum if I have more question. Thanks again!

kawait

Yes if you go to www.ni.com and do a search for +user +event +dll you will find eventually examples for that. As a shortcut you can also search specifically for PostLVUserEvent, which is the exported LabVIEW callback to post a user event to LabVIEW.

Rolf Kalbermatter

Link to comment

QUOTE (rolfk @ Apr 19 2008, 03:43 PM)

Yes if you go to www.ni.com and do a search for +user +event +dll you will find eventually examples for that. As a shortcut you can also search specifically for PostLVUserEvent, which is the exported LabVIEW callback to post a user event to LabVIEW.

Rolf Kalbermatter

Thanks for your info, I will spend this weekend to study the example code, If I have more questions I will post them in this board.

Thanks again.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.