4.2 TermService Listener Example

The following example retrieves the listeners that run on the terminal server. This example uses TermSrvBindSecure from section 4.3.

  1. Get the RCM binding.

     HANDLE GetRCMBinding(LPWSTR pszServerName)
     {
       RPC_STATUS rpcStatus = RPC_S_OK;
       HANDLE hRCMBinding = NULL;
      
       rpcStatus = TermSrvBindSecure(
         gpszPublicUuid,
         gpszRemoteProtocolSequence,
         pszServerName,
         TSRCMRPC_REMOTE_ENDPOINT,
         gpszOptions,
         &hRCMBinding
         );
      
       if( rpcStatus != RPC_S_OK  || hRCMBinding == NULL)
       {
         wprintf(L"ERR: TermSrvBindSecure failed: %d\n", rpcStatus );
         SetLastError( rpcStatus );
       }
      
       return hRCMBinding
     }
      
      
    
  2. Get the list of listeners.

     hRCMBind = GetRCMBinding(pszServerName);
     if(hRCMBind)
     {
         RpcTryExcept
         {
             hr = RpcGetAllListeners( hRCMBind, &pListeners, 
                                       CURRENT_LST_ENUM_LEVEL, &NumListeners );
             if(hr == S_OK)
             {
                 for(ULONG i=0;i<Entries;i++)
                 {
                     wprintf(L"%-10d %-20s %-40s\n", 
                         pListeners[i].Data.ListenerEnum_Level1.Id,                     WinstationStateClassNames[pListeners[i].Data.ListenerEnum_Level1.bListening ? State_Listen : State_Down], 
                         pListeners[i].Data.ListenerEnum_Level1.Name);
                 }
                 rv2 = TRUE;
             }
             else
             {
                 wprintf(L"ERR: RpcGetAllListeners failed %d\n",hr );
             }
      
             if ( pListeners )
             {
                 MIDL_user_free( pListeners );
             }
      
         }
      
         RpcExcept(I_RpcExceptionFilter(RpcExceptionCode()))
         {
             hr = HRESULT_FROM_WIN32( RpcExceptionCode() );
             wprintf(L"ERR: RpcGetAllListeners threw an exception: 0x%x\n",
                      hr );
         }
      
         RpcEndExcept
      
      
         RpcBindingFree(&hRCMBind);
     }
      
      
    
  3. Close the handle.

       
     if(hRCMBind)
               RpcBindingFree(&hRCMBind);
      
    
Show: