1
votes

In my application, all printers are listed using printer.printers command. This lists only printer names. Upon selection, it is saved in the Database.

Later on, this printer name is assigned using the AssignFilefunction to a text file. And printing is done using Rewrite function.

If I save the selected printer as \\PCname\printer name in the database and then use it with Rewrite function then it works.

But if we save only printer name in the database then printing is not happening. Is it necessary to save \\PCname\printer name path? or Is there any other solution.

2
@DelphiCoder: Yes plz..that will be helpful for us. - poonam
@DelphiCoder: I accepted your answer. Thank you. - poonam

2 Answers

2
votes

Obviously, you need PCname. You can save it in the database as you said but it will be a problem if the database is used from several PC, the only save printer name in the database and add PCname on the PC using the printer. You can get PCname using GetComputerName

2
votes

Anotherway than printing using the Pascal file access functions is to use the Windows API for the spooler.

function PrintWithSpooler(const Name: string; const Data: AnsiString): integer;
var
  hPrinter: THandle;
  DocInfo: TDocInfo1;
  bSuccess: boolean;
  dwBytesWritten: DWORD;
begin
  result := S_OK;
  bSuccess := false;

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

  if OpenPrinter(PChar(Trim(Name)), hPrinter, nil) then
  begin
    try
      if StartDocPrinter(hPrinter, 1, @DocInfo) > 0 then
      begin
        try
          if StartPagePrinter(hPrinter) then
          begin
            try
              bSuccess := WritePrinter(hPrinter, Pointer(Data), Length(Data), dwBytesWritten);
            finally
              EndPagePrinter(hPrinter);
            end;
          end;
        finally
          EndDocPrinter(hPrinter);
        end;
      end;
    finally
      ClosePrinter(hPrinter);
    end;
  end;

  if not bSuccess then
  begin
    result := GetLastError;

    // in case there was no error from GetLastError
    if result = S_OK then
      result := S_FALSE;
  end;
end;