简体   繁体   English

如何从Windows 7中的C#检测应用程序中是否包含UI元素?

[英]How to detect if an application has UI elements in it from C# in Windows 7?

I have ac# application in Windows 7 that runs in Session 0. This application is basically a framework for software patches installation that will install patches in the background (in session 0). 我在Windows 7中运行的ac#应用程序在会话0中运行。此应用程序基本上是一个软件补丁安装的框架,它将在后台安装补丁(在会话0中)。

So this app will download patches from the server and start installing them on the client machines. 所以这个应用程序将从服务器下载补丁并开始在客户端计算机上安装它们。 The way it installs the patches is by calling CreateProcess("Patch.exe"). 安装补丁的方法是调用CreateProcess(“Patch.exe”)。

Now mostly, Patch.exe will be a non-ui silent installation and henceforth, installing the patch from session 0 goes through successfully. 现在大多数情况下,Patch.exe将是一个非ui静默安装,从此以后,从会话0安装补丁成功完成。

However, sometimes this Patch.exe happens to have some UI elements in it such as prompting the user for some details (like installation location, etc..) and let us say that these UI elements cannot be avoided. 但是,有时这个Patch.exe中碰巧有一些UI元素,比如提示用户提供一些细节(比如安装位置等等),让我们说这些UI元素是无法避免的。

So is it possible for my installation framework (that runs in Session 0 written in C#), to know that the process Patch.exe which was created by my framework has any UI elements in it? 那么我的安装框架(在用C#编写的Session 0中运行)是否可以知道我的框架创建的进程Patch.exe中有任何UI元素?

The reason I ask is, if I determine that the application has any UI elements in it, then I do not want to continue with the installation (a crude way of doing this would be to kill the installer process Patch.exe, but that is a different story and not of concern here). 我问的原因是,如果我确定应用程序中有任何UI元素,那么我不想继续安装(这样做的粗略方法是杀死安装程序进程Patch.exe,但那是一个不同的故事,而不是在这里关注)。

Not real easily. 不容易。 If they're native Win32 executables you could try something like hooking the CreateWindowEx WinAPI function. 如果它们是本机Win32可执行文件,您可以尝试挂钩CreateWindowEx WinAPI函数。 (See Detours for a way to hook API calls.) (有关挂钩API调用的方法,请参阅Detours 。)

For .NET you could probably determine if it references the System.Windows.Forms assembly. 对于.NET,您可以确定它是否引用System.Windows.Forms程序集。

Neither would really be a perfect solution. 两者都不是一个完美的解决方案。 The best solution would be to force a policy that patches should never display a UI (or can accept a command-line to suppress any UI). 最好的解决方案是强制一个策略,补丁永远不应该显示UI(或者可以接受命令行来禁止任何UI)。 But I'm guessing that is not possible here. 但我猜这不可能。

You can use EnumWindows API function to iterate through every window and check if it belongs to your instance of Patch.exe by calling GetWindowThreadProcessId (assuming you've created the process for Patch.exe and have its PID). 您可以使用EnumWindows API函数遍历每个窗口并通过调用GetWindowThreadProcessId检查它是否属于您的Patch.exe实例(假设您已为Patch.exe创建了进程并拥有其PID)。 You can enforce this check by calling GetWindowLong with GWL_STYLE argument and check if the resulting style has the WS_VISIBLE bit set. 您可以通过使用GWL_STYLE参数调用GetWindowLong来强制执行此检查,并检查生成的样式是否设置了WS_VISIBLE位。 Some applications can create hidden windows to do things which are not not actually related to user interface. 某些应用程序可以创建隐藏的窗口来执行与用户界面实际上没有关系的事情。

Alternatively, you can use SetWinEventHook with an argument EVENT_OBJECT_CREATE to recieve notification each time new window is created. 或者,您可以使用带有参数EVENT_OBJECT_CREATE SetWinEventHook在每次创建新窗口时接收通知。 Then you can perform a check as described above. 然后您可以执行上述检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测UI按钮是否已释放? 统一 - C# - How to detect if the UI button has been released? Unity - C# 如何检测Windows Mobile设备是否具有摄像头? C# - how to detect if windows mobile device has camera ? c# 检测从c#应用程序最小化的所有窗口 - detect all windows minimized from c# application c#应用程序如何调用带有UI的外部windows应用程序? - How to call an external windows application with UI by a c# application? c#如何检测外部应用程序是否已弹出窗口? - c# How to detect if an external application has popped up a window? 如何在C#内部检测应用程序是在控制台还是Windows模式 - How to detect internally whether the application is in console or windows mode in C# 如何调用将UI元素从WPF C#应用程序中的主线程加载到不同堆栈面板的函数 - How to call a function that loads UI elements to different stackpanel from the main thread in WPF C# application Windows窗体应用程序如何从数据层C#访问UI层中的datagridview - How to access datagridview in UI layer from data layer C# ?windows forms application 从C#Windows应用程序插入语句时,如何知道特定列具有外键冲突? - How to know particular column has foreign key violation when insert a statement from c# windows application? C#windows应用程序中的两个UI线程 - Two UI Thread in C# windows Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM