CRO_Tomislav 0 Posted February 16, 2019 Dear all. Please take my apology if I post in a wrong section of this forum... I new to Delphi and I have a problem with a using a C++ .dll from Delphi VCL. In a manual for .dll I have for a next function description: MXIO_GetDllVersion Get the DLL version. C/C++ int MXIO_GetDllVersion (); Visual Basic Declare Function MXIO_GetDllVersion Lib "MXIO.dll" () As Long Arguments: None Return Value: Succeed Return the DLL version. If the value is 0x2000, then the version is 2.0.0.0 I call from Delphi this function as: function MXIO_GetDllVersion : Integer; cdecl; external 'MXIO.dll' name 'MXIO_GetDllVersion'; In a ShowMessage(IntToStr(MXIO_GetDllVersion)); Problem: I get incorrect data. Instead of 21200; I get: 11264 Thx in advance Tom Share this post Link to post
Boris Novgorodov 10 Posted February 16, 2019 (edited) You show decimal representation while version components correspond to nibbles of hex representation 11264dec = $2C00, so your version number is 2.12.0.0 (2.$C.0.0) Edited February 16, 2019 by Boris Novgorodov 2 Share this post Link to post
PeterBelow 238 Posted February 16, 2019 The docs you quoted state that the version is encoded in the *hexadecimal* value returned. You look at the decimal value. Decimal 11264 is hexadecimal 0x2c00 ($2C00 in Delphi notation). So the version is 2.12.0.0 Share this post Link to post
CRO_Tomislav 0 Posted February 16, 2019 OMG... It is a long night behind me... Sorry Tom Share this post Link to post
CRO_Tomislav 0 Posted February 16, 2019 I have problem with a next function. How to tranlsate it to a delphi code? C/C++ int MXEIO_E1K_Connect( char * szIP, WORD wPort, DWORD dwTimeOut, int * hConnection, char * szPassword); Visual Basic Declare Function MXEIO_E1K_Connect Lib "MXIO.dll" (ByVal szIP As String, ByVal iPort As Integer, ByVal nTimeOut As Long, handle As Long, ByVal szPassword As String) As Long Arguments: szIP IP address of the Ethernet I/O device to be connected.A-OPC Server IP address or ioLogik 1200 IP address wPort TCP port number of Ethernet I/O device. Please use 502 for ioLogik 1200 dwTimeOut Timeout value for establishing a network connection with the ioLogik Ethernet Adapter. The unit is in milliseconds. hConnection Handle for the I/O device connection. szPassword Max length 8 bytes Return Values : Succeed MXIO_OK Fail Refer to Return Codes. I try with: function MXEIO_E1K_Connect(szIP: String; iPort: Integer; nTimeOut: Integer; handle: Integer; szPassword: String): Integer; external 'MXIO.dll' name 'MXEIO_E1K_Connect'; Whene I call this function it crash my app. (Integer overflow). Is it problem in my sintaks or error in .dll? Share this post Link to post
uligerhardt 18 Posted February 16, 2019 You didn't specify a calling convention. This is probably cdecl like in MXIO_GetDllVersion. Another poular choice is stdcall. Share this post Link to post
stijnsanders 35 Posted February 16, 2019 (edited) Also use PAnsiChar instead of string. It's a stricter translation of C’s *char and Delphi has great auto conversion to and from string with it Edited February 16, 2019 by stijnsanders Share this post Link to post
CRO_Tomislav 0 Posted February 16, 2019 Hi. It works only with stdcall.. Cdecl chrashes... With best regards Tomislav Share this post Link to post
Remy Lebeau 1396 Posted February 16, 2019 (edited) 7 hours ago, uligerhardt said: You didn't specify a calling convention. This is probably cdecl like in MXIO_GetDllVersion. Another poular choice is stdcall. The DLL's documentation includes VB declarations. VB doesn't support cdecl at all, only stdcall. Also, I just found this, which clearly shows the DLL functions using CALLBACK for the calling convention. That is a preprocessor macro that maps to __stdcall. Edited February 16, 2019 by Remy Lebeau 2 Share this post Link to post
Remy Lebeau 1396 Posted February 16, 2019 8 hours ago, CRO_Tomislav said: I have problem with a next function. How to tranlsate it to a delphi code? ... I try with: function MXEIO_E1K_Connect(szIP: String; iPort: Integer; nTimeOut: Integer; handle: Integer; szPassword: String): Integer; external 'MXIO.dll' name 'MXEIO_E1K_Connect'; Whene I call this function it crash my app. (Integer overflow). Is it problem in my sintaks or error in .dll? Your translation is WAY off. DONT use String for char* parameters, use PAnsiChar instead. DONT use Integer for (D)WORD parameters, use (D)WORD instead. And your handle parameter needs to be declared as 'var' or 'out' since it is an output parameter. Try this instead: function MXEIO_E1K_Connect(szIP: PAnsiChar; wPort: WORD; dwTimeOut: DWORD; var hConnection: Integer; szPassword: PAnsiChar): Integer; stdcall; external 'MXIO.dll' name 'MXEIO_E1K_Connect'; Or this: function MXEIO_E1K_Connect(szIP: PAnsiChar; wPort: WORD; dwTimeOut: DWORD; hConnection: PInteger; szPassword: PAnsiChar): Integer; stdcall; external 'MXIO.dll' name 'MXEIO_E1K_Connect'; Share this post Link to post
CRO_Tomislav 0 Posted February 18, 2019 Hi. It works! BIG THX!!!! With best regards Tomislav Share this post Link to post
Rudy Velthuis 91 Posted February 19, 2019 (edited) Befre you make more mistakes like the ones above, please ready my article about the Pitfalls of converting. It explains everything discussed here and a lot more. Edited February 19, 2019 by Rudy Velthuis 1 Share this post Link to post
David Heffernan 2345 Posted February 20, 2019 You can tell it must be stdcall since that is the only convention supported by classic VB. 1 Share this post Link to post