Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

12 Experts available now in Live!

Get 1:1 Help Now