Re: Delphi.NET



Daniel Hobson wrote:
kennedyri@xxxxxxxxx wrote:
On Jun 27, 3:41 am, "Daniel Hobson" <djhob1...@xxxxxxxxxxxx> wrote:

The source buffer? Are you sure you had the parameters in the right
order? Show your code.

Here is the code that we tried.

{$UNSAFECODE ON}
procedure TMainForm.Button1Click(Sender: TObject); unsafe;
type
TTestRec = record
t1: array [1 .. 38] of char;
t2: array [1 .. 22] of char;
end;
var
inputMsg : TBytes;
MyTest : TTestRec;
MySrcPtr : IntPtr;
begin
try
try
{ hostclient is an TidTCPClient component }
hostclient.Connect;
MySrcPtr := IntPtr.Create(@MyTest);
SetLength (inputMsg, 60);
hostclient.IOHandler.ReadBytes (inputMsg, 60, false);
Marshal.Copy(inputMsg, 0, MySrcPtr, 60);

There you're writing 60 bytes into an eight-byte buffer. I suspect at least some of those bytes end up being written onto the stack position of inputMsg.

hostclient.IOHandler.Writeln ('Reply');
except

end;

Get rid of that empty exception handler. It's not helping anything.

finally
{
Cleanup before exiting.
}
SetLength (inputMsg, 0);
end;

That's not necessary. Never has been. In Win32, inputMsg is a dynamic array that gets cleaned up automatically when it goes out of scope. In ..Net, inputMsg is a System.Array object that gets cleaned up by the garbage collector, just like all other objects.

end;

You're reading 60 bytes of input. Your record has two arrays for storing 120 bytes. How do you wish to distribute that?

Here's one way:

x := 0;
for i := 1 to 38 do begin
MyTest.t1[i] := Chr(inputMsg[x]);
Inc(x);
end;
for i := 1 to 22 do begin
MyTest.t2[i] := Chr(inputMsg[x]);
Inc(x);
end;

--
Rob
.


Quantcast