Link to home
Start Free TrialLog in
Avatar of aburgoyne
aburgoyne

asked on

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

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 :-)
Avatar of MerijnB
MerijnB
Flag of Netherlands image

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?
Avatar of aburgoyne
aburgoyne

ASKER

Yes... lots probably!  I used Delphi quite a bit a few years back (D5) but haven't touched it for ages.
Haven't used EE for quite a while either ;-) ...forgot to ask if you could include a code snippet?
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

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

will post the rest soon
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome! - works like a charm!

Thanks for all your help :-)