We help IT Professionals succeed at work.

Need to get window caption when I only have the instance from GetModuleHandle

Medium Priority
489 Views
Last Modified: 2013-12-03
I have a DLL that I'm writing in Delphi and I need to be able to get the main window caption of the calling application.  I have no issue identifying the application using GetModuleFileName() and then using GetModuleHandle() to get the instance but I can't work out how to get any further.  Any assistance greatly appreciated :-)
Comment
Watch Question

MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
First you'll have to find it's window, using EnumWindows(),
after that you can get the caption with GetWindowText().

Need any help with that?

Author

Commented:
Yes... lots probably!  I used Delphi quite a bit a few years back (D5) but haven't touched it for ages.

Author

Commented:
Haven't used EE for quite a while either ;-) ...forgot to ask if you could include a code snippet?
MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
Here's a function to find the window belonging to a process id:
function GetHwnd(ProcessID: DWORD): hwnd;
var WindowProcesID: DWORD;
begin
 result := GetWindow(GetDesktopWindow(), GW_CHILD);
 
 while result <> 0 do
 begin
  if IsWindowVisible(result) then
  begin
   GetWindowThreadProcessId(result, WindowProcesID);
   if ProcessID = WindowProcesID then
    break;
  end;
 
  result := GetWindow(result, GW_HWNDNEXT);
 end;
end;

Open in new window

MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
oops forgot: You can get your own processid by calling GetCurrentProcessID().

will post the rest soon
Sr. Software Engineer
CERTIFIED EXPERT
Commented:
Here it is in total:
function GetHwnd(ProcessID: DWORD): hwnd;
var WindowProcesID: DWORD;
begin
 result := GetWindow(GetDesktopWindow(), GW_CHILD);
 
 while result <> 0 do
 begin
  if IsWindowVisible(result) then
  begin
   GetWindowThreadProcessId(result, WindowProcesID);
   if ProcessID = WindowProcesID then
    break;
  end;
 
  result := GetWindow(result, GW_HWNDNEXT);
 end;
end;
 
procedure TForm2.Button1Click(Sender: TObject);
var WinHandle: hwnd;
    WindowText: string;
begin
 WinHandle := GetHwnd(GetCurrentProcessId());
 
 if WinHandle <> 0 then
 begin
  SetLength(WindowText, 255); // assume some length
  SetLength(WindowText, GetWindowText(WinHandle, @WindowText[1], Length(WindowText)));
  Label1.Caption := WindowText;
 end;
end;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Awesome! - works like a charm!

Thanks for all your help :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.