Solved

Handle of my appilication?

Posted on 2007-04-02
6
164 Views
Last Modified: 2010-04-23
Hello

I want to search for mutliple instances of my application.
So I am searching through all processes and if the exename is the same as my app-exename, I add currentinstances +1
if currentinstances > 1 then I want to kill all old instances

so I loop again through all processes and then if the exename is the same as my app exe-name, I want to compare the handles, to check if this instance is the actual running instance.

So I tried:

                        Dim prochandle As String = oprocess.Handle.ToString
                        Dim myhandle As String = Me.Handle.ToString
                        If prochandle <> myhandle Then
                            oprocess.Kill()
                        End If

But this did not work, Me.Handle seems to be different from the Process.Handle of my application...

What is wrong?

Thanks

Sven
0
Comment
Question by:Ruttensoft
6 Comments
 
LVL 2

Expert Comment

by:MasterThai
ID: 18836552
This should be of some help.
http://www.developerfusion.co.uk/show/134/
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18836695
this will return you the handle

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
returns long handle >> FindWindow(, "WINDOW TITLE")
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18836962
"So I am searching through all processes and if the exename is the same as my app-exename"

You are doing more work than you need too...

Use Process.GetProcessesByName():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassgetprocessesbynametopic1.asp

Something like...

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim appName = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
        Dim ps() As Process = Process.GetProcessesByName(appName)        
        While ps.Length > 1
            ps(0).Kill() ' or ps(0).CloseMainWindow()
            ps = Process.GetProcessesByName(appName)        
        Wend
    End Sub

End Class

You sure you want to kill OLD instances?

What version VB.Net are you working in?

Do you need to pass info to the old and/or new instances?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now