Microsoft KB Archive/251066

From BetaArchive Wiki
Knowledge Base

How To Determine If a Computer Is a Microsoft Terminal Server

Article ID: 251066

Article Last Modified on 7/1/2004


  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 3.0b Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition

This article was previously published under Q251066


This article shows how to determine if a computer is a Microsoft Windows NT or Windows 2000 Terminal Server.


  1. In Visual FoxPro, create a program file using the following code:

    *Start of Code
    LOCAL lTerminalServer
    lTerminalServer = IsTerminalServer()
    IF (lTerminalServer) THEN
       =MESSAGEBOX ("This Computer is a Terminal Server")
       =MESSAGEBOX("This Computer is not a Terminal Server")
    FUNCTION IsTerminalServer
       * This function will determine if the machine is a Terminal Server.
       * It will return .T. if it is and .F. if not.
       * Constants that are needed for Registry functions
       #DEFINE HKEY_LOCAL_MACHINE -2147483646  && BITSET(0,31)+2
       #DEFINE REG_DWORD 4                     && A 32-bit number.
       * WIN 32 API functions that are used
       DECLARE Integer RegOpenKey IN Win32API ;
          Integer nHKey, String @cSubKey, Integer @nResult
       DECLARE Integer RegQueryValueEx IN Win32API ;
          Integer nHKey, String lpszValueName, Integer dwReserved,;
          Integer @lpdwType, String @lpbData, Integer @lpcbData
       DECLARE Integer RegCloseKey IN Win32API ;
          Integer nHKey
       * Local variables used
       LOCAL nErrCode      && Error Code returned from Registry functions
       LOCAL nKeyHandle    && Handle to Key that is opened in the Registry
       LOCAL lpdwValueType     && Type of Value that we are looking for
       LOCAL lpbValue      && The data stored in the value
       LOCAL lpcbValueSize     && Size of the variable
       LOCAL lpdwReserved      && Reserved Must be 0
       LOCAL cKey          && Key we need to open
       LOCAL cValueToGet       && Value to get
       LOCAL lTerminalServer   && True if it is a Terminal Server
       LOCAL nTSEnabled    && Numeric value of TSEnabled
       LOCAL OSVersion     && OS Version
       LOCAL cMajorVersion     && Just the Version number i.e. 4 or 5
       * Initialize the variables
       cKey = "System\CurrentControlSet\Control\Terminal Server"
       cValueToGet = "TSEnabled"
       nKeyHandle = 0               
       lpdwReserved = 0        && Must be 0
       lpdwValueType = REG_DWORD
       lpcbValueSize = 4          && DWORD is 4 bytes
       lTerminalServer = .F.      && Assume it isn't a Terminal Server
       lpbValue = SPACE(4)
       nErrCode = RegOpenKey(HKEY_LOCAL_MACHINE, cKey, @nKeyHandle)
       * If the error code isn't 0, then the key doesn't exist or can't be opened.
       IF (nErrCode # 0) THEN
          RETURN .F.
       * We need to check and see what version we are running NT 4 and Windows 2000 are different
       OSVersion = LongToStr(148) + REPLICATE(CHR(0), 144)
       nMajorVersion = StrToLong(SUBSTR(OSVersion, 5, 4))
       cMajorVersion = ALLTRIM(STR(nMajorVersion))
       * If it is NT 4 and we made it this far the machine is a Terminal Server
       IF (cMajorVersion = "4") THEN
          * Close the key when done.
          RETURN .T.
       * Get the value of TSEnabled. 1 is a Terminal  Server 0 isn't 
       nErrCode=RegQueryValueEx(nKeyHandle, cValueToGet, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
       * Close the key when done.
       IF (nErrCode # 0) THEN
          RETURN .F.
          nTSEnabled = StrToLong(lpbValue)
       * Check to see if it is a Terminal Server    
       IF (nTSEnabled = 1) THEN
           RETURN .T.
          RETURN .F.        
    FUNCTION StrToLong
    * This function converts a String to a Long
       PARAMETERS cLongStr
       LOCAL nLoopVar, nRetval
       nRetval = 0
       FOR nLoopVar = 0 TO 24 STEP 8
          nRetval = nRetval + (ASC(cLongStr) * (2^nLoopVar))
          cLongStr = RIGHT(cLongStr, LEN(cLongStr) - 1)
    RETURN nRetval
    FUNCTION LongToStr
    * This function converts a long to a string
       PARAMETERS nLongVal
       LOCAL nLoopVar, strReturn
       strReturn = ""
       FOR nLoopVar = 24 TO 0 STEP -8
          strReturn = CHR(INT(nLongVal/(2^nLoopVar))) + strReturn
          nLongVal = MOD(nLongVal, (2^nLoopVar))
    RETURN strReturn
    *End of Code
  2. Run the program created in step 1. The result appears in a message box as:

    This computer is a Terminal Server.


    This computer is not a Terminal Server.

Keywords: kbhowto kbapi kbcodesnippet KB251066