FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Printer hDC is 0
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Printer hDC is 0
Posted: Wed Feb 15, 2023 04:34 AM
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 (fw): Select all Collapse
 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 (fw): Select all Collapse
   // 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 (fw): Select all Collapse
   //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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Printer hDC is 0
Posted: Wed Feb 15, 2023 10:05 AM
This is how i am using it:
Code (fw): Select all Collapse
::hDC = PRINTERDCFROMNAME( STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 1, "," ), cModel, STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 2, "," ) )
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Printer hDC is 0
Posted: Wed Feb 15, 2023 03:49 PM
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Printer hDC is 0
Posted: Thu Feb 16, 2023 02:23 AM
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 (fw): Select all Collapse
::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
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Printer hDC is 0
Posted: Thu Feb 16, 2023 02:28 AM
FWH 11.08/FWH 19.12

BCC5.82/BCC7.3

xHarbour/Harbour
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Printer hDC is 0
Posted: Thu Feb 16, 2023 06:28 AM
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 (fw): Select all Collapse
   
   ...
   cText   := GetProfString( "Devices", aPrn[ nScan ] )
   cDevice := aPrn[ nScan ] + "," + cText

return oPrinter := TPrinter():New( cDoc, .f., lPreview, cDevice, lModal, lSelection )
In FWH1912 it is
Code (fw): Select all Collapse
...
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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Printer hDC is 0
Posted: Thu Feb 16, 2023 08:33 AM
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.

Continue the discussion