Printer hDC is 0

Printer hDC is 0

Postby hua » Wed Feb 15, 2023 4: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: Select all  Expand view  RUN
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 view  RUN

   // 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 view  RUN

   //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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Printer hDC is 0

Postby Enrico Maria Giordano » Wed Feb 15, 2023 10:05 am

This is how i am using it:

Code: Select all  Expand view  RUN
::hDC = PRINTERDCFROMNAME( STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 1, "," ), cModel, STRTOKEN( GETPROFSTRING( "Devices", cModel, "" ), 2, "," ) )
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Printer hDC is 0

Postby karinha » Wed Feb 15, 2023 3:49 pm

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7831
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Printer hDC is 0

Postby hua » Thu Feb 16, 2023 2: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: Select all  Expand view  RUN
::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: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Printer hDC is 0

Postby hua » Thu Feb 16, 2023 2:28 am

FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Printer hDC is 0

Postby hua » Thu Feb 16, 2023 6: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: Select all  Expand view  RUN
 
   ...
   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 view  RUN
...
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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Printer hDC is 0

Postby Enrico Maria Giordano » Thu Feb 16, 2023 8: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.
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 77 guests