News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

[SOLVED]DialogBoxParam pops the dialog but i cannot close it

Started by bluedevil, August 29, 2022, 09:28:30 AM

Previous topic - Next topic

bluedevil

Hello;
I am trying Mikl__'s samples. Thank you Mikl__ btw.

I have wrote a small code to open a dialog from template. I can see the template but i cannot close it from X button. Alt+F4 combination also doesn't works


    OPTION DOTNAME                          ; required for macro files
    option casemap:none                     ; case sensitive
; _____________________________________________________________________________
; include files
    include \masm64\include64\win64.inc     ; main include file
    include \masm64\include64\kernel32.inc
    include \masm64\include64\user32.inc
    include \masm64\include64\comctl32.inc
   
; _____________________________________________________________________________
; libraries
    includelib \masm64\lib64\user32.lib
    includelib \masm64\lib64\kernel32.lib
    includelib \masm64\lib64\comctl32.lib

; _____________________________________________________________________________
; MASM64 macros
    include \masm64\macros64\vasily.inc     ; main macro file
    include \masm64\macros64\macros64.inc   ; auxillary macro file
   

; _____________________________________________________________________________
; constant variables
.const
    ; Main Dialog
    IDD_DIALOG EQU 1000
; _____________________________________________________________________________
; initialized variables
.data
    lpTitle     db ".title",0
    lpMessa     db ".message",0

.code

WinMainCRTStartup proc hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD

    invoke InitCommonControls
    invoke DialogBoxParam,hInstance, IDD_DIALOG,0,addr DlgProc,NULL

    invoke ExitProcess,NULL

WinMainCRTStartup endp

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
   
mov eax,uMsg
.if edx==WM_INITDIALOG
        invoke MessageBox,hWnd,addr lpMessa,addr lpTitle,MB_OK
.elseif edx==WM_COMMAND
        ; code here
.elseif edx==WM_CLOSE
invoke EndDialog,hWnd,0
ret
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp

end


I have debug the code via debugger and understand that in DlgProc the uMsg does not carry the WM_xxxx messages?

Where am i doing wrong.

PS. I am using masm64 sdk.

I have attached my radasm2 project.
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

zedd151

#1
Nevermind this post. Mikl__ has answered better than I could have.
See his post below this one.


I removed my comments and code to avoid further confusion.  :undecided:

Mikl__

Hi, blue_devil!
DialogAppx64.asm; GUI #
include win64a.inc     ; main include file
; constant variables
IDD_DIALOG EQU 1000
IMAGE_BASE EQU 400000h 
; _____________________________________________________________________________
; initialized variables
.data
    lpTitle     db ".title",0
    lpMessa     db ".message",0
.code
WinMain proc hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD

invoke InitCommonControls
invoke DialogBoxParamA,IMAGE_BASE, IDD_DIALOG,0,addr DlgProc,NULL
        invoke RtlExitUserProcess,NULL
WinMain endp

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
        mov hWnd,rcx

cmp edx,WM_INITDIALOG
je wmINITDIALOG
cmp edx,WM_COMMAND
je wmCOMMAND
cmp edx,WM_CLOSE
je wmCLOSE
xor eax,eax;eax=FALSE
jmp exit_

wmINITDIALOG:
mov edx,offset lpMessa
mov r8d,offset lpTitle
        invoke MessageBoxA,hWnd,,,MB_OK
jmp wmBYE
wmCOMMAND:; code here
jmp wmBYE
wmCLOSE:invoke EndDialog,hWnd,0
wmBYE: mov eax,TRUE
exit_: leave
ret
DlgProc endp
end
DialogAppx64.rc#include "resource.h"
#define IDD_DIALOG 1000
#define FW_NORMAL 400
#define FALSE 0

IDD_DIALOG DIALOGEX 6,6,189,99
FONT 8,"MS Sans Serif",FW_NORMAL, FALSE, 1
STYLE WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_VISIBLE | WS_CAPTION
| WS_SYSMENU | WS_THICKFRAME
BEGIN
END


zedd151

Hi Mikl__
I noticed that you too have not called GetModuleHandle for the instance handle. Does it work without?
I'm on my 32 bit box so can't test it


Your example has some strange looking code. I might have to load up 64 bit OS to investigate
Seems mixing 32 and 64 bit registers. ?? How odd.

Mikl__

Hi, Swordfish!
hInstance = IMAGE_BASE = 400000h
the GetModuleHandle function returns this value because the /BASE:0x400000 key was specified during assembly

zedd151

Doh!!! I totally missed that. But why even set the image base manually. I don't understand.
So what about the mixed 32 and 64 bit register usage. Looks odd to me in a relatively mundane function.
I haven't coded anything 64 bit in a few years, maybe I need a refresher course.  :biggrin:

Mikl__

