Search code examples
c++windowswinapiadsi

Issue with ADsOpenObject() remote connection and the WinNT provider


I have a problem getting an IADsContainer object from ADsOpenObject() by using the WinNT provider from a service on a remote machine.

I've been able to get the code working in the following scenarios except number 4

  1. My application is deployed on the same machine to which it connects(localhost).
  2. My application is deployed as a Local Service on the same machine to which it connects(localhost).
  3. My application is deployed on a remote machine and it connects by providing an IP.
  4. My application is deployed on a remote machine as a Local Service and it connects by providing an IP.
#include <iostream>

#include <Adshlp.h>
#include <comdef.h>

IADsContainer* container = nullptr;
HRESULT hres = ADsOpenObject( L"WinNT://192.168.1.30", L"Administrator", L"someAdminPass", ADS_SECURE_AUTHENTICATION | ADS_READONLY_SERVER, IID_IADsContainer, (void**)&container );

if( SUCCEEDED(hres) )
{
    // do stuff with the container object
}
else
{
    _com_error err( hres );
    LPCTSTR errMsg = err.ErrorMessage();
    std::wcout << errMsg;
}

I expect hres to be S_OK and valid 'container' pointer but I only get E_FAIL with error string "Unspecified error"


Solution

  • The issue is resolved thanks to the guides in the comments from @DrakeWu-MSFT!

    I just needed to specify a FQDN username. If your machine domain is "DOMAIN.COM' then the username string should be "[email protected]". This solved the issue for me. Also big "thanks" to MS for such a descriptive error message... :)

    The main code line now looks like this:

    HRESULT hres = ADsOpenObject( L"WinNT://192.168.1.30", L"[email protected]", L"someAdminPass", ADS_SECURE_AUTHENTICATION | ADS_READONLY_SERVER, IID_IADsContainer, (void**)&container );
    

    P.S. Beware that there are other WinAPI functions that don't like FQDN username.