Solved

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

Posted on 2004-04-02
5
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

691 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