Thursday, April 9, 2009

Detect if the current session is a media extender session from unmanaged code.

I never remember I have a blog...so I thought I'd post some code I helped a tgb member with. The question was "Is there a way for my C++ app to detect if current session is an extender session?". From managed code or host apps this is straight forward using the SDK however I did not know how to do it from unmanaged code. Here is what I came up with (Note: I only spent ~30 minutes on this in between other projects.) Does anyone know of a better way?


BOOL IsMediaCenterExtender()

{

 // Note: GetSystemMetrics is not needed; however, I believe it is faster than WTSQuerySessionInformation. If your primary usage will be via RDP consider removing

if ( !GetSystemMetrics( SM_REMOTESESSION ) )

return FALSE;
BOOL mcx = FALSE;

 LPTSTR pData = NULL;

DWORD bytesReturned = 0;
// Note: Either link wtsapi32.lib or dynamically load wtsapi32.dll w/ LoadLibrary

if ( WTSQuerySessionInformation( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSWinStationName, &pData, &bytesReturned ) && ( 6 * sizeof( TCHAR ) < bytesReturned ) && ( 0 == _tcsncmp( _T( "EH-Tcp" ), pData, 6 ) ) )

{

mcx = TRUE;

}
WTSFreeMemory(pData);
return mcx;

}

No comments: