Question

We are using the Below code for printing the tickets data in LPT1 port. These code in working fine in Win-7 Delphi-7 Exe but same code is not working on Win-7 Delphi XE. I've try the solution available in net. but it is not helping me to resolved the issues. Please can you suggested any solution for that.

function TdmDisneyCastTrac.SendToParallelPort(pContent : TStringList): boolean;
var
  slPrintLines : TStringList;
  hFile : THandle;
  Overlapped : TOverlapped;
  I : integer;
  bContentPrinted : boolean;
  dw : DWORD;
begin
   slPrintLines := TStringList.Create;
   result := True;
   try
      slPrintLines.AddStrings(pContent);
      FillChar(Overlapped, SizeOf(Overlapped),0);
      I := 0;
      repeat
         hFile := INVALID_HANDLE_VALUE;
         bContentPrinted := True;
         hFile := CreateFile(PChar('LPT1:'), GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
         if hFile = INVALID_HANDLE_VALUE then
            bContentPrinted := False
         else
         begin
            Overlapped.hEvent := CreateEvent(nil, False, False, nil);
            if Overlapped.hEvent = 0 then
               bContentPrinted := False
            else
            begin
               if not WriteFile(hFile, slPrintLines.Text[1], length(slPrintLines.Text), dw, @Overlapped) then
               case WaitForSingleObject(Overlapped.hEvent, 4000) of  // wait 3 seconds
                  WAIT_OBJECT_0 : ;
                  WAIT_TIMEOUT,
                  WAIT_ABANDONED : begin
                                     bContentPrinted := False;
                                     if hFile <> INVALID_HANDLE_VALUE then
                                       CloseHandle(hFile);
                                       if Overlapped.hEvent <> 0 then
                                            CloseHandle(Overlapped.hEvent);
                                   end;
             end;
           end;
          end;
          inc(I);
        until bContentPrinted or (I = 3);
        result := bContentPrinted;
  finally
     slPrintLines.Free;
      if hFile <> INVALID_HANDLE_VALUE then
         CloseHandle(hFile);
      if Overlapped.hEvent <> 0 then
         CloseHandle(Overlapped.hEvent);
   end;
end;

Noted:- This is the Win API function.

Was it helpful?

Solution

The obvious change is that Delphi 7 uses ANSI text, and XE uses UTF-16 text. I guess you'll need to convert the text to ANSI explicitly in order to match previous behaviour.

var
  ansistr: AnsiString;
....
ansistr := AnsiString(slPrintLines.Text);

Then you can send that to the file handle in the same way as you did previously.

There are many other issues in this code but I don't want to perform a complete re-write. This is the main issue that you face.

Before you do anything else you must read Marco Cantù's white paper on Delphi and Unicode. There's no point continuing until you have a good understanding of this major breaking change introduced in Delphi 2009.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top