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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only allowing 1 program to run at a time

Status
Not open for further replies.

progolf069

Programmer
Jun 13, 2001
37
US
I was wondering if Visual Basic supports any type of program monitoring. The program that I am developing is a main menu system that shells out (executes or calls) to other programs. What I am wanting to do is prevent two instances of the same program from executing.

Example: One of the menu options is to run a network terminal emulator program. If the user already has a terminal windows running, I do not want the users to be able to open another terminal window. If the program is already running on the computer (in the background or visable) I want to user to be prompted with an error message stating: "That Program is Already Running. Please Close it Before Continuing"

Any help would be greatly appreciated! Thanks for your Contribution!

Ian Wickline
K's Merchandise, Inc.
 
I have found on an MS page a function named FindWindowLike (bellow).
You could find hwnds with this using the programs caption, or class or...
When you starts an instance of a program get its hwnd, and the class (perhaps GetClassName API funct., or if it a concrete program monitoring, use the program's class name constant), and close program when it's running already.

i hope i made it clear
and it helps you
ide

Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" _
(ByVal hWnd As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindowLW Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) _
As Long

Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's text.
'
'ClassName
' - The pattern used with the Like operator to compare window's class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'
'----------------------------------------------------------------------
'Remove this next line to use the strong-typed declarations
Function FindWindowLike(hWndArray() As Long, ByVal hWndStart As Long, _
WindowText As String, Classname As String, ID) As Long
On Error Resume Next
Dim hWnd As Long
Dim r As Long
' Hold the level of recursion:
Static level As Long
' Hold the number of matching windows:
Static iFound As Long

Dim sWindowText As String
Dim sClassName As String
Dim sID
' Initialize if necessary:
If level = 0 Then
iFound = 0
ReDim hWndArray(0 To 0)
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hWnd = GetWindow(hWndStart, GW_CHILD)
Do Until hWnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWndArray(), hWnd, WindowText, Classname, ID)
' Get the window text and class name:
sWindowText = Space(255)
r = GetWindowText(hWnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassName = Space(255)
r = GetClassName(hWnd, sClassName, 255)
sClassName = Left(sClassName, r)
' If window is a child get the ID:
If GetParent(hWnd) <> 0 Then
r = GetWindowLW(hWnd, GWL_ID)
sID = CLng(&quot;&H&quot; & Hex(r))
Else
sID = Null
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText And sClassName Like Classname Then
If IsNull(ID) Then
' If find a match, increment counter and
' add handle to array:
iFound = iFound + 1
ReDim Preserve hWndArray(0 To iFound)
hWndArray(iFound) = hWnd
ElseIf Not IsNull(sID) Then
If CLng(sID) = CLng(ID) Then
' If find a match increment counter and
' add handle to array:
iFound = iFound + 1
ReDim Preserve hWndArray(0 To iFound)
hWndArray(iFound) = hWnd
End If
End If
Debug.Print &quot;Window Found: &quot;
Debug.Print &quot; Window Text : &quot; & sWindowText
Debug.Print &quot; Window Class : &quot; & sClassName
Debug.Print &quot; Window Handle: &quot; & CStr(hWnd)
End If
' Get next child window:
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop
' Decrement recursion counter:
level = level - 1
' Return the number of windows found:
FindWindowLike = iFound
End Function

'****************************************
'this calling disables close buttons (X) of all running windows
Sub main()
Dim hWnds() As Long
Const SC_CLOSE As Long = &HF060
r = FindWindowLike(hWnds(), 0, &quot;*&quot;, &quot;*&quot;, Null)
'r = FindWindowLike(hWnds(), 0, &quot;*Excel*&quot;, &quot;*&quot;, Null)
For i = 0 To r
hMenu = GetSystemMenu(hWnds(i), 0)
DeleteMenu hMenu, SC_CLOSE, 0& 'Disable _ application close button
Next i
End Sub
 
You can also get a list of all processes running to detect the application by name. Refer to faq222-61
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top