Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: XE6 How To Get Printer PaperSize and Orientation


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Jul 11, 2014 10:31 AM Last Post By: Quentin Correll
Andrew Hill

Posts: 7
Registered: 2/10/04
XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 10, 2014 2:33 PM
The code below fails to compile, please advise how to fix - thanks

procedure TMiscObject.OpenThePrinterDevice; // Printers, Winspool
var
Driver_Info2: PDriverInfo2;
Retrieved: dword;
hPrinter: THandle;
s: String;
begin
Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);
if DeviceMode = 0 then Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);
OpenPrinter(FDevice, hPrinter, nil);
GetMem(Driver_Info2, 255);
GetPrinterDriver(hPrinter, nil, 2, Driver_info_2, 255, Retrieved);
StrLCopy(FDriver, PChar(ExtractFileName(StrPas(Driver_Info2^.PDriverPath)) + #0), 63);
FreeMem(Driver_info_2, 255);
DevMode:= GlobalLock(DeviceMode);
//
if ((DevMode^.dmFields and DM_ORIENTATION) = DM_ORIENTATION)
and (DevMode^.dmOrientation = DMORIENT_LANDSCAPE)
then begin
s:= 'Landscape';
end else begin
s:= 'Portrait';
end;
//
if ((DevMode^.dmFields and DM_PAPERSIZE) = DM_PAPERSIZE)
and (DevMode^.dmPaperSize = DMPAPER_LETTER)
then begin
s:= 'Letter';
end else begin
s:= 'A4';
end;
//
GlobalUnlock(DeviceMode);
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 10, 2014 4:31 PM   in response to: Andrew Hill in response to: Andrew Hill
Andrew wrote:

The code below fails to compile, please advise how to fix - thanks

For future reference, please always indicate which line does not compile,
provide the compiler error message, etc. And please always provide relevant
variable declarations. You did not provide the declarations of your FDevice,
FDriver, FPort, DeviceMode, or DevMode variables. Dont make people hunt
for things.

Taking those issues into account, the only compiler error I get from your
code is the following:

GetPrinterDriver(hPrinter, nil, 2, Driver_info_2, 255, Retrieved); // E2029 
'(' expected but ',' found


Clicking on the error message puts the cursor on the ',' after "Driver_info_2".

The 4th parameter of GetPrinterDriver() is a Pointer to an allocated buffer,
but you are passing a record type instead, hense the error. You need to
pass your Driver_Info2 variable instead.

Fixing that error, a new error appears:

FreeMem(Driver_info_2, 255); // E2029 '(' expected but ',' found


Same mistake. You are passing a record type where a pointer is expected.
And you can remove the second parameter. GetMem() allocates tracking information
that FreeMem() can look at to know how much memory to free.

Fixing that error, no more compiler errors occur.

However, the rest of your code is missing error handling, and you are leaking
the device handle from OpenPrinter(). Try this code instead:

procedure TMiscObject.OpenThePrinterDevice; // Printers, Winspool
var
  Driver_Info2: PDriverInfo2;
  Retrieved: DWORD;
  hPrinter: THandle;
  s: String;
begin
  Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);
  if DeviceMode = 0 then Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);
  if not OpenPrinter(FDevice, hPrinter, nil) then
  begin
    // do something
    Exit;
  end;
  try
    Retrieved := 255;
    GetMem(Driver_Info2, Retrieved);
    try
      repeat
        if not GetPrinterDriver(hPrinter, nil, 2, Driver_Info2, Retrieved, 
Retrieved) then
        begin
          if GetLastError() <> ERROR_INSUFFICIENT_BUFFER then
          begin
            // do something
            Exit;
          end;
          FreeMem(Driver_Info2);
          Driver_Info2 := nil;
          GetMem(Driver_Info2, Retrieved);
        end;
      until False;
      StrLCopy(FDriver, PChar(ExtractFileName(Driver_Info2^.PDriverPath)), 
63);
    finally
      FreeMem(Driver_Info2);
    end;
    DevMode := GlobalLock(DeviceMode);
    if DevMode = nil then
    begin
      // do something
      Exit;
    end;
    try
      //
      if ((DevMode^.dmFields and DM_ORIENTATION) = DM_ORIENTATION)
        and (DevMode^.dmOrientation = DMORIENT_LANDSCAPE) then
      begin
        s := 'Landscape';
      end else begin
        s := 'Portrait';
      end;
      //
      if ((DevMode^.dmFields and DM_PAPERSIZE) = DM_PAPERSIZE)
        and (DevMode^.dmPaperSize = DMPAPER_LETTER) then
      begin
        s:= 'Letter';
      end else begin
        s:= 'A4';
      end;
      //
    finally
      GlobalUnlock(DeviceMode);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;


--
Remy Lebeau (TeamB)
Andrew Hill

Posts: 7
Registered: 2/10/04
Re: XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 10, 2014 5:20 PM   in response to: Andrew Hill in response to: Andrew Hill
Thank You Remy.

I still get AV when line

Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);

I am using XE6, Windows 8.1
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 10, 2014 7:49 PM   in response to: Andrew Hill in response to: Andrew Hill
Andrew wrote:

I still get AV when line

Printer().GetPrinter(FDevice, FDriver, FPort, DeviceMode);

Is Printer().PrinterIndex returning -1?

--
Remy Lebeau (TeamB)
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 11, 2014 9:44 AM   in response to: Andrew Hill in response to: Andrew Hill
Andrew Hill wrote:

The code below fails to compile, please advise how to fix - thanks

Throw it away <g>

Take a look at this: http://tinyurl.com/6nh2cn

The unit may be old but it compiles and works in XE at least.

--
Peter Below (TeamB)

Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: XE6 How To Get Printer PaperSize and Orientation  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 11, 2014 10:31 AM   in response to: Peter Below in response to: Peter Below
Peter,

| | The code below fails to compile, please advise how to fix - thanks
|
| Throw it away <g>

LOL!

| Take a look at this: http://tinyurl.com/6nh2cn

| The unit may be old but it compiles and works in XE at least.

That's one heckuva unit!!!

--

Q

07/11/2014 10:30:12

1.19.1.372 [Q'sBrokenToolBar] [Running on TQ]

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02