2

I am using the below code to identify instance of the application also if we need to check that which user is using this application what will be the code for it?

Function PrevInstance() As Boolean
    If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
        Return True
    Else
        Return False
    End If
End Function

My requirement is if same user tries to open the application then it should display pop up message like "application already opened".

Please advice... abhay

abhayk
  • 287
  • 1
  • 2
  • 15
  • Just a remark ... 'if something = something else then return true else return false' is somewhat more complicated than ... using your example simply 'Return Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0' – Florian Schmidinger Mar 17 '15 at 10:58
  • possible duplicate: http://stackoverflow.com/questions/11055147/how-do-i-get-list-of-process-names-running-in-vb-net – Florian Schmidinger Mar 17 '15 at 11:00
  • I have done something in vbscript but you removed this tag so if you want to take a look at this code just add the tag Vbscript to add my code for you for testing if you want of course. – Hackoo Mar 17 '15 at 12:30
  • Use a Mutex combining an ID and the launching username; http://stackoverflow.com/a/2415639/246342 – Alex K. Mar 17 '15 at 14:00

2 Answers2

1

Thanks a lot guys.... solution on my problem as follows:

> Function PrevInstance() As Boolean
>         If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName))
> > 0 Then
>                   Dim CurUser As Boolean = GetProcessOwner(Diagnostics.Process.GetCurrentProcess.ProcessName)
>             Return CurUser
>         Else
>             Return False
>         End If
>     End Function
Function GetProcessOwner(ByVal ProcessName As String) As Boolean
    Dim boolVal As Boolean
    Dim CurUserName As String
    Dim CountInstance As Integer
    CountInstance = 0
    CurUserName = System.Environment.UserName
    Dim selectQuery As SelectQuery = New SelectQuery("Select * from Win32_Process Where Name = '" + ProcessName + ".exe' ")
    Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(selectQuery)
    Dim y As System.Management.ManagementObjectCollection
    y = searcher.Get
    For Each proc As ManagementObject In y
        Dim s(1) As String
        proc.InvokeMethod("GetOwner", CType(s, Object()))
        Dim n As String = proc("Name").ToString()
        If n = ProcessName & ".exe" Then
            If s(0) = CurUserName Then
                CountInstance = CountInstance + 1
                If CountInstance > 1 Then
                    boolVal = True
                End If
            End If
        End If
    Next
    Return boolVal
End Function

I have called PrevInstance() in my Form_Load() and its working perfectly.

abhayk
  • 287
  • 1
  • 2
  • 15
0

PrevInstance Property

Returns a value indicating whether a previous instance of an application is already running.

Syntax

object.PrevInstance

The object placeholder represents an object expression that evaluates to an object in the Applies To list.

Remarks

You can use this property in a Load event procedure to specify whether a user is already running an instance of an application. Depending on the application, you might want only one instance running in the Microsoft Windows operating environment at a time.

Note Since a computer running Windows NT can support multiple desktops, if you use a component designed to work with distributed COM, it can result in the following scenario:

A client program in a user desktop requests one of the objects the component provides. Because the component is physically located on the same machine, the component is started in the user desktop.

Subsequently, a client program on another computer uses distributed COM to request one of the objects the component provides. A second instance of the component is started, in a system desktop. There are now two instances of the component running on the same NT computer, in different desktops.

This scenario is not a problem unless the author of the component has placed a test for App.PrevInstance in the startup code for the component to prevent multiple copies of the component from running on the same computer. In this case, the remote component creation will fail.


Send feedback to MSDN.Look here for MSDN Online resources.


Session

The above tells you if it's already running. This tells you what session. An interactive user is always session1 on Vista and later. 0 for XP and earlier.

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
        msgbox objitem.name & " PID=" & objItem.ProcessID & " SessionID=" & objitem.sessionid
Next

PS

The rules for single instance programs is just before you exit you switch windows to the previous instance.

PPS

Due to problems introduced by 32 bit computing previnstance (Win32's one rather than VB's) becomes less meaningful. The common way to do this now is to open and lock a file on startup (Windows also has memory constructs you can use such as mailslots, pipes, etc). If a program can't lock then another is already running.

Serenity
  • 34
  • 3