473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I judge an application is runing ??

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
3 1655
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
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
My problem had solved . Too many thanks for your help !

Thanks and Regards
Yadong Zhao

Dec 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jassim Rahma | last post by:
Hi, I have developed a Pocket PC application and I want to know how to terminate it? I used Aplication.Exit() but the program will still be running in the process list!! Many Thanks,...
0
by: Ravikanth[MVP] | last post by:
Hi Check out this URL: http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnaspp/html/converttoaspnet.asp? frame=true --Ravikanth
5
by: Bernard Bourée | last post by:
How to obtain the runing application path ? -- Bernard Bourée bernard@bouree.net
7
by: Alice Wong | last post by:
I am setting up my Web ASP.net application to connect to Sql server using windows authentication. I set up IIS to have integrated windows authenication and sql to allow Windows authentication....
5
by: Ravi Ambros Wallau | last post by:
Dear friends: This is my third question on this news in three days... Sorry for this spam behavior :-) I've a lot of problems on "first page load" after some change on web.config or even in the...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
2
by: Conbine Guard | last post by:
Hi all I have build an application with a windows.In the application i started a new thread when i click the exit button the windows disappeared but the taskmgr shows the application still runing...
1
by: Cylix | last post by:
How to terminnate the application which contain another thread runing a looping of tcplisten? When I just execute the application.exit, the thread doesn't stop. How can I terminate the appication...
4
by: oliv | last post by:
Hi, new to .NET, I need some unique var among the whole application in during the whole lifecycle of the webapp. This var is a Boolean : at start time of the webapp, I want this var to be set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.