Solved

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

Posted on 2004-04-02
5
398 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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