Author Topic: Output to printer not working  (Read 3428 times)

shankle

  • Member
  • ****
  • Posts: 835
Output to printer not working
« on: September 03, 2012, 05:36:12 AM »
9-2-2012
for a 32-bit program
Best I can tell GoBug seems to run without exceptions.
Since I am so new to GoBug don't count on it.
BUT, execution of TextOutA  shows as bad by the test code below.
TextOutA commands to the screen work fine. TextOutA commands
to the printer do not work.
My guess is the code in WM_Create never has been solved
sucessfully. Guess the handle for hdcPrn still has a problem.

DATA SECTION
dwNeeded       dd  0
dwReturned     dd  0
holdright         dd  0
holdbottom     dd  0
PrintText1      db   "MASTER COPY",0
PrintCString   db   96 DUP 0         ; buffer to receive the printer name     
szText           db   ' executed correctly',0
szText2         db   ' is bad',0
szCaption      db   'TRUE',0
szCaption      db   'FALSE',0

WndProc:
        FRAME hWnd,uMsg,wParam,lParam
        USES ebx,edi,esi
        Local hMemory,hdc,hdcPrn
       
.WM_CREATE
        cmp D[uMsg],WM_CREATE
        jne >>.WM_CHAR
        push ebx
        invoke  EnumPrinters, PRINTER_ENUM_LOCAL,NULL,4,NULL,\
                0,addr dwNeeded,addr dwReturned                                      
       invoke  GlobalAlloc, GHND,[dwNeeded]
       mov       [hMemory], eax
       invoke  GlobalLock, eax   
;?        mov       [pinfo4],eax   
       invoke  EnumPrinters, PRINTER_ENUM_LOCAL,NULL,4,addr PrintCString,\
                                [dwNeeded],addr dwNeeded,addr dwReturned
        mov ebx,addr PrintCString
        mov edx, [ebx+PRINTER_INFO_4.pPrinterName]
        invoke  CreateDC, NULL,edx,NULL,NULL
       mov       [hdcPrn],eax
;?       mov     [hdc],eax
        invoke  GlobalUnlock, [hMemory]   
        invoke  GlobalFree, [hMemory]
       
;    invoke SystemParametersInfoA, SPI_GETWORKAREA,0,addr rc,0       
;    holdright/holdbottom are filled by the SystemParametersInfoA api.           
        invoke Rectangle, [hdc],0,0,[holdright],[holdbottom]     
        pop ebx
        ret
       
; other code
;      1st TextOutA to the printer
       invoke TextOutA, [hdcPrn],2100,115,addr PrintText1,11   
       
; test begin code
; this gives a bad result for the TextOutA api             
       pushad
          cmp eax,0                  ; success
           je >
           invoke MessageBox, NULL, addr szText, addr szCaption, MB_OK ;true
           jmp >.outit
:             
           invoke MessageBox, NULL, addr szText2, addr szCaption2, MB_OK
.outit           
       popad
; test end code

; othercode
        ENDF
   

 
   

shankle

  • Member
  • ****
  • Posts: 835
Re: Output to printer not working
« Reply #1 on: September 04, 2012, 11:07:25 PM »
9-4-2012
for a 32-bit program
This is what I found after the above tests

Testing of CREATEDC shows that hdcPrn was created correctly
Testing of STARTDOC with hdcPrn executed correctly
Testing of STARTPAGE with hdcPrn executed correctly
Testing of CreateFontA executed correctly
Testing of SelectObject with hdcPrn gave an error. Invalid handle
I have executed a DeleteObject on all other uses of SelectObject
 

DATA SECTION
dwNeeded       dd  0
dwReturned     dd  0
holdright         dd  0
holdbottom     dd  0
PrintCString    db   96 DUP 0         ; buffer to receive the printer name     

WndProc:
        FRAME hWnd,uMsg,wParam,lParam
        USES ebx,edi,esi
        Local hMemory,ps:PAINTSTRUCT,hdc,hdcPrn,DI:DOCINFO           
       
.WM_CREATE
        cmp D[uMsg],WM_CREATE
        jne >>.WM_CHAR
        push ebx
        invoke  EnumPrinters, PRINTER_ENUM_LOCAL,NULL,4,NULL,\
                0,addr dwNeeded,addr dwReturned                                      
       invoke  GlobalAlloc, GHND,[dwNeeded]
       mov       [hMemory], eax
       invoke  GlobalLock, eax   
;?        mov       [pinfo4],eax   
       invoke  EnumPrinters, PRINTER_ENUM_LOCAL,NULL,4,addr PrintCString,\
                                [dwNeeded],addr dwNeeded,addr dwReturned
        mov ebx,addr PrintCString
        mov edx, [ebx+PRINTER_INFO_4.pPrinterName]
        invoke  CreateDC, NULL,edx,NULL,NULL
       mov       [hdcPrn],eax
;?       mov     [hdc],eax
        invoke  GlobalUnlock, [hMemory]   
        invoke  GlobalFree, [hMemory]
       
;    invoke SystemParametersInfoA, SPI_GETWORKAREA,0,addr rc,0       
;    holdright/holdbottom are filled by the SystemParametersInfoA api.           
        invoke Rectangle, [hdc],0,0,[holdright],[holdbottom]     
        pop ebx
        ret
       
; other code

       mov eax, SIZEOF DOCINFO           ; fill in fields of the DOCINFO Struct
       mov [DI.cbSize],eax
       mov eax,addr AppName
       mov [DI.lpszDocName], eax
       mov D[DI.lpszOutput],NULL
       mov D[DI.lpszDatatype],NULL
       mov D[DI.fwType],0
       
       invoke StartDoc, [hdcPrn],addr DI       
       invoke StartPage, [hdcPrn]                                         
       invoke  CreateFontA, 200,74,0,0, FW_SEMIBOLD, FALSE,FALSE,FALSE, \
               DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, \
               PROOF_QUALITY, FF_DONTCARE, addr szTimesNR
       mov   [hNFont], eax       
       invoke  SelectObject,[hdcPrn],[hNFont] ;(TESTED BAD)
       push eax               

; othercode
        ENDF
   

 
   

wjr

  • Member
  • **
  • Posts: 209
    • WJR's website
Re: Output to printer not working
« Reply #2 on: September 05, 2012, 12:50:43 AM »
You obtain hdcPrn during WM_CREATE and save it as a LOCAL variable. Once you hit ENDF for WM_CREATE, the value for hdcPrn on the stack will most likely be overwritten and most likely not even in the same location when processing other messages. I think this is the problem that you are encountering... so if you intend on using it later while processing other messages, hdcPrn should be in your DATA SECTION instead.

shankle

  • Member
  • ****
  • Posts: 835
Re: Output to printer not working
« Reply #3 on: September 05, 2012, 09:18:27 PM »
Just want to thank everyone for their help through this learning process.
Especially WJR.
The program is approximately 95% working. Printing is working though
I have something set wrong.
Then the 64-bit hassle. Will be starting from scratch. :biggrin: