Jump to content
CRO_Tomislav

HELP: Using C++ .dll in Delphi

Recommended Posts

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

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 by Boris Novgorodov
  • Like 2

Share this post


Link to post

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

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

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

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 by stijnsanders

Share this post


Link to post
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 by Remy Lebeau
  • Like 2

Share this post


Link to post
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×