none
List of all users running a process RRS feed

  • Question

  • I'm writing a program in C++ CLR and I need to get the list of users running a process. The code is for a license manager and I need to limit the number of users running a program. (Each user is allowed to run unlimited copies.)

    The following two lines get me the list of processes, now I need to get the names of the owners.

    String ^ program = gcnew String("myprogram");

    array<Process^>^procs = Process::GetProcessesByName( program );

    Any recommendations on how to proceed from here since the Process class doesn't list the users?

    thanks,

    RudyS

    Tuesday, September 4, 2012 7:57 PM

Answers

All replies

  • Here is a CodeProject article that describes how to get the user's SID based on a process ID: http://www.codeproject.com/Articles/14828/How-To-Get-Process-Owner-ID-and-Current-User-SID

    It demonstrates two different approaches to this.

    That being said, if you run your program as a service on the machine, it may be simpler to have the program directly request a license.  You can then pass the current user name to limit the # of users.  This also allows you to prevent the program from running if your "manager" has been shut down...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Tuesday, September 4, 2012 8:03 PM
    Moderator
  • Thanks for the link. The C++ code in the "Comments and Discussion" section looks like it will work with a few modifications.

    I do have one question. Does the calling process need administrative rights to run correctly? I am not using this to check remote machines.

    RudyS

    Thursday, September 6, 2012 4:13 PM
  • Thanks for the link. The C++ code in the "Comments and Discussion" section looks like it will work with a few modifications.

    I do have one question. Does the calling process need administrative rights to run correctly? I am not using this to check remote machines.

    RudyS

    I believe you do need elevated permissions if you want to get the token information about other users - so you'll likely need admin permissions (unless you explicitly grant the required token access).  I would test it to verify, however.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 6, 2012 4:18 PM
    Moderator
  • If admin rights are needed, we may not be able to do what we want. When a user starts our program it confirms that a valid license has been entered. We have a customer who wants to install on a virtual computer and multiple users will run the app there. We wanted to embed the number of allowed simultaneous users in the license key, and have the app check at startup. Therefore the process will not have admin rights. 

    RudyS

    Thursday, September 6, 2012 6:55 PM
  • If admin rights are needed, we may not be able to do what we want. When a user starts our program it confirms that a valid license has been entered. We have a customer who wants to install on a virtual computer and multiple users will run the app there. We wanted to embed the number of allowed simultaneous users in the license key, and have the app check at startup. Therefore the process will not have admin rights. 

    RudyS

    A better approach is typically to install a service (whcih can run as admin), and have the program "ask" the service for a license.  It can easily tell how many users are running at any given point in time, then.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 6, 2012 7:02 PM
    Moderator
  • I just confirmed that 

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);

    fails for processes not owned by the user.

    We were trying to avoid installing a service since it requires additional software development and administrative overhead for customers. If this had worked we'd have a working solution by days end. We will probably jut cancel this idea.

    Thursday, September 6, 2012 8:01 PM
  • You might be able to get some of the info via WMI.  Though it may still fail for non-admin users

        class Program
        {
            static void Main(string[] args)
            {
                using (var query = new ManagementObjectSearcher("select * from Win32_Process"))
                    foreach (ManagementObject process in query.Get())
                    {
                        var outParams = process.InvokeMethod("GetOwner", null, null);
    
                        var result = (uint)outParams["ReturnValue"];
                        var domain = (string)outParams["Domain"];
                        var user = (string)outParams["User"];
    
                        if (0 != result) // see Win32_Process docs for meaning of return value
                            Console.WriteLine(process["Name"] + " (not available) ");
                        else
                            Console.WriteLine(process["Name"] + " " + domain + "\\" + user);
                    }
            }
        }
    


    This signature unintentionally left blank.

    Tuesday, September 11, 2012 3:08 PM