Chris Douce
2/23/2011 6:57:00 AM
On 22/02/2011 23:27, BeeJ wrote:
> For example, I want to open the Windows Microphone control.
> So I use
>
> Shell "sndvol32.exe /rec", vbNormalFocus
>
> But if I click the button again I get another instance of the same.
> Now, I know that I could search and see if there is an app with that
> caption already open, but ...
> The caption is
> R...
> Not too helpful.
> So rather than searching for a caption, what is a better controlled way
> of either launching an app and getting something in return that will let
> me test it to see if it is already open if I try again or some other
> method? This cannot be a modal op since I need to do other things while
> the app is open. Not sure what to search for.
>
>
Hi BeeJ,
That's what i use (tested in project with 1 form and 1 bas moduile)
'*********************************
'In a Bas Module
Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal
dwProcessId _
As Long) As Long
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ACTIVE = &H103
Public Declare Function GetExitCodeProcess Lib "kernel32.dll" _
(ByVal hProcess As Long, lpExitCode As Long) _
As Long
Function is_prog_actief(h_ID, Optional hproc As Long) As Long
Dim exit_code As Long
'Bepalen van de Processhandle door middel van de ID van de Shell
If hproc = 0 Then hproc = OpenProcess(PROCESS_QUERY_INFORMATION,
False, h_ID)
'checken of het programma nog actif is
zz = GetExitCodeProcess(hproc, exit_code)
is_prog_actief = exit_code
End Function
'**********************************
'**************************
'In form with 1 button
Private Sub Command1_Click()
Static hnote As Long
' Instance handle from Shell function.
note = is_prog_actief(hnote)
If note = STILL_ACTIVE Then
AppActivate hnote
Else
hnote = Shell("notepad.exe ", 1)
End If
End Sub
'*******************************************
Kind regards
Chris