Search code examples
delphiwinapishowwindow

ShowWindow doesn't work when a window is running under SYSTEM account


I'm currently using this code to restore a minimized window, and it works perfectly when I try to restore a window that runs under my own user account.

ShowWindow(wHandle, SW_RESTORE);

The problem arises when I try to restore a window that runs under the SYSTEM account. I found out that ShowWindow() always returns a false value to me. Also, in my manifest, I have tried setting it to both "Requires Administrator" and "As Invoker", but it still yields the same result.

Is there another function that works similarly to ShowWindow() and is able to restore a window that is running under the SYSTEM account?


Solution

  • You can create a thread with "local system" privileges in the user application. Then execute your ShowWindow in the thread.

    Some steps:

    1. Program sends session ID to service
    2. Service calls OpenProcessToken and DuplicateTokenEx to create a Local System token
    3. Service calls SetTokenInformation to change the token session ID to match the program's
    4. Service calls DuplicateHandle to create handle to the token
    5. Service sends handle to program
    6. Program calls SetThreadToken using the received handle

    Refer: Run process as Local System

    For the @fpiette's comment, Interacting with a User from a Service Indirectly shows more details.