I have a window with two text fields inside it.
How can I get the handles of both text fields using WinAPI calls?
Note: Both text boxes belong to a different application (I make the WinAPI calls in application A and the text boxes are located in application B).
Update 1:
I get Invalid window handle message when invoking GetClassName.
I suppose that something is wrong with my declaration of the callback function.
EnumChildWindows is invoked from one of the methods of TMyClass like this:
EnumChildWindows(handle, @TMyClass.CBList, 0);
Here’s the code of the callback function.
function TMyClass.CBList(Win: THandle; lp: LPARAM): Boolean; stdcall;
var
ClassName:array [1..1024] of Char;
begin
GetClassName(Win, PChar(@ClassName), 1024);
OutputDebugString(PChar('SysErrorMessage(GetLastError): '));
result := true;
end;
Use a tool like Spy++ to understand the structure of the target app, and find out the precise window class names that it uses.
Your questions in the comments about how to call
GetClassNamegot me thinking. If you are using XE3, you could write a simple type record helper forHWNDto make it syntactically cleaner to get hold of the class name:And then you can write
hwnd.ClassNameto obtain the window class name. Of course, if you are not using XE3 you can do it like this:Note that I am using a buffer length of 256 since window class name lengths are limited to be no longer than that.
Regarding the code in the update, you must not use an instance method for the callback. The callback must be declared like this:
This is made clear in the documentation. Unfortunately the declaration of
EnumChildWindowsinWindows.pascompletely abandons type safety of the callback function. So you have to get it right without help from the compiler.Note also that
HWNDandTHandleare not the same thing. Don’t mix them up.