News:

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

failed to get window caption

Started by elmo, March 31, 2011, 08:37:03 AM

Previous topic - Next topic

elmo

I usually get Window Caption with GetWindowText API
   example:   invoke GetWindowText,hWin,addr buff,1024
where hWin is a handle of my window application


but when I try to get Window Caption in the following code, it show empty string on the MessageBox.
That's weird.
maybe I missunderstand.
Would you like to give me a clue? and give me a link that explain about this?
Thank you

http://www.masm32.com/board/index.php?PHPSESSID=fa4590ba57dbaad4bc44088172af0b49&topic=15254.0
http://www.masm32.com/board/index.php?PHPSESSID=faf058b6f7a48b76b16d2cdf2af10712&topic=10074.0






.386
.model flat,stdcall
option casemap:none

;INCLUDE
include C:\masm32\include\kernel32.inc
include C:\masm32\include\windows.inc
include C:\masm32\include\user32.inc
include C:\masm32\include\masm32.inc
include C:\masm32\macros\macros.asm

;LIBRARY
includelib C:\masm32\lib\user32.lib
includelib C:\masm32\lib\kernel32.lib
includelib C:\masm32\lib\masm32.lib




;VARIABLE
.data




;VARIABLE
.data?
pe32 PROCESSENTRY32 <>
hProcesses HANDLE ?
hProcess HANDLE ?
strText db 1024 dup(?)



;CODE
.code
start:

invoke CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
mov hProcesses,eax ;Did not need 2 copies of your snap shot
mov pe32.dwSize,SIZEOF PROCESSENTRY32
invoke Process32First,hProcesses,ADDR pe32
.IF eax
.WHILE eax
invoke CompareString,
LOCALE_USER_DEFAULT,
NORM_IGNORECASE,
addr pe32.szExeFile,
-1,
SADD("notepad.exe"),
-1
.IF eax==2
invoke OpenProcess,
PROCESS_TERMINATE, FALSE,
pe32.th32ProcessID ;With having addr pe32.th32ProcessID you were getting an invalid PID
.IF eax!=NULL
mov hProcess, eax ;Need to save the process handle to terminate
invoke GetWindowText,hProcess,addr strText,1024 ;=>TRY TO GET WINDOW CAPTION HERE BUT FAILED
invoke MessageBox,0,addr strText,SADD("caption"),0
invoke TerminateProcess, hProcess, 0
invoke CloseHandle, hProcess
.endif
.endif
invoke Process32Next, hProcesses, ADDR pe32
.endw
invoke CloseHandle,hProcesses
.endif


invoke ExitProcess,NULL
end start

be the king of accounting programmer world!

sinsi

GetWindowText needs a window handle, not a process handle.
Also, MSDN says if it's a window owned by another process to use WM_GETTEXT.
Light travels faster than sound, that's why some people seem bright until you hear them.

elmo

hi sinsi,
thank you for quick Reply and great explanation :U

So, I remove line
           invoke GetWindowText,hProcess,addr strText,1024 ;=>TRY TO GET WINDOW CAPTION HERE BUT FAILED
with this:
           invoke SendMessage,hProcess,WM_GETTEXT,sizeof  strText,addr strText
But it still show empty string in the MessageBox.

what happened?
that's weird ::)
be the king of accounting programmer world!

sinsi

You only have the handle to the process (hProcess), you need the handle to the window owned by that process (hWnd).
It might be easier to find the window via enumeration then get the process that owns it.
Light travels faster than sound, that's why some people seem bright until you hear them.

ragdog

Get the windows caption text use FindWindow and the return handle can you use


invoke FindWindow,0,CTEXT("WINDOW TEXT")
.if (eax)
mov hWindow,eax
invoke GetWindowText,hWindow,addr strText,1024
.else
  ;FALSE window not found
.endif

elmo

Maybe I'm false to tell what I mean.

My Purpose: You can look at my file attachment. There are 2 image files in it (taskbar.JPG and tm.JPG).
I have 4 application avail on my taskbar like the picture taskbar.JPG
I use WinXP. I do ctrl+alt+del to show TaskManager. It will list all caption of 4 application that avail in taskbar like picture tm.JPG
With my previous code, I only hope I can get "File1.asm-Notepad" on my MessageBox.
But that's failed.
How to do this?
Thank you.

Sorry for my bad English.
be the king of accounting programmer world!

dedndave

not really sure what you want, exactly
you may want to enumerate processes and find the notepad proc
however, this will be confusing if you have more than one instance of notepad opened


qWord

hi elmo,
you can use EnumWindow() to enumerate all top-level windows. To find the right window(s), you can analyse the title of the window (GetWindowtext()).
In the attachment an example listing all notepad instances, where *.asm and *.inc files are opened.
FPU in a trice: SmplMath
It's that simple!

ragdog

Here is a Quick example for EnumWindows


.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
include \masm32\include\windows.inc

include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

EnumWindowsProc PROTO :DWORD,:DWORD

.CODE
START:
    invoke EnumWindows,offset EnumWindowsProc,0
    invoke ExitProcess,0

EnumWindowsProc PROC hWnd:DWORD, lParam:DWORD
   LOCAL Buffer[256]:BYTE
   .DATA
       lf DB 10,13,0
   .CODE
    invoke GetWindowText,hWnd, addr Buffer,255
     .if eax
  invoke StdOut,addr Buffer
  invoke StdOut,addr lf
     .endif
    mov eax,hWnd
   ret
EnumWindowsProc ENDP
END START