By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

How can I judge an application is runing ??

P: n/a
Hi all

I want to judge an application is runing or not when another application
start .
If the application is running , get focus.
I use the Pinvoke to invoke the FindWindow function ,I declared FindWindow
function
in assembly WindowsApplication3. If the WindowsApplication2 is running ,then
WindowsApplication2 getfocus . WindowsApplication2 has a form named Form1

[DllImport("User32",EntryPoint="FindWindow")]
static extern IntPtr FindWindow(string className, string windowName);

Invoke:

IntPtr hWnd = FindWindow("WindowsApplication2.exe", "Form1");

But I always get IntPtr.Zero value .
Someone can tell me how to assign to two parameter of FindWindowsFunction ?

thanks in advance
Yadong Zhao

Dec 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
hi,

I did exactly once what you are lookign for (although in VB.NET):
"BringExtraToFront" is the method that mustb e calles, in "ProcessExtra" it
returns the process with the name "EXTRA": to get the process-name of your
WindowsAppllication2.exe: look in the Task Manager.

hope this helps,

Pieter
Public Sub BringExtraToFront()
Try
Dim prc As Process
Dim clsProc As New clsProcesses
prc = clsProc.ProcessExtra
If prc Is Nothing Then
Exit Sub
End If
Dim handle As IntPtr = prc.MainWindowHandle
Dim Win32Help As New Win32Helper
If Not IntPtr.Zero.Equals(handle) Then
Win32Helper.SetToForGround(handle)
End If
Catch ex As Exception
ErrorMessage(Nothing, ex, "BringExtraToFront Exception",
"BringExtraToFront")
End Try
End Sub
Public Shared Function ProcessExtra() As Process
' Durchlaufen aller Prozesse mit gleichem Namen.
Dim p As Process
For Each p In Process.GetProcessesByName("EXTRA")
'If UCase(Left(p.MainWindowTitle, 4)) = SessionName Then
'->optional: for the Windows.Caption in case you can have more than once the
same process open...
Return p
Exit Function
'End If
Next p
Return Nothing
End Function
Public NotInheritable Class Win32Helper
Public Const GW_HWNDPREV = 3
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function SetForegroundWindow(ByVal handle As IntPtr) As
Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function ShowWindow(ByVal handle As IntPtr, ByVal nCmd As
Int32) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function IsIconic(ByVal hWnd As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function IsZoomed(ByVal hWnd As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="SetForegroundWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall,
_
CharSet:=Runtime.InteropServices.CharSet.Unicode,
SetLastError:=True)> _
Public Shared Function SetForegroundWindow(ByVal handle As Integer)
As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function ShowWindow(ByVal handle As Integer, ByVal nCmd
As Int32) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32. dll", _
EntryPoint:="IsIconic", _
CallingConvention:=Runtime.InteropServices.Calling Convention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Private Shared Function IsIconic(ByVal hWnd As Integer) As Boolean
' Leave function empty
End Function
Public Shared Sub SetToForGround(ByVal hwnd As IntPtr)
Dim strStatus As String
'Dim hwnd As IntPtr
'hwnd = p.MainWindowHandle

If IntPtr.Zero.Equals(hwnd) Then
strStatus = ""
Exit Sub
End If
If IsIconic(hwnd) Then
strStatus = "MIN"
End If
'If IsZoomed(hwnd) Then
' IsNormal = True
'End If
'If IsIconic(hwnd) And IsZoomed(hwnd) Then
' IsNormal = True
'End If

If strStatus = "MIN" Then
'mimized
ShowWindow(hwnd, SW_RESTORE)
SetForegroundWindow(hwnd)
Else
'maximzed or restored
SetForegroundWindow(hwnd)
End If
End Sub

Public Shared Sub SetToForGround(ByVal hwnd As Integer)
Dim strStatus As String
'Dim hwnd As IntPtr
'hwnd = p.MainWindowHandle

If IntPtr.Zero.Equals(hwnd) Then
strStatus = ""
Exit Sub
End If
If IsIconic(hwnd) Then
strStatus = "MIN"
End If
'If IsZoomed(hwnd) Then
' IsNormal = True
'End If
'If IsIconic(hwnd) And IsZoomed(hwnd) Then
' IsNormal = True
'End If

If strStatus = "MIN" Then
'mimized
ShowWindow(hwnd, SW_RESTORE)
SetForegroundWindow(hwnd)
Else
'maximzed or restored
SetForegroundWindow(hwnd)
End If
End Sub

End Class ' End Win32Helper
Dec 23 '05 #2

P: n/a
Hi,
IMO this is not the best option, what if it happens that you have another
program with the same windows title?
If you control the two applications you could create a mutex (this
technique is used to assure only one instance of an application running)

Also a problem is if the first app create a child windows , you would have
to check for the names of ALL the possible windows of the app

In any case , here is hte code I'm using for that:

IntPtr mainwin = FindWindow(null, "New Order");
if(! mainwin.Equals( IntPtr.Zero ) )
{
SetForegroundWindow(mainwin);
Application.Exit();
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"dahuzizyd" <da********@hotmail.com> wrote in message
news:el**************@tk2msftngp13.phx.gbl...
Hi all

I want to judge an application is runing or not when another application
start .
If the application is running , get focus.
I use the Pinvoke to invoke the FindWindow function ,I declared
FindWindow function
in assembly WindowsApplication3. If the WindowsApplication2 is running
,then WindowsApplication2 getfocus . WindowsApplication2 has a form named
Form1

[DllImport("User32",EntryPoint="FindWindow")]
static extern IntPtr FindWindow(string className, string windowName);

Invoke:

IntPtr hWnd = FindWindow("WindowsApplication2.exe", "Form1");

But I always get IntPtr.Zero value .
Someone can tell me how to assign to two parameter of FindWindowsFunction
?

thanks in advance
Yadong Zhao

Dec 23 '05 #3

P: n/a
My problem had solved . Too many thanks for your help !

Thanks and Regards
Yadong Zhao

Dec 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.