Solved

Handle of my appilication?

Posted on 2007-04-02
6
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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