How to get a process window class name from c#?

前端 未结 2 458
余生分开走
余生分开走 2021-01-14 12:22

How can I get the window class name of a certain process? I want to achieve this in c#.

I\'ve tried the process class in c# but I can only get the window name of the

相关标签:
2条回答
  • 2021-01-14 12:54

    You can also use the windows ui automation framework to achieve this without getting into pinvoke.

            int pidToSearch = 316;
            //Init a condition indicating that you want to search by process id.
            var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty, 
                pidToSearch);
            //Find the automation element matching the criteria
            AutomationElement element = AutomationElement.RootElement.FindFirst(
                TreeScope.Children, condition);
    
            //get the classname
            var className = element.Current.ClassName;
    
    0 讨论(0)
  • 2021-01-14 12:59

    I assume you mean you want to get the class name of the main window of a process.

    To do this, you will need to get the handle to the main window using the MainWindowHandle of your Process object, and then use the following interop method to obtain the class name:

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
    

    see pinvoke.net for sample code and MSDN for details on the function.

    0 讨论(0)
提交回复
热议问题