简体   繁体   中英

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).

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").

Now mostly, Patch.exe will be a non-ui silent installation and henceforth, installing the patch from session 0 goes through successfully.

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.

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?

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).

Not real easily. If they're native Win32 executables you could try something like hooking the CreateWindowEx WinAPI function. (See Detours for a way to hook API calls.)

For .NET you could probably determine if it references the System.Windows.Forms assembly.

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). 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). You can enforce this check by calling GetWindowLong with GWL_STYLE argument and check if the resulting style has the WS_VISIBLE bit set. 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. Then you can perform a check as described above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM