Reputation: 117
I have a byte array with some data...
CommandBytes : array of byte;
And I have a function of a VCL that sends command to a bluetooth printer. The VCL function prototype is this
SendData(PAnsiChar, Cardinal);
"This method transmits bytes from the memory buffer into opened device"
How could I pass correctly the byte array to function to send command to printer?
I'm new to Delphi, I tried:
SendData(PAnsiChar(@CommandBytes[0]), SizeOf(CommandBytes));
but it doesn't work...
Thanks in advance.
Upvotes: 1
Views: 2099
Reputation: 34889
The SizeOf(CommandBytes)
will return the size of a pointer.
Use Length(CommandBytes)
to get the element count in the array, which in this case is the allocated buffer size, since SizeOf(byte) = 1.
Upvotes: 1
Reputation: 612794
SizeOf()
on a dynamic array yields the size of a pointer. A dynamic array is a reference type that is represented as a pointer to the first element of the array. And hence SizeOf()
returns the size of a pointer. And that's no use to you here.
You need to use Length()
instead:
SendData(PAnsiChar(@CommandBytes[0]), Length(CommandBytes));
The Length()
function returns the number of elements in an array.
What's more, I would probably simplify the way you cast to PAnsiChar
. You can do it more concisely like this:
SendData(PAnsiChar(CommandBytes), Length(CommandBytes));
That's meaningful because CommandBytes
, being a reference to a dynamic array, points to the first element of the array.
Upvotes: 7