Solved

Get last window handle?

Posted on 2004-04-14
13
949 Views
Last Modified: 2010-08-05
HI :D

I have a little program that has some shortcuts to windows apps:
I need function to Get the handle of the last program...


Can Somebody Help? please :P
0
Comment
Question by:lnishimura
[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
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 10830909
Could you please specify what you mean with "the last program"?
0
 

Author Comment

by:lnishimura
ID: 10830929
All Exe Program Software that creates a window has a handle right?
Ussually an integer... i need to know the handle of the last one...


Got it?


here goes a sample that lists the handles but i cant get the  last one opened


procedure LIstApp;
const
MAX_WINDOW = 16364;
var
 I     : Integer
Caption: Array[0..255]Of Char;
//Lista  : TStringList;
begin
//Lista:=TStringList.Create;
//Lista.Sorted:=True;
For I:=1 To MAX_WINDOW Do
Begin
If((GetWindowText(I,Caption,255)<>0)And (GetWindowLong(I,GWL_EXSTYLE)<>0)And
(GetWindowLong(I,GWL_HWNDPARENT)=0)And (GetWindowLong(I,GWL_HINSTANCE)<>0))Then
If(IsWindowVisible(I))Then begin

 form1.Lista.Items.Add(inttostr(i));
 end;

end;
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 10831047
You should use EnumWindows instead of looping through possible window handles.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:lnishimura
ID: 10831096
Any Sample or function i can use?
0
 
LVL 3

Expert Comment

by:MarcG
ID: 10831207

For EnumWindows you need to write a function that is called when a window is found. Like this:

function EnumWindowsProc(hWnd: HWND; lParam: LParam): Bool;
var
  processID, tmpID: DWord;
  tmpCaption: array[0..99] of Char;
  tmp: String;
begin
  Result:= True;
  GetWindowThreadProcessID(hWnd, @processID);
  //*** do not get oneself
  if processID <> GetCurrentProcessID then
  begin
    //*** exclude taskbar, desktop and own tray menu
    if (hWnd <> FindWindow('Shell_TrayWnd', nil)) and
      (hWnd <> FindWindow('Progman', nil)) and
      (hWnd <> Findwindow(nil, 'frmMain.mnuTray')) then
    begin
      if isWindowVisible(hWnd) then
      begin
         { Do something }
      end;
    end;
  end;
end;

Then call EnumWindows like this:

EnumWindows(@EnumWindowsProc, 0);

The EnumWindowsProc above is from a prog of me, so the code inside is just an example.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 10831382
Pleae make that EnumWindowsProc to be "stdcall" !!
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10832340

   You execute these windows Apps from your code.... right ? If so - just when you execute with ShellExecute for example take the result somewhere. It returns Handle ...
0
 

Author Comment

by:lnishimura
ID: 10833144
i Know i can put some thing after ShellExecute...and MarcG is very nice to use too....However
what can i put after shellExecute that returns me the las handle?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10833317

    I don't understand you .... ShellExecute is function - it has result :

  HINSTANCE ShellExecute(
    HWND hwnd,      // handle to parent window
    LPCTSTR lpOperation,      // pointer to string that specifies operation to perform
    LPCTSTR lpFile,      // pointer to filename or folder name string
    LPCTSTR lpParameters,      // pointer to string that specifies executable-file parameters
    LPCTSTR lpDirectory,      // pointer to string that specifies default directory
    INT nShowCmd       // whether file is shown when opened
  );

Return Values

If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.
0
 

Author Comment

by:lnishimura
ID: 10833348
hmn
i will test it

let you know...does it returns the Handle of the soft i opened rght?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10833412

   "instance handle of the application that was run"

   you can find the declaration of HInst type, which is in fact THandle... Enjoy :)
0
 

Author Comment

by:lnishimura
ID: 10833465
dude.... i'm too much stupid to undersntand your answer(still newbie in delphi)...

Can u post some code that really helps me a lot Thanx :D
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 500 total points
ID: 10833797

    Sorry, my mistake, MSDN says that with ShellExecute you receive number > 32 if success, else - <= 32. You can not do anything with this.

   The methods you need to get Handle are CreateProcess and OpenProcess. Here is some source you can look at :
   http://www.delphicorner.f9.co.uk/articles/wapi4.htm

   Then use OpenProcess and you will have the Handle.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

624 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