flat assembler
Message board for the users of flat assembler.

Index > Windows > GetTextMetrics crashing my program?

Author
Thread Post new topic Reply to topic
silkodyssey



Joined: 02 Oct 2003
Posts: 198
Location: St.Vincent & the Grenadines
silkodyssey 17 Oct 2003, 16:21
I am trying to convert petzold's system metrics program to fasm but the call to GetTextMetrics in the window proecedure is crashing the program and I have no idea why. If I comment out the call to GetTextMetrics, the program runs otherwise it crashes, Any help would be appreciated.

Code:
 

; template for program using standard Win32 headers

format PE GUI 4.0
entry start

include '%include%\win32a.inc'

section '.data' data readable writeable
   include 'sysmets.inc'

  _title db 'Get System Metrics No. 1',0
  _class db 'SysMets1',0

  hInstance dd ?
  CommandLine dd 0

  cxCaps dd 0
  cxChar dd 0
  cyChar dd 0

section '.code' code readable executable

  start:

        invoke  GetModuleHandle,0
        mov     [hInstance],eax
        invoke  GetCommandLine
        mov     [CommandLine], eax
        stdcall WinMain, [hInstance],NULL,[CommandLine], SW_SHOWDEFAULT
        invoke  ExitProcess,eax

proc WinMain, hInst, hPrevInst, CmdLine, CmdShow
        .wc     WNDCLASSEX
        .msg    MSG
        .hwnd   dd 0

        enter
        mov     [.wc.cbSize], sizeof.WNDCLASSEX
        invoke  LoadIcon,0,IDI_APPLICATION
        mov     [.wc.hIcon],eax
        mov     [.wc.hIconSm], eax
        invoke  LoadCursor,0,IDC_ARROW
        mov     [.wc.hCursor],eax
        mov     [.wc.style], CS_HREDRAW or CS_VREDRAW
        mov     [.wc.lpfnWndProc],WindowProc
        mov     [.wc.cbClsExtra],0
        mov     [.wc.cbWndExtra],0
        mov     eax,[hInstance]
        mov     [.wc.hInstance],eax
        mov     [.wc.hbrBackground],COLOR_WINDOW+1
        mov     [.wc.lpszMenuName],0
        mov     [.wc.lpszClassName],_class
        lea     eax, [.wc]
        invoke  RegisterClassEx, eax

        invoke  CreateWindowEx,0,_class,_title,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,[hInst],NULL
        mov     [.hwnd],eax

        invoke  ShowWindow, [.hwnd], [CmdShow]
        invoke  UpdateWindow, [.hwnd]

  msg_loop:
        lea     eax, [WinMain.msg]
        invoke  GetMessage,eax,NULL,0,0
        or      eax,eax
        jz      end_loop
        lea     eax, [WinMain.msg]
        invoke  TranslateMessage, eax
        lea     eax, [WinMain.msg]
        invoke  DispatchMessage, eax
        jmp     msg_loop

  end_loop:
        mov     eax,[WinMain.msg.wParam]
        return

proc WindowProc, hwnd,wmsg,wparam,lparam

        .hdc      dd 0
        .ps       PAINTSTRUCT
        .szBuffer rb 10
        .tm      TEXTMETRIC

        enter
        push    ebx esi edi
        cmp     [wmsg],WM_DESTROY
        je      wmdestroy
        cmp     [wmsg], WM_CREATE
        je      wmcreate
  defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     finish
  wmcreate:
        invoke  GetDC, [hwnd]
        mov     [WindowProc.hdc], eax

        lea     edx, [WindowProc.tm]
        ; here is the problem 
        invoke  GetTextMetrics, [WindowProc.hdc] , edx

        ;mov     eax, [WindowProc.tm.tmAveCharWidth]
        ;mov     [cxChar], eax

        ;mov     al, [WindowProc.tm.tmPitchAndFamily]
        ;and     al, 1
        ;cmp     al, 1
        ;jne     .bitnotset
        ;mov     eax, 3
        ;jmp     .continue
  .bitnotset:
        ;mov     eax, 2
  .continue:
        ;mul     [cxChar]
        ;mov     edx, 0
        ;mov     ecx, 2
        ;div     ecx
        ;mov     [cxCaps], eax

        ;mov     eax, [WindowProc.tm.tmHeight]
        ;add     eax, [WindowProc.tm.tmExternalLeading]
        ;mov     [cyChar], eax

        ;invoke  ReleaseDC, [hwnd], [WindowProc.hdc]
        ;xor     eax, eax
        jmp     finish

  wmdestroy:
        invoke  PostQuitMessage,0
        xor     eax,eax
  finish:
        pop     edi esi ebx
        return

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL'

  include '%include%\apia\kernel32.inc'
  include '%include%\apia\user32.inc'
  include '%include%\apia\gdi32.inc'  

    

_________________
silkodyssey
Post 17 Oct 2003, 16:21
View user's profile Send private message MSN Messenger Reply with quote
silkodyssey



Joined: 02 Oct 2003
Posts: 198
Location: St.Vincent & the Grenadines
silkodyssey 17 Oct 2003, 16:26
LOL I just figured out what I was doing wrong Very Happy . I forgot to include 'gdi32,'GDI32.DLL' in the import section. Why didn't fasm warn me about this?

_________________
silkodyssey
Post 17 Oct 2003, 16:26
View user's profile Send private message MSN Messenger Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 18 Oct 2003, 12:19
Because importing functions is implemented as macroses and they don't check if you don't declare the name of library you're using.
Post 18 Oct 2003, 12:19
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.