ELCouz
ELCouz

Reputation: 572

Buffer and pointers in Delphi from C DLL

I am still learning from the DLL process and since no Delphi FTDI Libmpsse library exist, I must work my own way through with the DLL from scratch.

However, I'm far from perfect in Delphi! I've been using it for few years without touching pointers.

This is the particular line I'm now stuck with (This is what I've tried to translate):

function I2C_DeviceWrite(
  handle              :dword;
  deviceAddress       :uint32;
  sizeToTransfer      :uint32;
  out buffer          :byte;
  out sizeTransferred :uint32;
  options             :uint32
):FT_Result; cdecl; external 'libmpsse.dll';

From the API User guide it is describe the C way like this:

FT_STATUS I2C_DeviceWrite(
  FT_HANDLE handle, 
  uint32    deviceAddress,
  uint32    sizeToTransfer, 
  uint8    *buffer, 
  uint32   *sizeTransferred,
  uint32    options )

Asterix means pointer in C from what I understand.

I don't know how to declare them properly yet.

Any hints?

NB: I'm still reading (XE2 Foundations) and searching about pointers and buffers the most I can!

Thank you!

BTW, API Guide is here for consulation:PDF from FTDI

EDIT:

var
Buffer: array of Byte;
written,towrite:cardinal;
begin
SetLength(Buffer,I2C_DEVICE_BUFFER_SIZE);
buffer[0] := $07;
towrite := 1;
i2c_openchannel(1,FT_HANDLE); //open channel 1
i2c_initchannel(FT_HANDLE,I2C_CLOCK_STANDARD_MODE and LATENCY_TIMER and 0);
i2c_devicewrite(FT_HANDLE,I2C_DEVICE_ADDRESS,towrite,buffer,written,I2C_TRANSFER_OPTIONS_START_BIT);
i2c_closechannel(FT_HANDLE); //close device

So far so good everything else work except the i2c_devicewrite line!
It gives me E2033:unable to compile actual and formal var parameters must be identical

Upvotes: 0

Views: 1861

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You've translated the byte array parameter incorrectly. You used

out buffer: byte

which is a single byte, passed out by reference. It should be

buffer: PByte

which here means a pointer to array of byte.

Then pass the address of the first element of the array. Like this:

@Buffer[0]

or like this

PByte(Buffer)

Note that I've not checked anything other than the byte array parameter.

FWIW, a constant length array may be simpler here than a dynamic array.

I'd also comment that you appear to be ignoring return values. Never do that. Always check return values for errors.

Upvotes: 2

Related Questions