Solved

Get main forms Classname starting from only the path to the exe ?

Posted on 2004-04-02
5
397 Views
Last Modified: 2010-04-05
Hi,

i wonder if it's possible to get the classname of the window when u only have the path to the exe to start with ?

I was thinking like something like letting it run hidden, get  it's classname from the main form of the application (the exe).

Only problem is i have no clue how to even start with this, what functions i need to be looking into etc.
0
Comment
Question by:joyrider
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10746231
The className of the window?   TForm?

Do you want the Window Caption?

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10746242
Normally each application has a 'unique' caption or at least a unique part of the caption text.

If you know all of the applications at compile-time you can use WinSight (comes with Delphi) to figure out the main windows classname for each app and then use the FindWindow API function with the classname to find the main window.

  Var
    wnd: HWND;
  Begin
    wnd:= FindWindow('ClassName', nil );
    //will be closed
    SendMEssage( wnd, WM_CLOSE, 0, 0 );
  End;

Note that you need to make sure the main form class for your application has a reasonably unique name, so FindWindow finds the correct window.

If you only know the Caption-Text you must use GETWINDOWTEXT for each application on system.

Shane
0
 

Author Comment

by:joyrider
ID: 10746347
no it's not something like that i wan't i actually don't know the classname to start with and need to be able to find it out for any application that is selected by the program that has a main window that shows normally.

so i only know the path to exe like 'c:\games\ut2004\ut2004.exe' and i wish to find out the classname of the window from that app. I would definatly need to start the program myself in some way but the problem lies in the fact that i have no clue how i can find out the window handle of the selected program that will run from only the path ?
0
 
LVL 17

Accepted Solution

by:
geobul earned 300 total points
ID: 10757711
Hi,

You first start your app of interest using CreateProcess, ShellExecuteEx, ShellExecute or even WinExec:
ShellExecute(0, 'open', PChar('c:\games\ut2004\ut2004.exe'), nil, nil, SW_HIDE);

Then try the following code for getting the classname of the main window (Note: won't work on Win NT 4):
enter:
ut2004.exe
in the Edit1 and press the button.

After you've finished you may close the app sending WM_CLOSE to the window handle (ExeWindow here):
SendMessage(ExeWindow, WM_CLOSE, 0, 0);

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ExeName : string;
  ExeWindow : HWND;

implementation

{$R *.DFM}

uses  tlhelp32;

function GetWindowExeName(Handle: THandle): String;
var
  PE: TProcessEntry32;
  Snap: THandle;
  ProcessId: cardinal;
begin
  result := '';
  GetWindowThreadProcessId(Handle,@ProcessId);
  Snap:= CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  try
    if Snap <> 0 then begin
      if Process32First(Snap, PE) then begin
        if PE.th32ProcessID = ProcessId then begin
          Result:= String(PE.szExeFile)
        end else while Process32Next(Snap, PE) do begin
          if PE.th32ProcessID = ProcessId then begin
            Result:= String(PE.szExeFile);
            break;
          end;
        end;
      end;
    end;
  finally
    CloseHandle(Snap);
  end;
end;

function CompExeName(hWnd: HWND) : boolean;
var
  ModuleFileName: string;
begin
  result := false;
  ModuleFileName := GetWindowExeName(hWnd);
  // without ExtractFileName if the full path has to be entered
  if (UpperCase(ExtractFileName(ModuleFileName)) = ExeName) then begin
    result := true;
    ExeWindow := hWnd;
  end;
end;

function EnumerateWindows(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin
  Result := true;
  if CompExeName(hWnd) then result := false;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  TheClassName: array[0..255] of char;
  ErrorCode: Integer;
begin
  ExeName := UpperCase(Edit1.Text);
  ExeWindow := 0;
  EnumWindows(@EnumerateWindows, 0);
  if ExeWindow <> 0 then begin
    ShowMessage('Found');
    ErrorCode := GetClassName(ExeWindow, TheClassName, 255);
    if ErrorCode <> 0 then begin
      ShowMessage('ClassName is:'+TheClassName);
    end;
  end else begin
    ShowMessage('Not found');
  end;
end;

end.

Regards, Geo
0
 

Author Comment

by:joyrider
ID: 11784277
thanks, and sorry for the long delay i kinda forgot about this question, just found out and test it, it works in most cases, so i can play a bit with it to set it up the way i want to.

thanks again !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now