Detecting the Terminal Server Environment

[This is preliminary documentation and subject to change.]

An application can detect if it is being installed in or is running in a Windows®-based Terminal Server environment. The application can then optimize its behavior for its environment.

When Terminal Server is installed, it adds the string "Terminal Server" to the list of installed product suites in the registry. The following example function, ValidateProductSuite, shows how to check the registry for this string. You can use the following code with the sample function, ValidateProductSuite, to determine whether Terminal Server is installed.

BOOL fIsTerminalServer;
fIsTerminalServer = ValidateProductSuite( TEXT("Terminal Server") ); 
 

The following code implements the ValidateProductSuite example function, which checks the registry to determine whether a specified product suite is installed.

#include <tchar.h>
BOOL
ValidateProductSuite(
    LPTSTR lpszSuiteName
    )
{
    BOOL fValidated = FALSE;
    LONG lResult;
    HKEY hKey = NULL;
    DWORD dwType = 0;
    DWORD dwSize = 0;
    LPTSTR lpszProductSuites = NULL;
    LPTSTR lpszSuite;

// Open the ProductOptions key.

    lResult = RegOpenKey(
        HKEY_LOCAL_MACHINE,
        TEXT("System\\CurrentControlSet\\Control\\ProductOptions"),
        &hKey
        );
    if (lResult != ERROR_SUCCESS) {
        goto exit;
    }

// Determine required size of ProductSuites buffer.

    lResult = RegQueryValueEx(
        hKey,
        TEXT("ProductSuite"),
        NULL,
        &dwType,
        NULL,
        &dwSize
        );
    if (lResult != ERROR_SUCCESS) {
        goto exit;
    }

    if (!dwSize) {
        goto exit;
    }

// Allocate buffer.

    lpszProductSuites = (LPTSTR) LocalAlloc( LPTR, dwSize );
    if (!lpszProductSuites) {
        goto exit;
    }

// Retrieve array of product suite strings.

    lResult = RegQueryValueEx(
        hKey,
        TEXT("ProductSuite"),
        NULL,
        &dwType,
        (LPBYTE) lpszProductSuites,
        &dwSize
        );
    if (lResult != ERROR_SUCCESS) {
        goto exit;
    }

    if (dwType != REG_MULTI_SZ) {
        goto exit;
    }

// Search for suite name in array of strings.

    lpszSuite = lpszProductSuites;
    while (*lpszSuite) {
        if (_tcscmp( lpszSuite, lpszSuiteName ) == 0) {
            fValidated = TRUE;
            break;
        }
        lpszSuite += (_tcslen( lpszSuite ) + 1);
    }

exit:
    if (lpszProductSuites) {
        LocalFree( lpszProductSuites );
    }

    if (hKey) {
        RegCloseKey( hKey );
    }

    return fValidated;
}