Reputation: 1
I am trying to write an application to use Controller Area Network devices over a ethernet network, so an engineer can connect to a technicians laptop that is physically connected to the CAN bus. I want to use the generic RP1210 device standard as most Data Link Adapters support this standard. The basic application has a client and server side. The RP1210 standards requires DLA manufactures to provide a DLL that interfaces with the hardware and provides common API functions accessible by using LoadLibrary and GetProcAddress. Making a single threaded application works perfectly but in operation the application will need to be multithreaded as there is a need to have a blocking read call while providing send signals to stimulate the response.
Where the wheels have come off is that I am unable to get any usable data back from the second thread, the first thread works OK but the second thread returns bad values and crashes.
I have tried using a single object with LoadLibrary and GetProcAddress in the main thread and giving pointers to the other thread, this works on the first thread but not the second. I tried calling LoadLibrary once and passing the handle to the treads and call GetProcAddress from each thread. I have also tried calling LoadLibrary and GetProcAddress from each thread, none of which works, same result as above.
Some questions I have: Should dll function pointers created in one thread be useable in another thread?
Can GetProcAddress return an address that is not usable by another thread, the pointers in both threads have non NULL values.
Application is written C++ using QT5.9-3 and the MinGw compiler on Windows 8.1. The threads are created using QThread which are instantiated from a QTcpServer class.
The function pointers are instantiated like this:
typedef short (WINAPI *fxRP1210_ClientConnect)(HWND,short,char*,long,long,short);
fxRP1210_ClientConnect pRP1210_ClientConnect = NULL;
pRP1210_ClientConnect = (fxRP1210_ClientConnect)(GetProcAddress(m_dll_handle,"RP1210_ClientConnect"));
short DLLEXPORT WINAPI BUS_ClientConnect(HWND, short, char*, long send_buf, long recv_buf, short sftw);
In the cpp
short Rp1210DllFunctions::BUS_ClientConnect(HWND win_handle, short client_id, char *protocol, long send_buf, long recv_buf, short sftw)
{
return pRP1210_ClientConnect( win_handle, client_id, protocol, send_buf, recv_buf,sftw );
}
Upvotes: 0
Views: 645
Reputation: 15162
No, pointers returned by GetProcAddress will be valid for all threads.
It is, however, very possible that objects (handles or otherwise) are only usable from one thread.
Upvotes: 2