Author Topic: "line" procedure in MASM32 SDK library  (Read 103 times)

zedd151

  • Member
  • *****
  • Posts: 1268
"line" procedure in MASM32 SDK library
« on: September 24, 2022, 02:23:09 AM »
I needed an easy way to draw some lines. In Masm32 SDK I found line.asm
Upon reading about using GetDC, in all the examples I have seen those examples that use GetDC also make a call to ReleaseDC before returning to the caller. But the code in line.asm does not call ReleaseDC. Posting so others may be aware of this.
Code: [Select]
line proc hndl:DWORD,colr:DWORD,x1:DWORD,y1:DWORD,x2:DWORD,y2:DWORD
    LOCAL hDC     :DWORD
    LOCAL hPen    :DWORD
    LOCAL hPenOld :DWORD
    invoke GetDC,hndl
    mov hDC, eax


    invoke CreatePen,0,1,colr
    mov hPen, eax
 
    invoke SelectObject,hDC,hPen
    mov hPenOld, eax


    invoke MoveToEx,hDC,x1,y1,NULL


    invoke LineTo,hDC,x2,y2


    invoke SelectObject,hDC,hPenOld
    invoke DeleteObject,hPen


    ret
line endp


At any rate, I would most likely use a procedure like that within WM_PAINT like this... :

Code: [Select]
line2 proc hdc:dword, colr:dword, x1:dword, y1:dword, x2:dword, y2:dword
local hPen:dword, hPenOld:dword
    invoke CreatePen, 0, 1, colr
    mov hPen, eax
   
    invoke SelectObject, hdc, hPen
    mov hPenOld, eax
   
    invoke MoveToEx, hdc, x1, y1, NULL
    invoke LineTo, hdc, x2, y2
   
    invoke SelectObject, hdc, hPenOld
    invoke DeleteObject, hPen
    ret
line2 endp
Where the first argument is the HDC, rather than HWND.
... :biggrin:

NoCforMe

  • Member
  • ****
  • Posts: 685
Re: "line" procedure in MASM32 SDK library
« Reply #1 on: September 24, 2022, 03:33:12 AM »
You got that 100% correct; you don't want to be getting a DC every time, using it (and then not releasing it! that's a huuuuge rookie mistake! can you say "memory leak"?). UsuallyTM you'll put this in your WM_PAINT handler and pass it the DC you get from BeginPaint().

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9767
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: "line" procedure in MASM32 SDK library
« Reply #2 on: September 24, 2022, 01:04:33 PM »
 :biggrin:

Here is the 64 bit version of the same algo with the ReleaseDC() call and it still climbs in memory usage. Once it is run, the memory usage goes up but rerun it and it does not get any bigger. Microsoft lazy write and memory allocation. The old 32 bit one is dated 1999 and it ran fine on Win98se.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 lineX proc hWin:QWORD,x1:QWORD,x2:QWORD,y1:QWORD,y2:QWORD,cref:QWORD,pwid:QWORD

    LOCAL hDC   :QWORD
    LOCAL hPen  :QWORD
    LOCAL hOld  :QWORD

    mov hDC, rv(GetDC,hWin)
    mov hPen, rv(CreatePen,PS_SOLID,pwid,cref)
    mov hOld, rv(SelectObject,hDC,hPen)

    invoke MoveToEx,hDC,x1,x2,0
    invoke LineTo,hDC,y1,y2

    invoke SelectObject,hDC,hOld
    invoke DeleteObject,hPen
    invoke ReleaseDC,hDC

    ret

 lineX endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: