none
Process id of an Excel application object created using automation in C#

    Question

  • Hello,

     

    I have the following code:

     

    Microsoft.Office.Interop.Excel.Application app;

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

    app = new Microsoft.Office.Interop.Excel.ApplicationClass();

     

    How do I obtain the Process object associated with app?  Or its process id if that is easier?

     

    I tried:

     

    [DllImport("user32.dll")]

    static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);

    ...

    int pid = 0;

    Process p = Process.GetProcessById(GetWindowThreadProcessId(app.Hwnd, out pid));

     

    but I got an exception because GetWindowThreadProcessId returned a different Process id from the real one (I can see the correct process id in task manager.)

     

    Thank you.

     

    Kevin

     

    Saturday, March 8, 2008 2:16 AM

Answers

  • Try to use the following code to get the process id.

     

    int excelProcessId = 0;  
    GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out excelProcessId);   // convert pointer to process id
    Thursday, April 10, 2008 2:35 AM
  • Code Snippet

            Process[] P0, P1;

            P0 = Process.GetProcessesByName("Excel");

            Excel.ApplicationClass Xl = new Excel.ApplicationClass();

            int I, J;

            P1 = Process.GetProcessesByName("Excel");

            I = 0;

            if (P1.Length > 1)

            {

              for (I = 0; I < P1.Length; I++)

              {

                for (J = 0; J < P0.Length; J++)

                  if (P0[J].Id == P1[I].Id) break;

                if (J == P0.Length) break;

              }

            }

            Process P = P1[I];

     

    Saturday, March 8, 2008 5:00 PM

All replies

  • Code Snippet

            Process[] P0, P1;

            P0 = Process.GetProcessesByName("Excel");

            Excel.ApplicationClass Xl = new Excel.ApplicationClass();

            int I, J;

            P1 = Process.GetProcessesByName("Excel");

            I = 0;

            if (P1.Length > 1)

            {

              for (I = 0; I < P1.Length; I++)

              {

                for (J = 0; J < P0.Length; J++)

                  if (P0[J].Id == P1[I].Id) break;

                if (J == P0.Length) break;

              }

            }

            Process P = P1[I];

     

    Saturday, March 8, 2008 5:00 PM
  • Try to use the following code to get the process id.

     

    int excelProcessId = 0;  
    GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out excelProcessId);   // convert pointer to process id
    Thursday, April 10, 2008 2:35 AM
  • With the declaration

    [DllImport("user32.dll")]
    static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

    This one really works

    Thanks!
    Friday, May 17, 2019 1:35 PM