Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close window 1

Status
Not open for further replies.

Phailak

Programmer
Apr 10, 2001
142
CA
Hail,

I need to stop my agents from opening more than 1 session of an application (which isn't mine and which I don't have the code for). I use the API SendMessage to close the window, but I can never be sure I close the right one. I want to close the old one and not the one that just opened. (The reason they have more than one session open is that they close the app through the X and it stays in memory but disappears from the screen, this is the instance I want to get rid of.)
How should I go about this, for now I simply use a timer with find window, if more than one instance I close them all which can be annoying for users.

Phailak
 
Maybe if you try using EnumWindows?
Then you could try to find your window by the caption (or other properties that are more useful).

To get all windows (including the invisible ones):

Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

To get the caption of a particular window:

Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Remedy
 
Hail,

Thanx for the quick response,
I do use the method you mentionned, problem is, same caption on both windows so I still can't differentiate between the two.

Phailak
 
I wasn't aware that I responded so quickly, but you're welcome..

You could use GetFocus to get a handle to the window that has the focus, and skip that one while closing them all.
That's quite a dirty way, because you never know if another window (from a different application) will pop up..
I'll let you know if I come up with something better.

Declare Function GetFocus Lib "user32.dll" () As Long

Remedy
 
Hail,

Mmmm, good suggestion, but they do have more than one app opened so I'd have to worry about them switching to another, then trying to open a new instance of the app I need closing on.
If you find a way, I'd appreciate, for now I guess they'll have to suffer ;o)

Phailak
 
You could store the handles and compare the new to the old -- when they open the first app, store that handle, then if they open another instance compare that handle to the old, and kill the old one.

Or you could change the shortcut so that when they click the icon, it first checks for other instances of itself, and if they exist, it closes them....
 
I think hardkor's final suggestion is the one to go for: write yourself a launcher application which checks for already running instances of the actual program and closes them, and only then launches the new instance.
 
Hail,

So just to make sure I got this right, close everything and then relaunch?
I guess that could work.
Thanx again

Phailak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top