none
System.Diagnostics.Process package name RRS feed

  • Question

  • Some of the process has the same name like svchost and RuntimeBroker, but has different package name, is there any property in System.Diagnostics.Process class that could specify the package name of the process.

    Thanks

    Sunday, February 25, 2018 5:51 AM

Answers

  • For example :

    System.Diagnostics.Process[] Processes = System.Diagnostics.Process.GetProcesses();
    foreach (System.Diagnostics.Process p in Processes)
    {
        IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, p.Id);
        string sPackage = string.Empty;
        if (hProcess != IntPtr.Zero)
        {
            uint nSize = 260;
            StringBuilder sPackageFullName = new StringBuilder((int)nSize);
            GetPackageFullName(hProcess, ref nSize, sPackageFullName);
            sPackage = (sPackageFullName.ToString() != string.Empty) ? " (package name = "+ sPackageFullName.ToString() + ")":"";                        
            CloseHandle(hProcess);
        }
        Console.WriteLine(p.ProcessName + " - " + p.Id + sPackage);                    
    }

    with declarations :

    public const long SYNCHRONIZE = (0x00100000L);
    public const int STANDARD_RIGHTS_REQUIRED = (0x000F0000);
    
    public const int PROCESS_VM_READ = (0x0010);
    public const int PROCESS_VM_WRITE = (0x0020);
    public const int PROCESS_DUP_HANDLE = (0x0040);
    public const int PROCESS_CREATE_PROCESS = (0x0080);
    public const int PROCESS_SET_QUOTA = (0x0100);
    public const int PROCESS_SET_INFORMATION = (0x0200);
    public const int PROCESS_QUERY_INFORMATION = (0x0400);
    public const int PROCESS_SUSPEND_RESUME = (0x0800);
    public const int PROCESS_QUERY_LIMITED_INFORMATION = (0x1000);
    public const int PROCESS_SET_LIMITED_INFORMATION = (0x2000);
    public const long PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFFF);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool CloseHandle(IntPtr hObject);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetPackageFullName(IntPtr hProcess, ref uint packageFullNameLength, StringBuilder packageFullName);

    Thursday, March 1, 2018 3:59 PM

All replies

  • Hi William A Wang,

    Thank you for posting here.

    For your question, could you provide more information about package name?

    >>Some of the process has the same name like svchost and RuntimeBroker,but has different package name

    What does this mean? The name you want to get is it the same with the name in Task Manager?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 26, 2018 8:18 AM
    Moderator
  • Hi Wendy,

    Thanks for reply,

    If you are running Windows 10, you can open the Task Manager, go to Details tab, right click on columns header and click "Select columns", then enable the "Package name" and click "OK". Then the Package Name column should display.

    Then you can scrolle down and find the "RuntimeBroker" processes, as I said these processes has same name but has different package name, what I want is get the package name in System.Diagnostics.Process class.

    Thanks,

    William

    Tuesday, February 27, 2018 10:48 AM
  • Hi Willian A Wang,

    Thank you for feedback.

    I research C# process class and C++ win32 process class. Sorry for that I do not find that property or method to get the package name in Task Manager.

    I will keep searching. If I get something new, I will feedback as soon as possible.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 1, 2018 1:23 AM
    Moderator
  • Hi Wendy,

    Thanks for support, waiting for your response.

    William

    Thursday, March 1, 2018 6:02 AM
  • You get the package name with GetPackageFullName
    Thursday, March 1, 2018 8:28 AM
  • Hi Castorix,

    Thanks for help, this sample you provided is in C++, is there any way to call this function in C#?

    Thanks

    Thursday, March 1, 2018 2:06 PM
  • For example :

    System.Diagnostics.Process[] Processes = System.Diagnostics.Process.GetProcesses();
    foreach (System.Diagnostics.Process p in Processes)
    {
        IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, p.Id);
        string sPackage = string.Empty;
        if (hProcess != IntPtr.Zero)
        {
            uint nSize = 260;
            StringBuilder sPackageFullName = new StringBuilder((int)nSize);
            GetPackageFullName(hProcess, ref nSize, sPackageFullName);
            sPackage = (sPackageFullName.ToString() != string.Empty) ? " (package name = "+ sPackageFullName.ToString() + ")":"";                        
            CloseHandle(hProcess);
        }
        Console.WriteLine(p.ProcessName + " - " + p.Id + sPackage);                    
    }

    with declarations :

    public const long SYNCHRONIZE = (0x00100000L);
    public const int STANDARD_RIGHTS_REQUIRED = (0x000F0000);
    
    public const int PROCESS_VM_READ = (0x0010);
    public const int PROCESS_VM_WRITE = (0x0020);
    public const int PROCESS_DUP_HANDLE = (0x0040);
    public const int PROCESS_CREATE_PROCESS = (0x0080);
    public const int PROCESS_SET_QUOTA = (0x0100);
    public const int PROCESS_SET_INFORMATION = (0x0200);
    public const int PROCESS_QUERY_INFORMATION = (0x0400);
    public const int PROCESS_SUSPEND_RESUME = (0x0800);
    public const int PROCESS_QUERY_LIMITED_INFORMATION = (0x1000);
    public const int PROCESS_SET_LIMITED_INFORMATION = (0x2000);
    public const long PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFFF);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool CloseHandle(IntPtr hObject);
    
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool GetPackageFullName(IntPtr hProcess, ref uint packageFullNameLength, StringBuilder packageFullName);

    Thursday, March 1, 2018 3:59 PM
  • Thanks for help, Castorix.
    Monday, March 5, 2018 12:00 PM
  • Hi William A Wang,

    Please mark the useful reply as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 6, 2018 12:57 AM
    Moderator