Jump to content
audi30tdi

System error - printer port

Recommended Posts

Hello!

Why does the following code give me an system error. Code 5. No access. This happens if I choose a printername from memo1 which have a different port then an ethernet.

 

procedure tform1.Display(const prefix: string; S: PChar);
begin
memo1.lines.add(prefix + string(S));
end;

function GetCurrentPrinterHandle: THandle;
const
  Defaults: TPrinterDefaults = (pDatatype: nil; pDevMode: nil; DesiredAccess:
    PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER);
var
  Device, Driver, Port: array[0..255] of char;
  hDeviceMode: THandle;
begin
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  if not OpenPrinter(@Device, Result, @Defaults) then
    RaiseLastWin32Error;
end;

procedure TForm1.Button14Click(Sender: TObject);
var
  pInfo: PPrinterInfo2;
  bytesNeeded: DWORD;
  hPrinter: THandle;
  u,i: Integer;
  sant : boolean;
begin
u:=strtoint(edit4.Text);
i:=-1;
sant:=false;
repeat
i:=i+1;
{  for i := 0 to printer.Printers.Count - 1 do begin}
    Printer.PrinterIndex := i;
    hPrinter := GetCurrentPrinterHandle;
    try
      GetPrinter(hPrinter, 2, nil, 0, @bytesNeeded);
      pInfo := AllocMem(bytesNeeded);
      try
        GetPrinter(hPrinter, 2, pInfo, bytesNeeded, @bytesNeeded);
if pinfo^.pPrinterName=memo2.lines then begin
        Display('ServerName: ', pInfo^.pServerName);
        Display('PrinterName: ', pInfo^.pPrinterName);
        Display('ShareName: ', pInfo^.pShareName);
        Display('PortName: ', pInfo^.pPortName);
sant:=true;
        end;
      finally
        FreeMem(pInfo);
      end;
    finally
      ClosePrinter(hPrinter);
    end;
{  end;}
until (i=printer.printers.Count-1) or (sant=true);
end;
 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×