LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Callback function for class member

Hello,
 
I'm using LabWindows/CVI data socket and Timer into the MSCV compiler. Everything works fine, but when I make it a module of C++ class and put the DS Callback function into a memberfunction, it causes error, especially with calling convention __cdecl.
Think this is because I called those callback functions from a class member... Tried make it static, but more problems follwed..
Is there any way to solve this problem?
 
Thanks.
 
0 Kudos
Message 1 of 4
(3,517 Views)
Hello,

This how I resolved the problem :

Here is the declaration of my CCviSocket class:

class CCviSocket
{
public:
CCviSocket();
virtual ~CCviSocket();

bool IsConnected();
bool Connect(const char *zAddress, int nPort, int timeout=1000);
bool Disconnect();

int Recv(void *pData, int nMaxLen, int timeout=1000);
int Send(const void *pData, int nLen, int timeout=1000);
protected:
unsigned int m_nSocketHandle;
virtual void OnDataReady();
virtual void OnDisconnect(int error);

private:
static int CVICALLBACK TCP_Callback (unsigned handle, int event, int error, void *callbackData);
};

And here is the implementation of the Connect() and the TCP_Callback() method:

bool CCviSocket::Connect(const char *zAddress, int nPort, int timeout)
{
int ret;

if (m_nSocketHandle!=INVALID_SOCKET)
{
return false;
}

ret=ConnectToTCPServer(&m_nSocketHandle,
nPort,zAddress,
TCP_Callback,reinterpret_cast(this),
timeout);

if (ret!=0)
{
return false;
}

return true;
}

int CVICALLBACK CCviSocket::TCP_Callback(unsigned int handle, int event, int error, void *callbackData)
{
CCviSocket *pSocket;

pSocket=reinterpret_cast(callbackData);
if (pSocket)
{
assert(pSocket->m_nSocketHandle==handle);

switch (event)
{
case TCP_DATAREADY:
pSocket->OnDataReady();
break;

case TCP_DISCONNECT:
pSocket->OnDisconnect(error);
break;
}
}
return 0;
}

Hope this helps...
0 Kudos
Message 2 of 4
(3,510 Views)

Thanks for that.

Well, I would rather seek for a general solution for the dialog box callback of LabWindows/CVI.

For the dialog window, there exist several callbacks, such as int CVICALLBACK ExitReadCB(...), or int CVICALLBACK ClosePanelCB(...), etc. These were originally set up as gloval functions. Data socket is one of these global functions defined right in the dialog window. I want to make these dialog window wrapped in one class, so that I can call it in other class's constructor.

The difficulty for me is to make these functions as member functions. When I make member function static, I also get problems with the all handles and variables for Data socket. Especially, I got the error:

  errorCode=DS_Open (Source_String, DSConst_Read, DSCallback, NULL, &DSReadHandle);

Here, the compiler complaints the third parameter (DSCallback) cannot be converted from int func () to int __cdecl func(). I guess it's because of the calling convention in MSVC.

Could give me some advice for more general case like this?

Thanks a lot.

0 Kudos
Message 3 of 4
(3,498 Views)
If you want the callback function to be a method of your class, it MUST be static.

That because it's the only way you can declare your function as __cdecl.

The solution I use is that describe in my example :
- declare the callback functions as static
- pass the object pointer in the callbackdata
- then call each object method corresponding to received events.
0 Kudos
Message 4 of 4
(3,479 Views)