News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Get window handle/title from executable..

Started by travism, May 22, 2009, 02:27:20 PM

Previous topic - Next topic

travism

Hey everyone, Im wondering if there is a way to get the window title from the executable file name like "blah.exe" or if after ive got a snapshot of the current process list if i can retrieve it somehow... I know I need to get the handle to the window, Ive tried a couple ways but all have failed. :S

Vortex

I guess your strategy to solve the problem should be based on those APIs below

FindWindow or FindWindowEx
EnumWindows

travism

Well the thing is, I got a snapshot of all the processes so they are in a certain order, I want to also display the window title of each process along with it, so I just need to make sure its in the same order, I thought about doing it seperatly, but I didn't believe it would be in the same order as the processes. :\ So I was trying to figure out a way as each time the call for the next snapshot ill receive a new process id and exe name and module id etc... if there was a way to get it from that so i would be guaranteed it was the right window text. :S

jj2007

If you have the processes, you might try this one:

The EnumThreadWindows function enumerates all nonchild windows associated with a thread by passing the handle of each window, in turn, to an application-defined callback function. EnumThreadWindows continues until the last window is enumerated or the callback function returns FALSE. To enumerate child windows of a particular window, use the EnumChildWindows function. This function supersedes the EnumTaskWindows function.

BOOL EnumThreadWindows(

    DWORD dwThreadId,   // thread identifier
    WNDENUMPROC lpfn,   // pointer to callback function
    LPARAM lParam    // application-defined value
   );   


Parameters

dwThreadId

Identifies the thread whose windows are to be enumerated.

lpfn

Points to an application-defined callback function. For more information about the callback function, see the EnumThreadWndProc callback function.

lParam

Specifies a 32-bit, application-defined value to be passed to the callback function.


Return Values

If the function succeeds, the return value is nonzero.

Vortex

Hi travism,

Here is my attempt :


include FindProcess.inc

.data

pExeFile db 'DlgBox.exe',0
capt1    db 'Error',0
capt2    db 'Process info',0
msg1     db 'Process DlgBox.exe could not be found',0
format1  db 'Handle to DlgBox.exe = %X',13,10
         db 'Caption                       = %s',0

.code

start:

    invoke  WinExec,ADDR pExeFile,SW_SHOW
    call    FindProcess
    invoke  ExitProcess,0

FindProcess PROC USES esi

LOCAL pe32:PROCESSENTRY32
LOCAL hProcessSnap:DWORD
LOCAL nVar:DWORD        ; this variable stores the process ID
LOCAL buffer[128]:BYTE  ; and the window handle respectively
LOCAL szTitle[16]:BYTE

MAX_COUNT   equ SIZEOF szTitle

    lea     esi,[pe32]
    invoke  CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
    mov     hProcessSnap,eax
    mov     pe32.dwSize,SIZEOF(PROCESSENTRY32)
    invoke  Process32First,eax,esi
@@:
    invoke  lstrcmp,ADDR pExeFile,ADDR PROCESSENTRY32.szExeFile[esi]
    test    eax,eax
    jz      @f
    invoke  Process32Next,hProcessSnap,esi
    test    eax,eax
    jnz     @b
    invoke  CloseHandle,hProcessSnap
    invoke  MessageBox,0,ADDR msg1,ADDR capt1,MB_ICONWARNING
    ret
@@:
    invoke  CloseHandle,hProcessSnap
    lea     eax,[nVar]
    push    PROCESSENTRY32.th32ProcessID[esi]
    pop     DWORD PTR [eax]
    invoke  EnumWindows,ADDR EnumWndProc,eax
    invoke  GetWindowText,nVar,ADDR szTitle,MAX_COUNT
    invoke  wsprintf,ADDR buffer,ADDR format1,nVar,ADDR szTitle
    invoke  MessageBox,0,ADDR buffer,ADDR capt2,MB_ICONINFORMATION
    invoke  SendMessage,nVar,WM_CLOSE,0,0
    ret

FindProcess ENDP

EnumWndProc PROC hWnd:DWORD,lParam:DWORD

LOCAL pid:DWORD
   
    invoke  GetWindowThreadProcessId,hWnd,ADDR pid

    mov     edx,lParam
    mov     eax,DWORD PTR [edx]
    cmp     eax,pid
    je      @f
    mov     eax,1
    ret
@@:
    push    hWnd
    pop     DWORD PTR [edx]
    xor     eax,eax
    ret

EnumWndProc ENDP

END start


FindProcess.exe will execute DlgBox.exe and search the process displaying the dialog box.

[attachment deleted by admin]