I'm updating a 16 bit Delphi1 program, that uses the serial ports, to run on Delphi5/Win2000 but I'm having trouble with WriteFile.
If I use huge strings ($H+) string literals are output correctly but a string variable comes out as junk (I guess that Windows is getting the wrong buffer address). If I use short strings ($H-) both literals and variables work (sort of) but the initial length byte is included in the output and (hence) the last character is not output.
This is basic stuff so what am I doing wrong? Also in the code insight parameters for WriteFile what is const Buffer; - it's not a normal Pascal declaration of the form:- name : type; and I what's a type named Buffer!
WriteFile(hFile: Cardinal; const Buffer; nNumberOfBytesToWrite: Cardinal; var lpNumberOfBytesWritten: Cardinal; lpOverlapped: POverlapped)
>>>>>>>>>>>>>>>>>>>>> Sample program <<<<<<<<<<<<<<<<<<<<<
For simplicity it assumes that the mode of the serial port is already set up.
program console;
{$APPTYPE CONSOLE}
uses
WinProcs,
SysUtils;
var
handle: cardinal;
written: cardinal;
output: string;
begin
handle := CreateFile('COM1', GENERIC_WRITE, 0, nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if handle = INVALID_HANDLE_VALUE then
RaiseLastWin32Error;
if not WriteFile(handle,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
52, written, nil) then
RaiseLastWin32Error;
output := 'the quick brown fox jumped over the lazy dogs';
if not WriteFile(handle, output, Length(output),
written, nil) then
RaiseLastWin32Error;
CloseHandle(handle);
end.
>>>>>>>>>>>>>>>>>>>>> Output with $H+ <<<<<<<<<<<<<<<<<<<<<
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ¹
>>>>>>>>>>>>>>>>>>>>> Output with $H- <<<<<<<<<<<<<<<<<<<<<
4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-the quick brown fox jumped over the lazy dog
Note:
* ord('4') is 52 - the length of the literal,
* ord('-') is 45 - the length of the variable string.
If I use huge strings ($H+) string literals are output correctly but a string variable comes out as junk (I guess that Windows is getting the wrong buffer address). If I use short strings ($H-) both literals and variables work (sort of) but the initial length byte is included in the output and (hence) the last character is not output.
This is basic stuff so what am I doing wrong? Also in the code insight parameters for WriteFile what is const Buffer; - it's not a normal Pascal declaration of the form:- name : type; and I what's a type named Buffer!
WriteFile(hFile: Cardinal; const Buffer; nNumberOfBytesToWrite: Cardinal; var lpNumberOfBytesWritten: Cardinal; lpOverlapped: POverlapped)
>>>>>>>>>>>>>>>>>>>>> Sample program <<<<<<<<<<<<<<<<<<<<<
For simplicity it assumes that the mode of the serial port is already set up.
program console;
{$APPTYPE CONSOLE}
uses
WinProcs,
SysUtils;
var
handle: cardinal;
written: cardinal;
output: string;
begin
handle := CreateFile('COM1', GENERIC_WRITE, 0, nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if handle = INVALID_HANDLE_VALUE then
RaiseLastWin32Error;
if not WriteFile(handle,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
52, written, nil) then
RaiseLastWin32Error;
output := 'the quick brown fox jumped over the lazy dogs';
if not WriteFile(handle, output, Length(output),
written, nil) then
RaiseLastWin32Error;
CloseHandle(handle);
end.
>>>>>>>>>>>>>>>>>>>>> Output with $H+ <<<<<<<<<<<<<<<<<<<<<
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ¹
>>>>>>>>>>>>>>>>>>>>> Output with $H- <<<<<<<<<<<<<<<<<<<<<
4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY-the quick brown fox jumped over the lazy dog
Note:
* ord('4') is 52 - the length of the literal,
* ord('-') is 45 - the length of the variable string.