# Getting a Windows Caption [Win32]

This topic is 3967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, This is very similar to my last thread, which I still havn't solved...but I think i'm trying too hard for something that should be rather straight forward. I'm trying to get a Caption in a Edit window of another application I didn't write. I've tried a couple different things. --------------------------------------- 1. I've tried EnumWindows, then EnumChildWindows to get the HWND of the control i'm looking for, then throwing a GetWindowsText() at it. 2. I've tried doing a GetProcessThreadById() and then an OpenProcess(), to try to gain control of the window...I hardly understood anything I was doing here. 3. I tried just grabbing the Handle ID with Microsoft Spy++, and sending the WM_GETTEXT message via SendMessage() function. none of which worked, but it's very possible I just did them wrong. I think it should be rather easy, because when I use the spy++ application, I drag the ToolFinder thing to the window, and bingo, it grabs all the text and throws it into the Window Caption Textbox of the Spy++ Window Property Dialog...Why can't I do that? This Spy++ application I'm guessing gets the Windows Caption of every window the same way right? I'm only trying to get the Caption for one control...this should be easy? Anyone know how Spy++ gets the Windows caption, or any ideas of what i'm doing wrong. Thanks again, to everyone who helps or reads this, much appreciated. Kyle

##### Share on other sites
You cannot access the windows (including child windows like controls) of another process in 32-bit Windows. This means anything that requires an HWND, for example, wants an HWND that belongs to your program. If the HWND belongs to some other process (and does not, by random chance, correspond to an HWND in your own process), the call will fail.

If you want to read values out of controls in someone else's application, you need to do process injection to run some of your own code inside of the other program's process.

The canonical way to do this in Win32 is using hooks, specifically via the SetWindowsHookEx function.

This Google search should guide you to some usable example code, and at least present the idea in more detail.

##### Share on other sites
Quote:
 Original post by ApochPiQYou cannot access the windows (including child windows like controls) of another process in 32-bit Windows. This means anything that requires an HWND, for example, wants an HWND that belongs to your program. If the HWND belongs to some other process (and does not, by random chance, correspond to an HWND in your own process), the call will fail.

Eh? Sure you can. Here's a little app that reads the title of my Firefox window.
#include "stdafx.h"#include <iostream>#include <windows.h>int _tmain(int argc, _TCHAR* argv[]){	const unsigned int HANDLE_THAT_I_GOT_FROM_SPYPLUSPLUS = 0x001A01F8;	TCHAR blah[255];	LRESULT result = ::SendMessage((HWND)HANDLE_THAT_I_GOT_FROM_SPYPLUSPLUS, WM_GETTEXT, 255, (LPARAM)blah);	if(result > 0)		std::wcout << blah << std::endl;	return 0;}

Output:
Getting a Windows Caption [Win32] - GameDev.Net Discussion Forums - Mozilla Firefox

ArchG, can you post your code?

##### Share on other sites
0. You can get almost any information from another application. Everything that Spy++ is doing you can too.

1. You'll need to send the window WM_GETTEXT explicitly. As stated in the documentation, GetWindowText() does not work across processes.

2. It sounds like your only problem is that you arent getting the right HWND. If you know the parent's class name and preferably its window title then you can use FindWindow() to get that HWND. From there you can use EnumChildWindows() to search for your edit.

##### Share on other sites
Quote:
 Original post by MarteeEh? Sure you can. Here's a little app that reads the title of my Firefox window.

Huh... I stand corrected [smile]