Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window handle IDs are not static

Status
Not open for further replies.

GWS2009

Programmer
Dec 8, 2009
2
0
0
US
Hi, all.

I'm not a Delphi guy, so forgive the stupid question. I notice when running a Delphi application that the window handles for controls (edit fields, buttons, etc.) are not static. In other words, an edit field on an application might have an ID of 0x000502cd the first time I run it, and the same edit field could have an ID of 0x00001133 the next time I run the program.


I'm interested in determining which field in an executable has focus at any given point in time, and obviously the non-static nature of the controls presents a problem. Is there any way to either force the control IDs to be static, or to otherwise identify a particular control between instantiations of an application?


Additional (possibly significant information): I'm only interested in Delphi on Windows operating systems. I have a good background in VC++ and the program that's snooping for this information is written in VC++. I determine the currently running program and the ID of the control in
this fashion:


HWND hwnd = GetForegroundWindow();
LONG_PTR lpID;
DWORD dwProcID;
TCHAR lpszFilename[MAX_PATH];


// Do some testing of hwnd (make sure it's not this process, etc.)


// Get the process name (very abbreviated)
GetWindowThreadProcessId(hwnd,&dwProcID);
HANDLE hProc = OpenProcess(...,dwProcID);
HMODULE hMod;
EnumProcessModules(hProc,&hModule,....)
GetModuleBase(hProc,hModule,lpszFilename,MAX_PATH);


// Get the window with focus
GUITHREADINFO tInfo;
tInfo.cbSize = sizeof(GUITHREADINFO);
GetGUIThreadInfo(NULL,&tInfo);
if (tInfo.hwndFocus)
{
lpID = GetWindowLongPtr(tInfo.hwndFocus,GWLP_ID);


// do stuff depending on process name and lpID



}


Thanks for any information.



 
I may be totally misunderstanding your question, but I've have always understood that window handles were only valid for the particular instance of a program - they were expected to be different every time you ran a program. You could also have multiple copies of the same program running at once, and clearly they would need to have different handles for the windows which were otherwise the same.

In any case, every control has a name by which you can refer to it, and every control which can have focus has a property that indicates whether it has focus or not at that particular moment. I think that's all you need "identify a particular control between instantiations of an application".
 
You're right: the window handles are different each time. I phrased it badly. What I was referring to was the window IDs: the value returned from GetWindowLongPtr(tInfo.hwndFocus,GWLP_ID).

I also failed to note that I'm doing this from an outside program: not from within the Delphi program itself, but from an application started when the user logs on. My problem is that the window IDs of the controls change every time the program runs, and I'm using the ID to determine context. For example, explorer.exe's "Address" field has an ID of 0x0000a205; its folder tree view pane has an ID of 0x00000064; its filename pane has an ID of 0x00000001. My application uses these values to recognize what the user is doing at any given time. For example, when my program notices that the "address" field of explorer has focus, it can provide a clue to the user in the form of a pop-up message: "Type a file pathname."

What we're trying to provide is something like tooltips for programs to which we don't have the source code. This works great for C and VC programs, but I can't determine "context" of a control in a Delphi program.

So is there a way to either 1) run a Delphi program with a "/makewindowidsconstant" switch :) or 2) get the name of the control (as you suggest) from an external program?

Yeah, I didn't think so.


 
Ah. Totally different problem.

I'm quite sure there is a way to do this. Once it's compiled a Delphi program is just like any other Windows app. However, I have not personally had to deal with this, so I'm afraid I'm not liable to be any help. It would not surprise me if this had not been addressed on some other, ancient Delphi forum. Rigorous and persistent searching may yield results.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top