Printer hDC is 0

Post Reply
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Printer hDC is 0

Post by hua »

I sub-classed TPrinter as XPrinter and called it like this.
(cPrinter is the name of the printer to use. Selected by user via PrinterSetup() and saved to a text file.)

Code: Select all | Expand

 XPRINTER oPrn PREVIEW MODAL NAME cRptTitle to cPrinter
This code works with my old version of FWH11.08.
With FWH1912+Harbour, if I set printer to "Microsoft Print To Pdf", it will generate error because hDC is 0.

When I compare TPrinter:new() between the FWH1108 and FWH1912 versions, the difference is the usage of PrinterDcFromName() in FWH1912

Code: Select all | Expand

   // FWH1912
   if lUser
      ::hDC := GetPrintDC( GetActiveWindow(), lSelection, PrnGetPagNums() )
      if ::hDC != 0
         cModel = ::GetModel() + "," + ::GetDriver() + "," + ::GetPort()
      endif
   elseif cModel == nil
      ::hDC  := GetPrintDefault( GetActiveWindow() )
      if ::hDC != 0
         cModel = ::GetModel() + "," + ::GetDriver() + "," + ::GetPort()
      endif
   else
      ::hDC = PrinterDcFromName( , cModel, )
   endif

Code: Select all | Expand

   //FWH1108
   if lUser
      ::hDC := GetPrintDC( GetActiveWindow(), lSelection, PrnGetPagNums() )
      if ::hDC != 0
         cModel = ::GetModel() + "," + ::GetDriver() + "," + ::GetPort()
      endif
   elseif cModel == nil
      ::hDC  := GetPrintDefault( GetActiveWindow() )
      if ::hDC != 0
         cModel = ::GetModel() + "," + ::GetDriver() + "," + ::GetPort()
      endif
   else
      cPrinter := GetProfString( "windows", "device" , "" )
      WriteProfString( "windows", "device", cModel )
      SysRefresh()
      PrinterInit()
      ::hDC := GetPrintDefault( GetActiveWindow() )
      SysRefresh()
      WriteProfString( "windows", "device", cPrinter  )
      // PrinterInit()
      // DeleteDC( ::hDC )
      // ::hDC = PrinterDCfromName( cModel )
   endif
 
I tested replacing PrinterDcFromName( , cModel, ) with the old code and it works.

So what is the accurate solution? I don't want to have to manually linked-in a fixed version of TPrinter.

TIA
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Printer hDC is 0

Post by Enrico Maria Giordano »

This is how i am using it:

Code: Select all | Expand

::hDC = PRINTERDCFROMNAME( STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 1, "," ), cModel, STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 2, "," ) )
User avatar
karinha
Posts: 7935
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil
Been thanked: 3 times
Contact:

Re: Printer hDC is 0

Post by karinha »

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: Printer hDC is 0

Post by hua »

Thanks Enrico.
Does this mean you maintain your own amended version of printer.prg?
Enrico Maria Giordano wrote:This is how i am using it:

Code: Select all | Expand

::hDC = PRINTERDCFROMNAME( STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 1, "," ), cModel, STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 2, "," ) )
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: Printer hDC is 0

Post by hua »

FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
Posts: 1075
Joined: Fri Oct 28, 2005 2:27 am
Has thanked: 1 time
Been thanked: 1 time

Re: Printer hDC is 0

Post by hua »

Just to update.

Enrico's post prompts me to eventually inspect var cModel value in my sub-class of TPrinter.
To my surprise, it wasn't just a string of printer name but includes the driver and port too.

In old PrintBegin(), the code at the end was

Code: Select all | Expand

   
   ...
   cText   := GetProfString( "Devices", aPrn[ nScan ] )
   cDevice := aPrn[ nScan ] + "," + cText

return oPrinter := TPrinter():New( cDoc, .f., lPreview, cDevice, lModal, lSelection )
 
In FWH1912 it is

Code: Select all | Expand

...
return oPrinter := TPrinter():New( cDoc, .f., lPreview, aPrn[ nScan ], lModal, lSelection, cFile )
After I update my PrintBegin() equivalent in my sub-class, no more error
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Printer hDC is 0

Post by Enrico Maria Giordano »

hua wrote:Thanks Enrico.
Does this mean you maintain your own amended version of printer.prg?
Yes, I have some classes that I rewrote to adapt them to my own purposes.
Post Reply