Board index » delphi » Problem with ReadFile in accesing serial ports

Problem with ReadFile in accesing serial ports

Hello everyone!

        I have a problem with access to serial ports in Delphi 2.0. I
use the following way of calling ReadFile:

        ReadFile(Port,buf,200,@cnt,NIL);

        where Port is of type THandle, buf is Array[0..255] of Char and
cnt is of type DWord. The compiler gives me an error 'Types of actual
and formal var parameters must be identical.'.

        Which is the right way of calling ReadFile?

Thanks in advance!

 

Re:Problem with ReadFile in accesing serial ports


Jos=?iso-8859-1?Q?=E9?= Antonio Zumalave Rivas <elz...@usc.es> writes:

Quote
>Hello everyone!
>    I have a problem with access to serial ports in Delphi 2.0. I
>use the following way of calling ReadFile:
>    ReadFile(Port,buf,200,@cnt,NIL);
>    where Port is of type THandle, buf is Array[0..255] of Char and
>cnt is of type DWord. The compiler gives me an error 'Types of actual
>and formal var parameters must be identical.'.
>    Which is the right way of calling ReadFile?

Here's how Delphi "prototypes" this call in the Windows.Pas file:

function ReadFile(hFile: THandle; var Buffer; nNumberOfBytesToRead: DWORD;
  var lpNumberOfBytesRead: DWORD; lpOverlapped: POverlapped): BOOL;
stdcall;

The problem is in your fourth parameter, called cnt in your case. You are
passing the address of this variable. This won't work since it's a var
parameter in Windows.Pas. Try this instead:

ReadFile(Port, buf, 200, cnt, NIL);

Just lose the "@" in fron of your cnt parameter.

Mike Lastort
Previously bitten by the same Delphi "undocumented feature"

Other Threads