Search code examples
cwinapicitrixterminal-serviceswtsapi32

WTSEnumerateServers ERROR_INVALID_DOMAINNAME


I'm trying to enumerate Terminal servers in our local domain, but WTSEnumerateServers() fails with ErrorCode 1212 ( The format of the specified domain name is invalid. ). I tried it with the subdomain name "sub", with the full domain name "sub.company.local" and with NULL which is a synonym for the computer's domain. Always the same.

The MSDN Article states: This function will not work if NetBT is disabled.
NetBT is NetBIOS over TCP/IP. Which, I checked, is on by default. I'm using Windows 8.1

Does anyone know why WTSEnumerateServers() is failing?

#include <stdio.h>
#include <Wtsapi32.h>
#include <Windows.h>

#pragma comment(lib, "Wtsapi32.lib")

void LastErrorMsgBox(int err);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
  PWTS_SERVER_INFO DiscoveredServers = NULL;
  DWORD count = 0;

  if(WTSEnumerateServers(NULL, 0, 1, &DiscoveredServers, &count) == 0)
  {
    LastErrorMsgBox(GetLastError());
    return 1;
  }
  WTSFreeMemory(DiscoveredServers);
  return 0;
}

void LastErrorMsgBox(int err)
{
  TCHAR  dbg[1024];
  LPTSTR sys = NULL;
  _stprintf(dbg, TEXT("Errorcode: %d\n"), err);
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&sys, 0, NULL);
  _tcsncat(dbg, sys, 1024);
  if(sys != NULL)
    LocalFree(sys);
  MessageBox(0, dbg, TEXT("Error"), MB_ICONERROR);
}

Solution

  • I searched a little and found the following here:

    /****************************************************************
    * PLEASE NOTE:
    * The WTSEnumerateServers function was no longer working in Windows 2008.
    * The WTSEnumerateServers function relies on NETBIOS to be functional in
    * the domain. Windows 2008 server disables the computer browser service on
    * the DC by default now, so Netbios is rendered useless. Enabling the computer
    * browser service on the DC only will return full functionality of the WTSAPI
    * functions. This is a great fix, but as Microsoft have said that they will
    * no longer support Netbios in the future, I’m sure we’ll have to revise our
    * development strategy in TS environment. For the record as well, those that
    * use the NETSERVEREnum function to do the same as WTSEnumerateServers, this
    * also requires the Computer Browser Service to be enabled on a Windows 2008 DC.
    * This is also the case for the Citrix WFAPI SDK - WFEnumerateServers *
    * The best replacement for this I have found is WNetOpenEnum().
    *
    ***************************************************************/

    Maybe WNetOpenEnum will help you too.