ShowWindow function does not work when the target application is launched as an administrator

I am writing a program that displays / hides the window of some target application. I tested it before and noticed something strange. If I run the target application as an administrator (right-click-> Properties, tab "Compatibility", "Run this program as administrator"), it does not work.

To demonstrate, I wrote a simple graphical application called "TargetApplication", and then I wrote the following code to verify that this application is shown / hidden:

class Program { static void Main(string[] args) { IntPtr windowPtr = FindWindow(null, "TargetApplication"); ShowWindow(windowPtr, 0); // 0 = Hide Console.WriteLine("The window is now hidden. Press Enter to restore"); Console.ReadLine(); ShowWindow(windowPtr, 9); // 9 = Restore Console.WriteLine("The window is now restored. Press Enter to exit."); Console.ReadLine(); } [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } 

If I run a windowed application without administrator privileges, it does not work.

Could anyone check this out for me? I downloaded the .exe file for both applications:

Download TestShowWindow

All you have to do is download them and run TestApplication.exe and then run TestShowWindow.exe. You will find that by changing TestApplication.exe to run as administrator, ShowWindow no longer works.

Of course, if you do not trust the download of my materials, you can always compile my code and test it on any target application in Windows so that you can change the compatibility mode.

PS I'm not sure if that matters, but I'm running Windows 8 Pro. 64 bit

+4
source share
2 answers

This is by design. This is a lesser known UAC twin called UIPI or UI privilege isolation. An unraised program cannot command an elevated one. Given the capabilities of UI Automation, this is an obvious countermeasure to stop programs from grabbing the power of an advanced process. Security breach caused by a destructive attack .

Workarounds are to provide a manifest with uiAccess = true for a program stored in c: \ windows or c: \ program files and provided with a certificate. And for the target program, call ChangeWindowMessageFilter to allow certain messages to be sent. In your case, it should be WM_SHOWWINDOW.

+7
source

If you are not against the window acting, how did you reduce it to the taskbar; You can usually show and hide windows from elevated processes by sending WM_SYSCOMMAND using wParam from SC_RESTORE or SC_MINIMIZE.

0
source

Source: https://habr.com/ru/post/1447153/


All Articles