Reputation: 33
Is there a way to programmatically switch from the start menu to desktop. For example if you had a service thats runs once the user logs in and you wanted that service to switch to the desktop view once the user logs in? I can't seem to find a way around it. I tried virtual key press of the windows key but that didnt work?
Upvotes: 1
Views: 1055
Reputation: 1952
I'm not really sure exactly what the problem you're facing is is. "programmatically switch from the start menu to desktop" can be interpreted a few different ways.
However, since you said "you wanted that service to switch to the desktop" "I tried virtual key press of the windows key", I assume you are trying to communicate with windows on the desktop from a service, which cannot be done. This is by design as a security feature. If you open task manager and do view -> select columns -> session ID, you will notice that the service runs in session 0, while 'desktop' applications run in the session of the logged on user. Applications cannot communicate via Windows messages between sessions.
There is a workaround, although more effort required than simply sending a virtual key press. The workaround is to have your service create a process in the user's session which then performs your tasks for you (your virtual key press method would work in this application, for example).
The API calls you will need to use to do this are:
CreateProcessAsUser
WTSGetActiveConsoleSessionId
WTSQueryUserToken
DuplicateTokenEx
EDIT
If you want to control the start menu, there is no easy method for that either. If you must do it, I suggest you use a tool called Spy++ (comes with visual studio - see Microsoft Visual Studio x.x\Common7\Tools, or can be downloaded). Use the Find Window feature to view the messages sent to the Windows Start button when you press it, and then you can see what messages you want to send to the button to control it in the way that you need to.
For example, you may see a WM_LBUTTONDOWN
message sent to the start button. that toggles the start menu. You can then use FindWindow
, perhaps with GetDesktopWindow
to get a handle to the start button, and then send the messages you want to control it with SendMessage
. You may also want to check if the start menu is shown by using the same procedure to get a handle to the start menu and using IsWindowVisible
.
Upvotes: 1