I was reading this month's FP Advisor Answers section, specifically "Shut Down Abandoned Applications" and there they recommened a method for finding opened programs and shutting them down.
Our problem is many of our users seem to have a problem double clicking, and tend to up up multiple versions of the same program at a time. This messes up somethings so we would like to not have this happen. So I want to do a check whenever this program, tester.exe, is open. If it is the only one running, do nothing. Otherwise, close all the extra programs running and activate&maximize the original(first program).
The article got me thinking about using GetWindows to cycle through the DeskTop's child windows, count up the number of tester.exe's that are running and then close down all those extra tester.exe's and activate the first one.
Problem 1) However, it seems like GetWindows doesn't go in temporal order.
Problem 2) Using PostMessage to close a program, seems to work okay. However, each time I PostMessage(window_handle, WM_CLOSE, 0, 0) a dialogue box pops up asking me if I really want to close tester.exe. If i interact with that dialogue box, PostMessage will stop closing any future programs. This seems like a really weird problem.
Problem 3) I find the "ClassName" for Tester.exe by sing the win32api function: FindWindow(NULL, "Tester") to get the handle for the window and then call GetClassName(Handle, @textbuffer, len(textbuffer). This works find and I can get the class name for the tester.exe (tester6c000000). However, when I scan through the children windows of DeskTop (using GetWindow) it sometimes shows MORE programs running then are actually running. IF I have manually opened 6 Tester.exe's it might show 8 or 9 being open. So when I start closing extra texter.exe's that are open, it closes all of them instead of just 5!
Problem 4) How do I maximize the original Tester.exe from another program (the second invocation of tester.exe for instance)? I tried using PostMessage(window_handle, WM_ACTIVATEAPP, 0 , 0) but that didn't work [I get an opperand missing error].
I am just floating in a sea of the blind, here. The documentatino for the win32API's isn't very good, and the article didn't provide much help either.
thanks
matt
ps sample code below
LPARAMETERS Data_Location
LOCAL lnH_Desktop, lnH_Window, lnH_LBOrig, lcClass, lnLen, lnCount
Data_Location = IIF(PARAMETERS() < 1, '', Data_Location)
CLOSE ALL
SET TALK OFF
SET CONSOLE OFF
SET EXCLUSIVE OFF
SET DATE TO 'MDY'
SET CENTURY ON
#INCLUDE "\INCLUDES\LOGBOOK.H"
#DEFINE GW_CHILD 5
#DEFINE GW_HWNDNEXT 2
#DEFINE WM_CLOSE 0x10
#DEFINE WM_ACTIVATEAPP = 0x1D
lnCount = 0
DECLARE LONG FindWindow IN WIN32API;
STRING lpClassName, STRING lpWindowName
DECLARE LONG GetDesktopWindow IN WIN32API
DECLARE LONG GetWindow IN WIN32API LONG HWND, LONG wCmd
DECLARE LONG GetClassName IN WIN32API;
LONG HWND, STRING lpClassName, LONG nMaxCount
DECLARE LONG PostMessage IN WIN32API;
LONG HWND, LONG WMsg, LONG wParam, LONG LPARAM
ACTIVATE WINDOW DEBUG
SET STEP ON
lnH_Desktop = GetDesktopWindow()
lnH_Window = GetWindow(lnH_Desktop, GW_CHILD)
DO WHILE lnH_Window <> 0
lcClass = SPACE(256)
lnLen = GetClassName(lnH_Window, @lcClass, LEN(lcClass))
lcClass = LEFT(lcClass, lnLen)
IF 'LB_4006c000000' == lcClass
lnCount = lnCount + 1
IF lnCount == 1
lnH_LBOrig = lnH_Window
ELSE
PostMessage(lnH_Window, WM_CLOSE, 0, 0)
ENDIF
ENDIF
lnH_Window = GetWindow(lnH_Window, GW_HWNDNEXT)
ENDDO
IF lnH_LBOrig > 0
ENDIF
RETURN