GetShellWindow vs. GetDesktopWindow

本文通过对比GetShellWindow和GetDesktopWindow两个API,揭示了它们在获取窗口句柄上的不同之处,并通过示例代码及Spy++工具进一步验证了两者的差异。

From http://blogs.microsoft.co.il/pavely/2011/06/18/getshellwindow-vs-getdesktopwindow/

In his post about running a process as a standard user from an elevated process, Aaron Margosis uses a technique that gets the access token of the shell process (typically explorer.exe) and uses that token to launch the new process (Sasha Goldshtein also blogged about that).

The first thing his code does is try to locate the shell process id. One way is to look for “explorer.exe” in the list of processes, but that’s a bit limiting, as there may be a different shell, or it may have been renamed for whatever reason. His code calls GetShellWindow to get the shell window handle, followed by GetWindowThreadProcessId that returns the window’s creator thread (and optionally its parent process). A similar looking function is GetDesktopWindow, which seems at first glance as a better candidate, or at least an equivalent. Let’s test that:

HWND hShell = ::GetShellWindow();         
HWND hDesktop = ::GetDesktopWindow();         
cout << "Shell window: " << hShell << endl;         
cout << "Desktop window: " << hDesktop << endl;         

DWORD idShell, idDesktop;         
::GetWindowThreadProcessId(hShell, &idShell);         
::GetWindowThreadProcessId(hDesktop, &idDesktop);         

TCHAR filename[MAX_PATH];         
if(GetImageFileName(idShell, filename, MAX_PATH))         
    wcout << "Shell process: " << filename << endl;         

if(GetImageFileName(idDesktop, filename, MAX_PATH))         
    wcout << "Desktop process: " << filename << endl;

GetImageFileName is a simple helper for getting the process image name:

BOOL GetImageFileName(DWORD pid, TCHAR* name, DWORD size) {         
    HANDLE hProces = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);         
    if(!hProces) return FALSE;         
    DWORD sz = size;         
    BOOL success = ::QueryFullProcessImageName(hProces, 0, name, &sz);         
    ::CloseHandle(hProces);         
    return success;         
}

This is the result of running this code:

image

The difference is evident: GetShellWindow returns the “program manager” window created by explorer.exe, while GetDesktopWindow returns the window created by the Windows subsystem process (csrss.exe). This code requires running elevated, so that opening a handle to csrss.exe succeeds, as it runs (naturally) under the all powerful system account.

What’s the relation between these two Windows? We can find out using the Spy++ utility (spyxx.exe from the Visual Studio tools). Run Spy++ (by default elevated). You should see something like this:

image 

If the Windows window is not shown, select Spy->Windows from the menu. Note that the root window handle is the same as the desktop window handle reported by our simple application. This window is owned by csrss.exe. That means it exists whether a shell process exists or not.

Now let’s find the shell window. Select Search->Find Window from the menu and type the handle reported by the application (1013A in this case) (hex is ok).

image

Click OK. The selected window is now this one:

image

This is the “Program Manager” of explorer.exe. How can we verify that? Right click the window and select Properties:

image

Click the Process tab:

image

Now click the shown process id:

image

This is indeed explorer.exe.

If we go back and look at the shell window again, and go up in the tree to its parent, we would find it to be the desktop window itself!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值