user32 GetClassName不正确 [英] user32 GetClassName isn't correct

查看:134
本文介绍了user32 GetClassName不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例程,可以获取所有打开的窗口(进程),然后使用user32中的GetClassName方法搜索其类名。但是,例如在所有应用程序的类名上都使用Teamviewer时,请获取teamviewer的类名。

I have a routine that get's all open windows (processes) and then searches for it's classname with the GetClassName method in user32. But when for example Teamviewer is on the classnames of all applications get the teamviewer classname.

示例:记事本已打开,TeamViewer上的类名是:'TeamViewer_TitleBarButtonClass'
记事本是打开的,并且TeamViewer的类名称为:'Notepad'

Example: Notepad is open and TeamViewer on classname: 'TeamViewer_TitleBarButtonClass' Notepad is open and TeamViewer off classname: 'Notepad'

我看了看这是怎么来的,发现Teamviewer在某些应用程序窗口的顶部放置了一个控件。

I looked how this came and found out that Teamviewer puts a control on top of some application windows.

那么我怎么能找到应用程序的真实类名而不是从Teamviewer中找到呢?

So how can i find the real classname of the applications and not from Teamviewer?

Process[] processes = Process.GetProcesses();  
StringBuilder className = new StringBuilder(100);  
For (int i = 0; i < processes.Length; i++)
     {
          if (processes[i].MainWindowHandle != IntPtr.Zero)
          {
                        list.Add(processes[i]);
                        GetClassName(processes[i].MainWindowHandle, className, className.Capacity);
          }
     }


推荐答案

Process类用来猜测哪个窗口是主窗口的启发式方法并不完美。应用程序无法将其创建的窗口标记为这是主要窗口。因此,最好的猜测是:第一个窗口。当然,这可能会出错,例如,您可能会找到一个隐藏的登录窗口。

The heuristic that the Process class uses to guess which window is the "main" window is not perfect. There isn't any way for an app to mark the windows it creates as "this is the main one". So it punts at the best guess: the first window. This certainly can go wrong, you may find a hidden login window for example.

另一种方法是从Process.Threads中枚举进程中的线程,然后为每个线程枚举用EnumThreadWindows()枚举它拥有的窗口,在每个窗口上调用GetClassName()。您将以这种方式看到所有窗口,它们将跨您要查找的窗口。如果无法选择该过程,则可以使用EnumWindows()。这样还可以避免您的当前代码太早枚举系统进程而导致的崩溃。

An alternative is to enumerate the threads in the process from Process.Threads, then for each thread to enumerate the windows it owns with EnumThreadWindows(), calling GetClassName() on each. You'll get to see all of the windows that way and should run across the one you are looking for. Using EnumWindows() is an alternative when can't be selective about the process. That also avoids the crash your current code suffers from when it happens to enumerate the "System" process too early.

最好处理诸如 TeamViewer之类的侵入性软件就是要卸载它。

The best to deal with intrusive software like this "TeamViewer" is to just uninstall it.

这篇关于user32 GetClassName不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