How can I change desktops? I am interested in sending mouse events to winlogon desktop(I will run the application on system account). The thing is that nothing happens when I call mouse_event(or any other function(keybd_event,sendinput,sendkeys etc.) Nothing happens even if I am still on the default desktop. setthreaddesktop returns true, so that means the change succeded. I even tried to put a messagebox with the same result.
I created a new thread because otherwise the setthreaddesktop return 170 error code(resource in use).
bool dd = false;
StringBuilder name = new StringBuilder(200, 200);
uint sss = 0;
IntPtr hDesktop = OpenInputDesktop(1, true, desktop_ReadObjects | desktop_WriteObjects);
System.Threading.Tasks.Task.Factory.StartNew(() =>
{ dd = SetThreadDesktop(hDesktop);
CloseDesktop(hDesktop);
bool get = GetUserObjectInformation(hDesktop, 2, name, 100, ref sss);
SendKeys.Send("X");
mouse_event(MOUSEEVENTF_LEFTDOWN, 400, 400, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 400, 400, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 400, 400, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, 400, 400, 0, 0);
}).Wait();
Edit: If I don't use SetThreadDesktop mouse_event is working
Thanks,
The Windows logon desktop is protected and requires special permissions to run processes and message loops.
If you're running under SYSTEM, you can duplicate your own security token and change the SessionId with SetTokenInformation to create a new process (using CreateProcessAsUser) on the WinSta0\Winlogon desktop of the target user session.
Alternatively, you can duplicate the security token of another process running as SYSTEM in the target user session, such as winlogon.exe and lsass.exe. See: Running a process at the Windows 7 Welcome Screen
Either way, you're going to be making a lot of Win32 calls to accomplish this task.