[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-04-02
5
Medium Priority
?
412 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 1200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

650 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