Closed ralfixx closed 3 years ago
Please disregard this issue. The proper solution is noted in Change the "modbus.vcproj" Visual Studio project file in order to correct an ERRNO problem in the Win32 platform #485 The modbus DLL and all code using it needs to get compiled with /MD compiler flag, not /MT. It would be very nice if the provided Visual Studio Project files in src/win32 could get adjusted ASAP.
libmodbus version
libmodbus-3.1.6
OS and/or distribution
Windows 10
Environment
64bit
References
Socket brutally closed by peer not correctly detected #133 errno not reliable on Windows #349 Error codes on Windows #571
Description
Modbus over TCP
On Windows, errno is not propagated properly between DLLs, so the mechanism used in libmodbus to transport error information does not work properly there. My DLL contains code like
When the TCP connection is closed by the remote side, the recv() which is eventually called returns 0 as documented which then is turned into "errno = ECONNRESET" by _modbus_receive_message(). But when the call returns to my DLL, the errno there is set to some other value. This is due to Windows not sharing these globals between DLLs/modules. Even calling _set_errno() in libmodbus and _get_errno() in my DLL return different values. I confirmed in the debugger that "errno" points to different memory locations in libmodbus and my DLL.
IMHO a possibly solution would be to add an "errnum" member to the modbus context and set that, so the calling code could inspect the errno from there. I would wrap that in
where libmodbus code would need to change
and client code addressing windows
This way also the difference between windows socket functions requiring WSAGetLastError() calls to obtain error information could be addressed, plus client code on Linux would not need to change at all (since errno works properly there).
I would be willing to provide patches if required.