QuoteSo what about the mixed use of 32 and 64 bit registers.
a value in a 32-bit register is padded with zeros to 64 bits, but when encoded, 32-bit registers take up less space
Some tricks for Win x64

zedd151

Quote from: Mikl__ on August 29, 2022, 11:34:49 AM
QuoteSo what about the mixed use of 32 and 64 bit registers.
a value in a 32-bit register is padded with zeros to 64 bits, but when encoded, 32-bit registers take up less space
Ok. Never knew that one, about the padding that is. No movzx to accomplish it, cool. As said it's been years since I dabbled in 64 bit code. Thanks for the clarification. I removed my code and comments in my first post here as you have explained much better than I could have.

TimoVJL

Quote from: blue_devil on August 29, 2022, 09:28:30 AM
WinMainCRTStartup proc hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD

    invoke InitCommonControls
    invoke DialogBoxParam,hInstance, IDD_DIALOG,0,addr DlgProc,NULL

    invoke ExitProcess,NULL

WinMainCRTStartup endp

Actually WinMainCRTStartup is void __stdcall WinMainCRTStartup (void)
so that hInstance is bogus.

https://docs.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol?view=msvc-170

May the source be with you

bluedevil

Thank you all for your replies, i've fixed the situation:
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    mov     hWnd,rcx
mov uMsg,edx
.if uMsg==WM_INITDIALOG
        invoke MessageBox,hWnd,addr lpMessa,addr lpTitle,MB_OK
.elseif uMsg==WM_COMMAND
        ; code here
.elseif uMsg==WM_CLOSE
invoke EndDialog,hWnd,0
ret
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp



* According to the x64 Microsoft Calling Conventions we know that first 4 parameters pass to the rcx,rdx,r8,r9. So as you can see above i passed rcx to hWnd and rdx to uMsg variables. So in our MASM64 sdk is this  line  useless: hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM Am I right?

I can write this dialog proc like that:
DlgProc proc
    LOCAL hWnd:HWND
    LOCAL uMsg:UINT
    mov     hWnd,rcx
mov uMsg,edx
.if uMsg==WM_INITDIALOG
        invoke MessageBox,hWnd,addr lpMessa,addr lpTitle,MB_OK
.elseif uMsg==WM_COMMAND
        ; code here
.elseif uMsg==WM_CLOSE
invoke EndDialog,hWnd,0
ret
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp


hutch-- has lot of examples like above. So is this the way, that I should write my assembly programs? Is this MASM64 convention(I mean according to the MASM32)?

..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

jj2007

Quote from: blue_devil on August 29, 2022, 08:34:25 PMSo in our MASM64 sdk is this  line  useless: hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

No, it's not useless, it shows the args. There are also ways to get these arguments filled by the assembler, instead of fumbling with the registers rcx, rdx etc.

bluedevil

Quote from: jj2007 on August 29, 2022, 08:42:35 PM
Quote from: blue_devil on August 29, 2022, 08:34:25 PMSo in our MASM64 sdk is this  line  useless: hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

No, it's not useless, it shows the args. There are also ways to get these arguments filled by the assembler, instead of fumbling with the registers rcx, rdx etc.

Hello jj, how're you?
I am looking for the way to get those arguments fiilled by assemblers. What/How is that way?
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

bluedevil

If i omit main functions parameters i can compile my code but the executable do not show dialog:


WinMainCRTStartup proc ;hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD
    LOCAL hInstance:HINSTANCE
    mov hInstance,rcx
    invoke InitCommonControls
    invoke DialogBoxParam,hInstance, IDD_DIALOG, 0, addr DlgProc, NULL

    invoke ExitProcess,NULL

WinMainCRTStartup endp
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

TimoVJL

use this to get hInstance
invoke GetModuleHandle,NULL
like this    invoke GetModuleHandle,NULL
    invoke DialogBoxParam,rax, IDD_DIALOG,0,addr DlgProc,NULL
May the source be with you

bluedevil

I think i cannot express myself right

This compiles and runs:

WinMainCRTStartup proc hInst:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD

    invoke InitCommonControls
    invoke GetModuleHandle,NULL
    invoke DialogBoxParam,rax, IDD_DIALOG,0,addr DlgProc,NULL

    invoke ExitProcess,NULL

WinMainCRTStartup endp


This also compiles but does not run:

WinMainCRTStartup proc

    invoke InitCommonControls
    invoke GetModuleHandle,NULL
    invoke DialogBoxParam,rax, IDD_DIALOG,0,addr DlgProc,NULL

    invoke ExitProcess,NULL

WinMainCRTStartup endp


Is it possible for me to make assembler to pass arguments to these arguments lines:

hInstance:HINSTANCE, hPrevInstance:HINSTANCE, lpCmdLine:LPSTR, nCmdShow:DWORD
or
hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github