LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to deal with the callback when I invoke a C++ dll using CLF?

The prototype is like this:
 
WORD SetCallback(WORD comHandle, (*callback)(int, int) Callbackfunction), the callbackfunction should be work when the COM1 port has a DTR change, I guess SetCallback monitor this change, maybe, in fact, I must write a Callbackfunction by VI, the custom do not have a Callbackfunction to deal with DTR change event. So, SetCallback is in dll but Callback function is some VI code.
 
This method is one of a group function in a dll, there are two functions can not work in all of them.
 
The first is SetCallback due to I can not integrate the SetCallback in dll and Callback in VI.
 
Another question is another function's prototype is WORD SetTestType(WORD comHandle, bool type1, bool type2, bool type3),   I tried to use a boolean control and a Boolean to (0,1) node in labVIEW to pass parameters to CLF, I config the bool input value as U8(according to C++ bool type) and U32(according to WIN32 BOOL type), it dose not help, when I execute the VI, LabVIEW will be disappeare, it seems to be closed, I must start it again.
 
Could someone has similar experience on this points?
********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 1 of 13
(5,475 Views)
It's not possible to write a callback function in labview.

One way to deal with this is to write your own DLL "wrapper" which can call the SetCallback function.  Your wrapper can pass any function it wants as the callback, and labview only has to interact with your wrapper.

I'm not sure of the prototype for your callback function, but here's an example.

typedef int (*CallbackTypedef)(int, int);
WORD SetCallback(WORD comHandle, CallbackTypedef Callbackfunction);

int callback( int , int ) {
  /* define your callback here */
  return 0;
}

__declspec(dllexport) WORD LV_wrapper( WORD comHandle ) {
    return SetCallback( comHandle , callback );
}

For your other question, you need to figure out how your compiler is interpertting the bool type.  This program should tell you quickly which type to use:

#include <stdio>

int main() {
    printf( "Sizeof bool: %d\n" , sizeof(bool) );
    return 0;
}


0 Kudos
Message 2 of 13
(5,464 Views)
Hi  Adambrews
 
What you told me to do just what I can not to do:)
The dll is from custom but just with a simple document which describe the prototype of the functions, I do not have source code due to it is a custom confidencial thing, the dll has the invoker which is named as SetCallback(), but do not have the callback function,so I need to produce callback and integrate it with SetCallback() from dll, so I think it is an unnormal way to do this.
 
I think bool can not go out of U8 or U32, I tried all the number type in CLF, it dose not work. I pass a boolean and convert it using Boolean to (0,1) node but not helpful.
********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 3 of 13
(5,456 Views)

Hi Paulbin,

Do you want to supply a function-pointer to a DLL so it can call[back] your VI?  Here's how it can be done with .NET, though, that solution requires a C compiler. Smiley Sad  A single "VI_to_function-reference" utility could handle all cases where the callback passes no parameters.  Maybe someday somebody will share that dll.Smiley Happy

Did you know that LabVIEW can use Events for some COM-signals change?  See pic.  (Note: DTR is not listed - Smiley Sad - )


Another question is another function's prototype is WORD SetTestType(WORD comHandle, bool type1, bool type2, bool type3),   I tried to use a boolean control and a Boolean to (0,1) node in labVIEW to pass parameters to CLF, I config the bool input value as U8(according to C++ bool type) and U32(according to WIN32 BOOL type), it dose not help, when I execute the VI, LabVIEW will be disappeare, it seems to be closed, I must start it again.
 
Could someone has similar experience on this points?

It's easy to crash LabVIEW misusing CLF - maybe there's another reason for the crash besides bool mis-match(?)  Do you know which "Calling convention" is required?  What type is comHandle? (where does comHandle come from?) 

Cheers!

 

Message Edited by tbd on 03-23-2007 09:34 PM

Message Edited by tbd on 03-23-2007 09:35 PM

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 4 of 13
(5,454 Views)

Hi tbd

I have read the post which is use .net to handle this case last night. but I have not went to more deeper:)

I known VISA event can do this but the dll function from custom is handling this, I do not know the SetCallback_DTR function just only monitor DTR, if it is, I can use VISA to instead, but if not, I will lose something in it. The custom dll has a SetCallback_DTR function without Callback function, so I need to do this function by VI and make it is known by the caller SetCaback_DTR. I tried to pass a VI reference to the CLF node but unsuccessful.

And, the comHandle comes from open com function

1.1      WORD AS_CreateComInstance(…)

Argument 1: [in] WORD i_wMode

Argument 2: [in] WORD i_wComNr

Argument 3: [out] WORD* i_pwHandle

this handle is used for all the process like a VISA session.

If I use VISA event, how can I handle the com handle from the dll?

Calling convention is _cdecl.

********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 5 of 13
(5,442 Views)
Hi tbd
Another two question as below:
 
1 Other functions in this dll have been invoked successful.
 
2 In that .net post, it is based on .net2005 and LabVIEW8.0, but on my PC just .net2003 and LabVIEW 7.1. the Callback Register node is not appear under communication/.net pallet in LabVIEW. I do not know it is can be instead by ActiveX/CallbackRegisterNode.
********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 6 of 13
(5,442 Views)
up
********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 7 of 13
(5,425 Views)
Hi Paulbin,
      You may be "out of luck" re:supplying a callback function-reference to the DLL.  I have a service-request with NI on exactly this subject, but it's an uncommon request, so, difficult to obtain knowledgable help.
 
I suspect that the bool problem is something simple you haven't tried (assuming the DLL is OK.)  Since you're running out of options, perhaps the free utility "Olly Debug" may help give you information about what the DLL needs.  MS provides debugging tools too.  Very sorry, but I'm simply out of ideas. Smiley Sad
 
Cheers!
"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 8 of 13
(5,425 Views)

Hi Alan

 

Just got your name right now.

Thanks very much for your sincere reply.I am trying to look for the callback function from the custom, and I will wrap the caller and callback in another dll using CVI, hope this helpful. I know that LabVIEW8.2 has a callback feature in CLF node:)

I will try to use Olly to track the ASM code to find out whether the bool is a 8bit or 16bit or more:)

Thanks again.

********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 9 of 13
(5,422 Views)
Hi Paulbin,
      Just  for the record, may name isn't "Alan Greenspan"! Smiley Wink
 
Cheers,
"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 10 of 13
(5,418 Views)