Why are lines used for direct printing?

I am trying to print directly to a printer using esc / p commands (EPSON TM-T70) without using a printer driver. The code is found here .

However, if I try to print any lines, they are truncated. For instance:

MyPrinter := TRawPrint.Create(nil); try MyPrinter.DeviceName := 'EPSON TM-T70 Receipt'; MyPrinter.JobName := 'MyJob'; if MyPrinter.OpenDevice then begin MyPrinter.WriteString('This is page 1'); MyPrinter.NewPage; MyPrinter.WriteString('This is page 2'); MyPrinter.CloseDevice; end; finally MyPrinter.Free; end; 

Will print only "This isThis"! I would normally not use MyPrinter.NewPage to send a line break command, but no matter why it truncates the line?

Also note the RawPrint WriteString function:

 Result := False; if IsOpenDevice then begin Result := True; if not WritePrinter(hPrinter, PChar(Text), Length(Text), WrittenChars) then begin RaiseError(GetLastErrMsg); Result := False; end; end; 

If I put a breakpoint and go through the code, then WrittenChars will be set to 14, which is correct. Why is this so?

+4
source share
2 answers

You are using a Unphode-enabled version of Delphi. The length of characters is 2 bytes. When you call your function using Length(s) , you send the number of characters, but the function is probably expecting a buffer size. Replace it with SizeOf (s) Length(s)*SizeOf(Char) .

Since the size of a single Unicode char is exactly 2 bytes when you send Length , when the size of the buffer is required, you basically say that the API uses only half the buffer. Therefore, all lines are rounded in half in half.

+4
source

Perhaps you can use the ByteLength function, which gives the length of the string in bytes.

+4
source

All Articles