Results 1 to 9 of 9

Thread: Check for a process per user VB

  1. #1
    Senior Member mungyun's Avatar
    Join Date
    Apr 2004
    Location
    Illinois
    Posts
    172

    Check for a process per user VB

    I posted this on several vb forums with no avail so i figure i would post here because i have always gotten better help here than anywhere else.

    I have an application on a terminal server that runs for each user as upon login and is intended to always be running for them. The problem i found out was that if they didnt logout and just disconnected then reconnected, another instance of this app would run as well as the currently running one, so on and so forth. I then added this simple code:

    Code:
    Dim Proc As System.Diagnostics.Process
    If proc.Length > 1 And Environment.UserName = Employee.NTUserName Then
        MsgBox("Another instance of TimeClock is already running. Terminating.")
        End
    End If
    That does what i want but it doesnt care about multiple users using the app. It looks and if more than one instance is running for all users then it will end so only one user can use it at any time. I need to check if more than one instance is running per user.

    Another thing i tried is this:

    Code:
    Dim processList As System.Diagnostics.Process()
    Dim thisProc As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
    Dim thisSessionID As Long = thisProc.SessionId
    
    processList = System.Diagnostics.Process.GetProcesses()
    Dim Proc As System.Diagnostics.Process
    
    Dim count As Integer = 0
    
    For Each Proc In processList
         If Proc.SessionId = thisSessionID And Proc.ProcessName = "LSTimeClock.exe" Then
         count += 1
         End If
    
         If count > 1 Then
              MsgBox("There is another instance of LSTimeClock Running")
              End
         End If
    This code looks like it should do what i want but it doesnt. I fairly recently got thrown into vb programming and for terminal services for that matter so there are lots of extra things i didnt expect like this.

    Any help would rock.

    Thx
    Mung
    I believe in making the world safe for our children, but not our children’s children, because I don’t think children should be having sex. -- Jack Handey

  2. #2
    Senior Member Aardpsymon's Avatar
    Join Date
    Feb 2007
    Location
    St Annes (aaaa!)
    Posts
    434
    Code:
    If Proc.SessionId = thisSessionID And Proc.ProcessName = "LSTimeClock.exe"
    Well, the error is clearly in that line. Its not the old "==" thing is it? number of times I have forgotten to put ==...(not done vb in ages, so it might not be that)

    Another solution might be to put a file in the user's home area and then lock it for editing. On startup if the file is locked, die.
    If the world doesn't stop annoying me I will name my kids ";DROP DATABASE;" and get revenge.

  3. #3
    Senior Member WolfeTone's Avatar
    Join Date
    Jun 2007
    Location
    Ireland
    Posts
    197
    The line of code is grand, in VB it's = and not ==.

    And I think the lock file would be the ideal solution.

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    They've invented semaphores for this.

    Another way to solve it would be to run the program as a service. That way it'll always be running, even if there are no users logged on.
    Last edited by SirDice; July 17th, 2007 at 12:39 PM.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Senior Member mungyun's Avatar
    Join Date
    Apr 2004
    Location
    Illinois
    Posts
    172
    SirDice, can a service have a system tray icon for every user to interact with? If so, then i would definitely do that.

    Aard and Wolfe, It sound pretty easy to do what you suggest, just have some file created by the app and do a check for that each time. I will probably do that for a quick fix but I really think that there has to be a way to see all of the processes for any specific user. If task manager can show users and their processes there has to be some code in the .NET framework that can do the same.

    Either way, thanks for the ideas and if anyone happens to know the fix im looking for, i would love to know.
    Thx
    I believe in making the world safe for our children, but not our children’s children, because I don’t think children should be having sex. -- Jack Handey

  6. #6
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    I would also expect your little script to work - what is the reason it does not work?

    Although the approach itself is not exactly the most elegant way (check
    SirDice's remarks!), it is an interesting issue just by itself. Thus:



    There are other ways to enumerate the running processes supplemented with
    the SessionId and the Owner. Try them, maybe one will work for you.

    1. WMI

    Add a reference to windows.management and try the following
    (example for illustrative purposes - you will figure out the rest)

    Code:
    Dim ms As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
            For Each mo As ManagementObject In ms.Get
                If CUInt(mo.InvokeMethod("GetOwner", args)) = 0 Then
                    Dim objExecutablePath As Object = mo.Properties("ExecutablePath").Value
    
                    If Not objExecutablePath Is Nothing Then
                        Console.WriteLine((args(1).ToString & "\" & args(0).ToString))
                        Console.WriteLine(objExecutablePath.ToString)
                        Console.WriteLine(mo.Properties("SessionId").Value.ToString)
    		    Console.WriteLine()
                    End If
                End If
            Next
    2. WTSQuerySessionInformationW

    You get access to that function by a dllimport

    Code:
    <DllImport("Wtsapi32.dll")> _
            Public Shared Function WTSQuerySessionInformationW(ByVal hServer As IntPtr, ByVal SessionId As Integer, ByVal WTSInfoClass As Integer, ByRef ppBuffer As IntPtr, ByRef pBytesReturned As IntPtr) As Boolean
        End Function
    Then, for illustrative purposes


    Code:
        Friend Shared WTS_CURRENT_SERVER_HANDLE As IntPtr = IntPtr.Zero
        Friend Shared WTS_UserName As Integer = 5
    
     (...)
    
        Dim procs As Process() = Process.GetProcesses()
            For Each proc As Process In procs
                If proc.ProcessName <> "Idle" Then
                    Dim AnswerBytes As IntPtr
                    Dim AnswerCount As IntPtr
                    If WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, proc.SessionId, WTS_UserName, AnswerBytes, AnswerCount) Then
                        Dim userName As String = Marshal.PtrToStringUni(AnswerBytes)
                        Console.WriteLine(userName)
                        Try
                            Console.WriteLine(proc.MainModule.FileName)
                        Catch ex As Exception
                        End Try
                        Console.WriteLine(proc.SessionId)
                        Console.WriteLine()
                    Else
                        Console.WriteLine("Could not access Terminal Services Server.")
                    End If
                End If
            Next
    Note the ugly Try-Catch in the latter - for processes like 'system' there is no MainModule.FileName defined.

    I quickly tested these approaches in a terminal server environment
    (several sessions with different users, several sessions with the same user)
    Good luck



    As per vb.net services with a system tray icon:
    google for "vb.net service system tray icon"
    and you will find plenty of examples


    Cheers
    Last edited by sec_ware; July 18th, 2007 at 09:17 AM.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  7. #7
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Just a bit of pseudo code:

    Code:
    if mysemaphore == 1 then
        # It's already running
        exit;
    endif
        mysemaphore =1; # set it so other instances know it's running
        ...
        rest of code
    
    
        # cleanup
        mysemaphore = 0; # we're quiting
        exit;
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  8. #8
    Senior Member
    Join Date
    Dec 2001
    Posts
    319
    Sec had it right with the WMI. That'd probably be the best way to go about it.

  9. #9
    Senior Member mungyun's Avatar
    Join Date
    Apr 2004
    Location
    Illinois
    Posts
    172
    Aw sweet sec, thats exactly what i was looking for. A coworker said he remembers a way to query for processes but couldnt remember what it was called and i didnt even think he was right. Im gonna go ahead and figure out the code, looks pretty straight forward. Hopefully i can get it to work... Never thought i would be doing windows programming at a web design company but meh, whaddya gonna do?

    Quote Originally Posted by sec_ware
    I would also expect your little script to work - what is the reason it does not work?
    Well kinda funny, I thought it was working because nobody had said anything in the whole week i was live tesing it but that was because only one person was using it and the beginning of the next week i get a call saying hey, only one person can use this.

    all it was doing was looking to see if more than one of that process was running. didnt matter which user was running it but it would kill itself if more than one instance was running.

    Thanks all for your help!
    Last edited by mungyun; July 18th, 2007 at 06:54 PM.
    I believe in making the world safe for our children, but not our children’s children, because I don’t think children should be having sex. -- Jack Handey

Similar Threads

  1. Basic IRC use and administration
    By MicroBurn in forum Other Tutorials Forum
    Replies: 1
    Last Post: March 2nd, 2005, 04:31 PM
  2. Network Security made easy?
    By Tiger Shark in forum Microsoft Security Discussions
    Replies: 5
    Last Post: January 14th, 2005, 08:47 PM
  3. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  4. Replies: 1
    Last Post: July 15th, 2002, 03:46 AM
  5. Batch File Tut
    By Badassatchu in forum Non-Security Archives
    Replies: 1
    Last Post: November 23rd, 2001, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •