Board index » delphi » Error 217 on WinNT when calling Printer.Printers

Error 217 on WinNT when calling Printer.Printers

I have an app that has been run on many Win95 machines with no problems, but
aborts with runtime error 217 when run on Windows NT.  Unfortunately I'm
trying to debug this remotely via modem, so I can't just trace into it to
see what is going on.

I've reduced the problem to a very small test case.  It has only one unit,
and this is it:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Printers;

{$R *.DFM}

var
  CurrPrinterName : string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := CurrPrinterName;
end;

initialization
  {The following line causes runtime error 217 when run on Windows NT}
  CurrPrinterName := Printer.Printers[Printer.PrinterIndex];
end.

I'm using Delphi v3.02.  Does anyone know why this problem is happening?

Thanks in advance,
--Rob Roberts

 

Re:Error 217 on WinNT when calling Printer.Printers


Quote
Rob Roberts wrote in message <73fo1b$a...@forums.borland.com>...
>var
>  CurrPrinterName : string;

>procedure TForm1.FormCreate(Sender: TObject);
>begin
>  Label1.Caption := CurrPrinterName;
>end;

>initialization
>  {The following line causes runtime error 217 when run on Windows NT}
>  CurrPrinterName := Printer.Printers[Printer.PrinterIndex];
>end.

At the time the initialization code of this unit is executed, the Printer
object in the Printers unit has not been created yet - i.e. Printers'
initialization has not been executed as yet, your unit came first. Move this
line of code to your FormCreate method.

As a general rule (and *strongly* recommended) one should never write code
in initialization or finalization sections that depend on code in *other*
units.

--
Wayne Niddery - WinWright Consulting
Delphi, C++Builder, JBuilder, InterDev --
http://home.ican.net/~wniddery/RADBooks.html
...remove chaff when replying...
"You know you've landed gear-up when it takes full power to taxi"

Re:Error 217 on WinNT when calling Printer.Printers


Wayne,

Quote
>At the time the initialization code of this unit is executed, the Printer
>object in the Printers unit has not been created yet - i.e. Printers'
>initialization has not been executed as yet, your unit came first. Move
this
>line of code to your FormCreate method.

>As a general rule (and *strongly* recommended) one should never write code
>in initialization or finalization sections that depend on code in *other*
>units.

Thanks for your help.  However, the Printers unit has no code in its
initialization section.  The Printer object is not created in unit
initialization.  Instead it gets created the first time it is accessed,
using this function:

function Printer: TPrinter;
begin
  if FPrinter = nil then FPrinter := TPrinter.Create;
  Result := FPrinter;
end;

So this isn't a case of accessing an object before it has been created.  Any
other ideas?...

Thanks,
--Rob Roberts

Re:Error 217 on WinNT when calling Printer.Printers


Quote
Rob Roberts wrote in message <73h6n4$b...@forums.borland.com>...

>Thanks for your help.  However, the Printers unit has no code in its
>initialization section.  The Printer object is not created in unit
>initialization.  Instead it gets created the first time it is accessed,
>using this function:

Been awhile since I looked at it, looks like they changed it on me in Delphi
3 <g>. My advice still stands, but you are, of course, right that it is not
the problem in this case.

I've run your sample code on my machine (NT4 WS SP3) and it works fine.
However, I can reproduce a 217 error by substituting -1 for
Printer.PrinterIndex which tells me that there is a problem with the setup
of printers on that machine - there are no listed printers that the Printers
object is able to find. Add a line to check that Printer.Printers.Count > 0
before trying to get a printer name.

--
Wayne Niddery - WinWright Consulting
Delphi, C++Builder, JBuilder, InterDev --
http://home.ican.net/~wniddery/RADBooks.html
...remove chaff when replying...
"You know you've landed gear-up when it takes full power to taxi"

Re:Error 217 on WinNT when calling Printer.Printers


Wayne,

Quote
>I've run your sample code on my machine (NT4 WS SP3) and it works fine.
>However, I can reproduce a 217 error by substituting -1 for
>Printer.PrinterIndex which tells me that there is a problem with the setup
>of printers on that machine - there are no listed printers that the
Printers
>object is able to find. Add a line to check that Printer.Printers.Count > 0
>before trying to get a printer name.

That explains it!  Thanks again for your help...

--Rob Roberts

Other Threads