Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

CNiOleException raised when using NiTimer and Datasocket

I am trying to write a small application which writes to the datasocket server at regular intervalls.  In OnInitDialog() I’ve added the following code:

 

m_dataSocketConnectionString="dstp://localhost/level_in_percent";

int status = m_myDataSocket.Status;
if (status == CNiDataSocket::Unconnected)
{
         m_myDataSocket.Connect(m_dataSocketConnectionString, CNiDataSocket::WriteAutoUpdate);
}

m_pTimer= new CNiTimer(0.5, false);
m_pTimer->InstallEventHandler(*this, TimerEvent);
m_pTimer->Start();

 

Now the problem is that the line:

 

int status= m_myDataSocket.Status;

 

in the member function TimerEvent raises an NiOleException. When using GetErrorMessage() I get ”CoInitialize has not been called”. What am I doing wrong?

 

Adam

0 Kudos
Message 1 of 4
(6,698 Views)

You are not calling CoInitialize(). The TimerEvent occurs in another thread and since datasocket is a COM component, COM needs to be initialized for that thread. Just call CoInitialize() as the very first function in the TimerEvent member function. That should take care of it.

Bilal Durrani
NI
0 Kudos
Message 2 of 4
(6,693 Views)

Thank you for a swift reply! The solution worked fine.

Adam

0 Kudos
Message 3 of 4
(6,689 Views)

Hi,CNiOleException raised when I want to call a function which is in a dll.

The code in the function is;

 

try{
       
        CNiDataSocket socket;
        socket.Init(model,threadAccess);
        socket.URL = "psp:\\\\localhost\\Untitled Library 5\\voltaj";
        socket.SetAccessMode(socket.ReadAutoUpdate);
        socket.Connect();
        CNiDataSocketData data =  socket.GetData();
        double val = data.GetValue();
        return val;
    }
    catch(const char aMessage[])
    {
        cout << endl
        << aMessage
        << endl;
    }

 

and in the first line of the code,  CNiOleException raised.

 

Do you have any idea about the problem?

 

Thanks

 

Ozan

0 Kudos
Message 4 of 4
(6,244 Views)