Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

InstallEventHandler

The Visual C++ compiler does not recognize the function template for InstallEventHandler in NiVisa.h, but only the __cdecl one, error C2664. It basically expects the function pointer as the second parameter.(Compilation works with the static function.)

My code snippets:
// this line in the constructor of my class
viStatus = session->InstallEventHandler(VisaEventServiceRequest, this, EventHandler, eventHandlerId);

ViStatus CCDMAtesterE8285A::EventHandler(CNiVisaEvent& event) {
return VisaSuccess;
}
0 Kudos
Message 1 of 4
(3,359 Views)
Change the line in your class constructor to:

viStatus = session->InstallEventHandler(VisaEventServiceRequest, *this, OnVisaEventHandler(CCDMAtesterE8285A, EventHandler), eventHandlerId);

This fixes two problems with the original line that would keep it from compiling:
(1) The second parameter is a reference to the object containing the event handler. "this" is a pointer, so it must be dereferenced to create an object reference by putting "*this".
(2) There is a limitation in the way the Visual C++ compiler generates pointers to member functions. To work around this limitation, the "OnVisaEventHandler" macro is supplied. This macro is required when you want to install an event handler that is a member of the current class.

I hope that helps.

Ton
y H.
Measurement Studio
0 Kudos
Message 2 of 4
(3,359 Views)
Thank you very much for your quick response. You saved me a few hours of fiddling and farting.

If you like to know the details of my mistakes except the ones you mentioned you might read on.

I had, before I posted my question, tried the syntax you mentioned and which I had found on this discussion forum, but to no avail. The following analysis of my mistakes might help others to avoid them. The actual trap was the auto list members feature in the Visual C++ editor. It listed only the __cdecl InstallEventHandler. Using this list of arguments produced of course a compilation error, since I had not declared my EventHandler as static. I soon realized that the static function was of new use, looked in the NiVisa.h file and found the function template. Then I
started fiddling around with the syntax, having forgotten in the late hours that my EventHandler member function was still declared as static. Visual C++ verbose compilation errors didn't help at all.

Well, that's a programmer's story, most of us know all too well. To summarize, don't believe too much in the auto list member feature, or whatever it is called (the yellow syntax help).
0 Kudos
Message 4 of 4
(3,359 Views)
Hi Gunni,

If I understand correctly, the function EvenHandler is a member function of your class CCDMAtesterE8285A. If this is the case, is your session object also a member of this class? If so, then you need to use the OnVisaEventHandler macro on the handler parameter of the InstallEventHandler function as follows:

viStatus = session->InstallEventHandler(VisaEventServiceRequest, *this, OnVisaEventHandler (CCDMAtesterE8285A, EventHandler));

Also, notice that you need to dereference your handler object (this) since the prototype of the function expects a reference to it. If you search for "installevent" on the Measurement Studio online help, you'll be able to see snippets of code that show how to pass those parameters correctly.
That should do it, but let
me know otherwise.

Regards,
Azucena
NI
0 Kudos
Message 3 of 4
(3,359 Views)