From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

OnVisaEventHandler() Macro

Can anyone tell me about this macro? It is mentioned briefly in the CNiVisaSession::InstallEventHandler() documentation, I'm unable to get it to work. I'm trying to install a callback handler for an VisaEventIoComplete event. I'd like the function to be a member function of the same class as the CNiVisaSession. Like so...

class MyDialog: public CDialog
{
...
CNiVisaSession *m_VisaSession;
ViStatus MyEventHandler(CNiVisaEvent& event);
...
}

I have been able to get the callback working if it is declared in a c style. I have also been able to get it working if it is declared as a member function of another class. This macro is supposed to allow you to have the callback in the same class, but it won't seem to work for me.

Thanks,
L
yle
0 Kudos
Message 1 of 5
(3,200 Views)
Lyle,
Yes you are right in that the OnVisaEventHandler macro is used if you want to install a member function of a class as a handler for a VISA event and this class also contains the CNiVisaSession object for which you are installing the event handler.
For example, if you have the following class definition,

class MyClass {
public:
void Init ();
ViStatus OnEvent (CNiVisaEvent& event);

private:
CNiVisaSession session;
};


and you want to install the OnEvent function as the handler for the VISA event of the CNiVisaSession object (session), you must use the OnVisaEventHandler macro as shown below.

MyClass::Init () {
session.InstallEventHandler(VisaEventTrigger, *this, OnVisaEventHandler(MyClass, OnEvent));
}


Failure to use this macro results in the following compiler error:

error C2661: 'InstallEventHandler' : no overloaded function takes 2 parameters

Note that you do not need to use this macro if you are installing a member function of an external class as the event handler.
For example, if you have the following class definition,

class ExternalClass {
public:
ViStatus OnEvent (CNiVisaEvent& event);
};

then, you can install ExternalClass::OnEvent in as MyClass::Init the event handler as shown below.

MyClass::Init () {
ExternalClass e;
session.InstallEventHandler(VisaEventTrigger, e, ExternalClass::OnEvent);
}

Good luck with your application,

Azucena
National Instruments
0 Kudos
Message 2 of 5
(3,200 Views)
Lyle,

This can be confusing and I frequently have trouble with it myself. For some reason, I always forget the asterisk on "this" and just use the keyword directly. This results in the same "no two-parameter overload" error message, even though the macro is being used. If you continue to have touble, can you post a code snippet?

--Chris
0 Kudos
Message 3 of 5
(3,200 Views)
Using the exact code above I get a 2664 error

error C2664: 'long __thiscall NI::CNiVisaSession::InstallEventHandler(unsigned long,long (__cdecl *)(class NI::CNiVisaEvent &),class NI::CNiVisaEventHandlerId &,void *)' : cannot convert parameter 2 from 'long (__thiscal
l CHandler::*)(class NI::CNiVisaEvent &)' to 'long (__cdecl *)(class NI::CNiVisaEvent &)'

Any help would be great.

Eric
0 Kudos
Message 4 of 5
(3,200 Views)
Eric:

From the compiler error, it looks like you may be using the wrong overload of CNiVisaSession::InstallEventHandler. There are three overloads for this function: two that are for installing static member or non-member global functions, and one that is for installing regular member functions of an object. It looks like the handler you want to install is on the CHandler class, so you'll want to use the third overload (the templatized one). In that case, your code would look one of the two code snippets Azucena presented previously. Be sure not to forget the second parameter, which should be an instance of the CHandler class.

-- Chris
0 Kudos
Message 5 of 5
(3,200 Views)