none
Getting all Process Information

    Question

  • Hi

    I am trying to get the information from the running processes. I especially wanted to get the Windows handle, but I get an 'Access Denied' error. So I  decided to just get a reading of all the information available in the processes. I'm trying something, but obviously my code isn't workable. Could someone help me fix it please? Thanks.

    Code:

    Process[] processlist = Process.GetProcesses();
                string[] results; 
                results = new string[1000];
                int resultsNum = 0;
                string resultsF;

                foreach (Process process in processlist)
                {
                    if (!String.IsNullOrEmpty(process.MainWindowTitle))
                    {
                        resultsNum += 1;
                        results[resultsNum] = "{?}";
                        resultsF = string.Format(results[resultsNum], process);
                        Console.WriteLine(resultsF);

                        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                        /*
                         * , process.Handle, process.MachineName, process.MainWindowHandle, 
                         * process.StartTime, process.TotalProcessorTime, process.UserProcessorTime, process.WorkingSet64
                         */
                        //IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
                    }
                }

    Monday, April 11, 2016 2:18 PM

Answers

  • Okay, I found a way to get around the 'Access Denied' error. I just did a search for the exact program, and then got the information:

                Process[] processlist = Process.GetProcesses();
                //string[] results; 
                //results = new string[1000];
                //int resultsNum = 0;
                //string resultsF;

                foreach (Process process in processlist)
                {
                    if (!String.IsNullOrEmpty(process.MainWindowTitle))
                    {
                        //resultsNum += 1;
                        string res = "{0}";
                        string resF = string.Format(res, process); Console.WriteLine(resF);
                        //results[resultsNum] = "{0}";
                        //resultsF = string.Format(results[resultsNum], process);
                        // find if gmax is running
                        int resIndex = resF.IndexOf("("); Console.WriteLine(resIndex);
                        string resString = resF.Remove(0, resIndex+1); Console.WriteLine(resString);
                        int resIndex1 = resString.IndexOf(")"); Console.WriteLine(resIndex1);
                        string resString1 = resString.Remove(resIndex1); Console.WriteLine(resString1);
                        int resIndex2 = resString1.IndexOf("gmax"); Console.WriteLine(resIndex2);
                        if (resIndex2 != -1) 
                        { 
                            Console.WriteLine("gmax running");
                            Console.WriteLine("Process: {0} ID: {1} Window title: {2} Handle: {3}",
                                process.ProcessName, process.Id, process.MainWindowTitle, process.Handle); 
                            break; 
                        }
                        else { Console.WriteLine("gmax not running"); }

                        //Console.WriteLine("Process: {0} ID: {1} Window title: {2} Handle: {3}", process.ProcessName, process.Id, process.MainWindowTitle);
                        /*
                         * , process.Handle, process.MachineName, process.MainWindowHandle, 
                         * process.StartTime, process.TotalProcessorTime, process.UserProcessorTime, process.WorkingSet64
                         */
                        //IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
                    }
                }

    I'll eventually use the try - catch, but I'll use this for now. Thanks

    • Marked as answer by JohnG1000 Monday, April 11, 2016 7:28 PM
    Monday, April 11, 2016 7:28 PM

