Delphi 2010 (or newer) . tcp server with indy

I have the following code running on D2007 that I need to port to delphi 2010.
(ie what I want to do is to open a tcp port and
take all data that arrives and write it to a printer.


procedure TfrmMain.IdTCPServer1Connect(AContext: TIdContext);

begin
  memLog.Lines.Add('Receiving   PrintJob');
  Printer.PrinterIndex := PrinterIndex;
  Printer.GetPrinter(sDevice, sDriver, sPort, hDeviceMode);

  if not WinSpool.OpenPrinter(@sDevice, hPrinter, nil) then
  begin
      aContext.Connection.Disconnect;
     exit;
  end;

  DocInfo.pDocName := 'Document';
  DocInfo.pDatatype := 'RAW';
  DocInfo.pOutputFile := nil;

  if StartDocPrinter(hPrinter, 1, @DocInfo) = 0 then
  begin
    aContext.Connection.Disconnect;
    Exit;
  end;

  if not StartPagePrinter(hPrinter) then
  begin
    aContext.Connection.Disconnect;
    Exit;
  end;
end;

procedure TfrmMain.IdTCPServer1Execute(AContext: TIdContext);
var
  data : String;
begin
 //  memLog.Lines.Add('Recieving Printjob');
  data:= aContext.Connection.IOHandler.InputBufferAsString;
  if not WritePrinter(hPrinter, pansichar(data), length(data), BytesWritten) then
  begin
    EndPagePrinter(hPrinter);
    EndDocPrinter(hPrinter);
    WinSpool.ClosePrinter(hPrinter);
    aContext.Connection.Disconnect;
    exit;
  end;

end;

procedure TfrmMain.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  try
    EndDocPrinter(hPrinter);
  except
  end;
  try
    WinSpool.ClosePrinter(hPrinter);
  except
  end;
   memLog.Lines.Add('Document sent to Printer');
end;
pucko73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MerijnBSr. Software EngineerCommented:
What problems do you encounter?
0
pucko73Author Commented:
It becomes empyt on the printer.

if I add
   sl :=TstringList.Create;
   sl.Text:= data;
   sl.SaveToFile('c:\testarea\abc.ps');

after the assignment of data,
and then do a printout directly to file and compare the files
I can see that  seems like the first half of the file is missing in abc.ps compared with  when i select print to file directly from print dialog
0
pucko73Author Commented:
Page becomes empty. is the real problem.
0
pucko73Author Commented:
It seems like changing  data : String; to  data : RawByteString
solves my problem
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pucko73Author Commented:
Solved by my self
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.