Avatar of think-cell
think-cell

asked on 

Detect Windows Desktop Switch

In a user-mode C++ program (not a driver) in Windows 2000 or later with regular user privileges, I would like to get an event when the user leaves the Ctrl-Alt-Del (a.k.a. Winlogon) screen and switches back to the regular desktop.

The background is that DirectDraw initialization does not see hardware devices when on the Winlogon desktop, so I want to delay it until the user switches back.

I am currently polling, which is not satisfactory. I attached the code snippet that I am using to test whether I am on the right desktop.
static bool OnDefaultDesktop() {
	// UOI_IO checks whether given desktop is currently receiving input, but it's >=Vista.
	// So we check whether thread desktop and input desktop have the same name.
 
    HDESK hdeskInput=OpenInputDesktop(0, FALSE, 0); // does not set GetLastError(), so GetLastError() is arbitrary if NULL is returned
	if( hdeskInput==NULL ) {
		TRACE( "hdeskInput==NULL" );
		return false;
	}
 
	DWORD nLengthNeeded;
	TCHAR szInputDesktop[16];
	APIERR( GetUserObjectInformation( hdeskInput, UOI_NAME, &szInputDesktop, sizeof(szInputDesktop), &nLengthNeeded ) && nLengthNeeded<=sizeof(szInputDesktop) );
 
	APIERR( CloseDesktop( hdeskInput ) );
 
	HDESK hdeskThread=GetThreadDesktop( GetCurrentThreadId() ); // MSDN says no need to close this handle
	APIERR( hdeskThread );
 
	TCHAR szThreadDesktop[16];
	APIERR( GetUserObjectInformation( hdeskThread, UOI_NAME, &szThreadDesktop, sizeof(szThreadDesktop), &nLengthNeeded ) && nLengthNeeded<=sizeof(szThreadDesktop) );
	_ASSERTNOTIFY( _tcsicmp( szThreadDesktop, _T("DEFAULT") )==0 ); // I think it is always named "DEFAULT"
 
	return _tcsicmp( szThreadDesktop, szInputDesktop )==0;
}

Open in new window

Game ProgrammingMicrosoft DevelopmentC++

Avatar of undefined
Last Comment
think-cell

8/22/2022 - Mon