All replies

  • I'm trying something, but obviously my code isn't workable.
    Please be more specific. What isn't working? Compiler error? Linker error? Is an exception being raised? If so, what is it? Are you still getting "Access denied"? Which line of code is causing the problem? Have you tried stepping through your code with the debugger?
    Monday, April 11, 2016 3:52 PM
  • Sorry. This line of code...

          resultsF = string.Format(results[resultsNum], process);

    is returning this error:

          System.FormatException: Input string was not in a correct format.
       at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)

    I know it has something to do with the array, but I don't know how to fix it. Thanks.

    Correction... 

    Evidently, it appears to be this "{?}" that's causing the problem.

    • Edited by JohnG1000 Monday, April 11, 2016 6:31 PM
    Monday, April 11, 2016 6:23 PM
  • Try replacing “{?}” with “{0}”, but you probably do not need these lines. Only keep the second Console.WriteLine inside the loop.

    In order to skip the special processes that gives “Access Denied”, perhaps use try-catch, ignore the error and continue the loop.

    Monday, April 11, 2016 6:36 PM
  • Okay, I found a way to get around the 'Access Denied' error. I just did a search for the exact program, and then got the information:

                Process[] processlist = Process.GetProcesses();
                //string[] results; 
                //results = new string[1000];
                //int resultsNum = 0;
                //string resultsF;

                foreach (Process process in processlist)
                {
                    if (!String.IsNullOrEmpty(process.MainWindowTitle))
                    {
                        //resultsNum += 1;
                        string res = "{0}";
                        string resF = string.Format(res, process); Console.WriteLine(resF);
                        //results[resultsNum] = "{0}";
                        //resultsF = string.Format(results[resultsNum], process);
                        // find if gmax is running
                        int resIndex = resF.IndexOf("("); Console.WriteLine(resIndex);
                        string resString = resF.Remove(0, resIndex+1); Console.WriteLine(resString);
                        int resIndex1 = resString.IndexOf(")"); Console.WriteLine(resIndex1);
                        string resString1 = resString.Remove(resIndex1); Console.WriteLine(resString1);
                        int resIndex2 = resString1.IndexOf("gmax"); Console.WriteLine(resIndex2);
                        if (resIndex2 != -1) 
                        { 
                            Console.WriteLine("gmax running");
                            Console.WriteLine("Process: {0} ID: {1} Window title: {2} Handle: {3}",
                                process.ProcessName, process.Id, process.MainWindowTitle, process.Handle); 
                            break; 
                        }
                        else { Console.WriteLine("gmax not running"); }

                        //Console.WriteLine("Process: {0} ID: {1} Window title: {2} Handle: {3}", process.ProcessName, process.Id, process.MainWindowTitle);
                        /*
                         * , process.Handle, process.MachineName, process.MainWindowHandle, 
                         * process.StartTime, process.TotalProcessorTime, process.UserProcessorTime, process.WorkingSet64
                         */
                        //IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
                    }
                }

    I'll eventually use the try - catch, but I'll use this for now. Thanks

    • Marked as answer by JohnG1000 Monday, April 11, 2016 7:28 PM
    Monday, April 11, 2016 7:28 PM
  • Just wanted to let you know that I finally achieved my goal, and I also wanted to share the code.

    I would have started a new thread with the title 'Get Window Class Name' but I wasn't sure I should post it as a question. So here it is:

                           

                Process[] processlist = Process.GetProcesses();

                foreach (Process process in processlist)
                {
                    if (!String.IsNullOrEmpty(process.MainWindowTitle))
                    {
                        string res = "{0}";
                        string resF = string.Format(res, process); //Console.WriteLine(resF);
                        // find if gmax is running
                        int resIndex = resF.IndexOf("("); //Console.WriteLine(resIndex);
                        string resString = resF.Remove(0, resIndex+1); //Console.WriteLine(resString);
                        int resIndex1 = resString.IndexOf(")"); //Console.WriteLine(resIndex1);
                        string resString1 = resString.Remove(resIndex1); //Console.WriteLine(resString1);
                        int resIndex2 = resString1.IndexOf("gmax"); //Console.WriteLine(resIndex2);
                        if (resIndex2 != -1) 
                        {
                            int gcn;
                            // Pre-allocate 256 characters, since this is the maximum class name length.
                            StringBuilder ClassName = new StringBuilder(256);
                            //Get the window class name
                            gcn = GetClassName(process.MainWindowHandle, ClassName, ClassName.Capacity);
                            if (gcn != 0) 
                            { 
                                strClassName = ClassName.ToString(); Console.WriteLine(strClassName); 
                            }
                            else { Console.WriteLine("failed to get classname."); }

                            string res1 = "{2}";
                            string resF1 = string.Format(res, process.MainWindowTitle); Console.WriteLine(resF1);
                            strWinName = resF1;

                            Console.WriteLine("gmax running  ");
                            break; 
                        }
                        else { Console.WriteLine("gmax not running"); }
                    }
                }

                // Get a handle to the application without the Spy++ tool.
                IntPtr gmaxHandle = FindWindow(strClassName, strWinName);
                // Make application the foreground application and send an input.
                SetForegroundWindow(gmaxHandle);
                SendKeys.SendWait("ENTER");






    • Edited by JohnG1000 Tuesday, April 12, 2016 12:30 AM process.MainWindowTitle instead of process
    Tuesday, April 12, 2016 12:08 AM
  • Sorry I made a few errors, in my excitement to share. It's working.
    Tuesday, April 12, 2016 12:32 AM