The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Hunk on April 23, 2012, 10:49:36 AM

Title: Dialog Handle
Post by: Hunk on April 23, 2012, 10:49:36 AM
Is it possible to get the handle of a dialog box by using its Control ID only?
Title: Re: Dialog Handle
Post by: fearless on April 23, 2012, 11:30:59 AM
Could maybe use CreateDialogParam as it returns a handle to the dialog and uses the dialogs control id as a parameter: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645445%28v=vs.85%29.aspx

Maybe something like:
MyOtherDlgProc PROTO :DWORD, :DWORD, :DWORD, :DWORD

.const
IDD_OTHERDIALOG EQU 1000 ; dialog resource is set to 1000

.data?
hOtherDlg dd ? ; dialog handle

.code
Invoke CreateDialogParam, hInstance, IDD_OTHERDIALOG, NULL, OFFSET MyOtherDlgProc, NULL
mov hOtherDlg, eax ; save as handle to dialog in our var

Also as an alternative you could always save your dialogs handle from WM_INITDIALOG of your dialog to a global var, that can be accessed easily?
.ELSEIF eax == WM_INITDIALOG
   mov eax, hWin
   mov hOtherDlg, eax ; got hWin for this dialog, so save it for later use
Title: Re: Dialog Handle
Post by: dedndave on April 23, 2012, 11:35:53 AM
GetDlgItem function (you need the handle of the parent window, of course)
it works for non-dialog windows, as well
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645481%28v=vs.85%29.aspx
Title: Re: Dialog Handle
Post by: fearless on April 23, 2012, 11:40:49 AM
lol, i was just thinking that as well dave, GetDlgItem might be an easier option.
Title: Re: Dialog Handle
Post by: donkey on April 23, 2012, 02:39:25 PM
A dialog box does not in itself have a control ID, only controls have that. It does however have a resource ID and GetDlgItem will not return the handle of the dialog with that since it can be used to create multiple dialogs from a single resource. Since the resource ID is only used at creation to load the template and is not saved by Windows there is no way I know of to extract a handle from it. You will have to use fearless's suggestion and save the handle of the dialog when it is created or walk the window list for your program using FindWindow (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx) or some such function using the dialog title or class.
Title: Re: Dialog Handle
Post by: dedndave on April 23, 2012, 02:40:21 PM
it might, if it is a child window

i mean, apparently, he has a control ID of something and wants the handle   :bg
Title: Re: Dialog Handle
Post by: donkey on April 23, 2012, 02:45:20 PM
Quote from: dedndave on April 23, 2012, 02:40:21 PM
it might, if it is a child window   :P

Outside of simple tab controls I have never seen a dialog that has the WS_CHILD style set, though they may be used in property sheets, not much experience with those. I think he might be confusing a resource ID with a control ID.
Title: Re: Dialog Handle
Post by: dedndave on April 23, 2012, 02:47:59 PM
yah - the best way to get a dialog box handle is when you receive WM_INITDIALOG in the DialogProc
get it from the hwndDlg parameter and store it in a global
Title: Re: Dialog Handle
Post by: NoCforMe on April 23, 2012, 08:16:59 PM
You want the handle to a dialog box that's actually open, right? Well, it's not what you asked for, but it's really easy to get this, and you don't even need the dialog ID. Just  use


    INVOKE GetWindow, MainWinHandle, GW_ENABLEDPOPUP
    MOV dialogHandle, EAX


This gets the handle to the currently-enabled popup of your main window, which would be the dialog. Now, if you need to distinguish among several different dialogs which could be up, that's a different story, but if you know a particular dialog is open, this will get you its handle.