Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

how to create dll with datasocket

I am trying to write a JNI programme for a java agent to communicate with Labview. My idea is to create a dll file with Measurement Studio. My problem is that I can't connect datasocket server if the connect function is not in the construct function. For example:

CTestdllApp::CTestdllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance

m_DataSocket.Connect("dstp://localhost/wave",CNiDataSocket::Read);

}

double CTestdllApp::Retrunvalue()
{
m_DataSocket.Update();
data=m_DataSocket.GetData();
CNiReal64Vector points(data);
double a=points.GetSize();
double b=points[0];
return a;
}

extern "C" DllExport float GetValue()
{
return theApp.Retrun
value();
}

This programme works fine, but if I don't place code to connect datasocket in construction function CTestdllApp::CTestdllApp(), but in Retrunvalue() function, the programme can't connect to datasocket server.

Can anybody please explain why? And how to install datasocket even handler in dll?

Thank you very very much in advanced.
0 Kudos
Message 1 of 4
(3,242 Views)
The CNiDataSocket Connect function requires that your application processes Windows messages while the DataSocket is connecting. Your application can only process messages when it is not executing your code.

The reason the above works is because the constructor is called when the DLL is loaded and the DataSocket connects during the time in between the DLL being loaded and you calling GetValue.

If you put the Connect call in the Retrunvalue function so that you attempt to Connect and then retrieve a value from the DataSocket without ever leaving functions you wrote, then the DataSocket will never connect.

If the thread that your DLL is running in doesn't have any user interface objects, you can put a Windows message pump in it after you call Connect lik
e this:

CNiDataSocket::ConnectionStatus status;
do
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
status = myDataSocket.Status;
}
while ((status != ConnectionActive)
|| (status != ConnectionIdle)
|| (status != ConnectionError));

Otherwise, you will need to either do what you have written above or provide one function in your DLL to connect and another to get a value.

I hope this helps. Let me know if something is not clear or doesn't work.

Thanks,
TonyH
Measurement Studio
NI
Message 2 of 4
(3,242 Views)
Thank you very much for your answer. It's very useful!! I have solved my problems. Thanks!
0 Kudos
Message 3 of 4
(3,242 Views)
I just found another problem relates to this question, and would you please take a look at my new question? Thanks!

http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RPAGEID=135&HOID=5065000000080000005D550000&USEARCHCONTEXT_CATEGORY_0=_48_%24_7_&USEARCHCONTEXT_CATEGORY_S=0&UCATEGORY_0=_48_%24_7_&UCATEGORY_S=0
0 Kudos
Message 4 of 4
(3,242 Views)