Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

C++ Example For NetJoinDomain API Call

Posted on 2004-08-10
Last Modified: 2008-02-01
I found a question by Dan Kennedy which asked the following:
Does anyone have any example code or information on programatically joining a computer to an NT/2000 domain using C code?
I'm looking for the names of API calls or example code (even better).

Comments were made about tne NetJoinDomain() API call in the answers to his question, however, I am unable to get the API to work properly.

Our situation is that we are adding computers that have NT and XP to our new Active Directory Domain.  I am able to unjoin the computers programatically via the NetUnjoinDomain, but have not had luck joining the computers to the Active Directory.  I end up with "ERROR_NONE_MAPPED: No mapping between account names and security IDs was done."

For simplicity purposes (or the KISS principle as it were...) I have the following to demonstrate what I was attempting (trimmed out of test program):

#ifndef UNICODE
#define UNICODE

#include <iostream>
#include <shlwapi.h>
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

#include <lm.h>
#include "ntsecapi.h"

using std::string;

NET_API_STATUS  fnTest() {
      LPCWSTR ml_Domain                        ;
      LPCWSTR ml_OUPath                        ;
      LPCWSTR ml_UserName                        ;
      LPCWSTR ml_UserPWD                        ;

      NET_API_STATUS m_Result                  ;

      ml_Domain = L"MFAD\\MFADIR00" ;
      ml_OUPath        = L"OU=EED,OU=Computers,OU=MCR,DC=mfad,DC=mfroot,DC=ad" ;
      ml_UserName   = L"MFAD\\Account"; // <-- Domain/Account here      ;
      ml_UserPWD    = L"password"; // <-- PASSWORD HERE

      m_Result = NetJoinDomain(NULL, ml_Domain,
                                                                 ml_OUPath, ml_UserName,
                                                                              || NETSETUP_ACCT_CREATE);
                 return m_Result;

As an alternate method I attempted:

      WCHAR m_Domain[  257]      ;
      WCHAR m_UserName[UNLEN+1]      ;
      WCHAR m_UserPWD[       PWLEN+1]      ;
      WCHAR m_OUPath[   257]      ;

      char ms_UserName[UNLEN+1];
      char ms_UserPWD[PWLEN+1];
      char ms_OUPath[257];
      char ms_Domain[257];

      ::ZeroMemory(ms_UserName, UNLEN);
      ::ZeroMemory(ms_UserPWD , PWLEN);
      ::ZeroMemory(ms_OUPath  , 256);
      ::ZeroMemory(ms_Domain  , 256);

      sprintf(ms_UserName, "MFAD\\ACCOUNT");
      sprintf(ms_UserPWD , "password");   //<-- PASSWORD HERE
      sprintf(ms_OUPath  , "OU=EED,OU=Computers,OU=MCR,DC=mfad,DC=mfroot,DC=ad");
      sprintf(ms_Domain  , "MFAD\\MFADIR00");

      MultiByteToWideChar( CP_ACP, 0, ms_UserName, strlen(ms_UserName)+1, m_UserName, sizeof(m_UserName)/sizeof(m_UserName[0]));
      MultiByteToWideChar( CP_ACP, 0, ms_UserPWD , strlen(ms_UserPWD )+1, m_UserPWD , sizeof(m_UserPWD )/sizeof(m_UserPWD[ 0]));
      MultiByteToWideChar( CP_ACP, 0, ms_OUPath  , strlen(ms_OUPath  )+1, m_OUPath  , sizeof(m_OUPath  )/sizeof(m_OUPath[  0]));
      MultiByteToWideChar( CP_ACP, 0, ms_Domain  , strlen(ms_Domain  )+1, m_Domain  , sizeof(m_Domain  )/sizeof(m_Domain[  0]));

      m_Result = NetJoinDomain(NULL, m_Domain, m_OUPath, m_UserName, m_UserPWD, NETSETUP_JOIN_DOMAIN || NETSETUP_ACCT_CREATE);

With the same results...

Any Ideas, or have a sample of code that works?

Question by:sellersjr
  • 3
  • 3
LVL 86

Expert Comment

ID: 11767598
The names seem somehow messed up, IMHO that should be

    ml_Domain = L"MFAD" ;
    ml_OUPath       = L"OU=EED,OU=Computers,OU=MCR,DC=mfad,DC=mfroot,DC=ad" ;
    ml_UserName   = L"MFAD\\Account"; // <-- Domain/Account here     ;
    ml_UserPWD    = L"password"; // <-- PASSWORD HERE

LVL 22

Expert Comment

ID: 11767801
ERROR_NONE_MAPPED is a roundabout way of saying the user account could not be looked up.  

Maybe take slightly smaller steps, just do the API to lookup the user sid first and see if that works?


Author Comment

ID: 11772251
Although the join did work by changing MFAD\\MFADIR00 to MFAD, the issue is that we want to join the computers to a specific domain controller rather than the default controller.  This is to avoid the delay caused by the addition replicating to the various domain controllers within MFAD.

Does NetJoinDomain allow connection to a specific controller? or just to the domain itself?
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 11774467
As an update of sorts, I was able to get the API call to work with MFAD\MFADIR00 on a system which had not been added to MFAD.  The problem, apparently, may be part of replication and accounts.


I add a computer to MFAD\MFADIR00 via API.
I use Active Directory Management to remove the account I just created.
I can run the program again and it re-adds the account.

Wait 10-20 minutes (replication time is around 15 minutes) and I then get the error I posted above ("ERROR_NONE_MAPPED: No mapping between account names and security IDs was done.").

A co-worker has a VB.Net application which calls the API with the same parameters that I use, and the VB.Net application adds the computer successfully every time.

To test my application I use the VB.Net to add the computer and then run mine -- and my code then works as the account exists in MFAD. (as the VB.Net program had previously created the account in MFAD)

Is there some difference in the API libraries when called from .Net?

LVL 86

Accepted Solution

jkr earned 500 total points
ID: 11775964
>>Is there some difference in the API libraries when called from .Net?

Usually, there isn't - but, I think I spotted the error:

     m_Result = NetJoinDomain(NULL, ml_Domain,
                                                                 ml_OUPath, ml_UserName,
                                                                              || NETSETUP_ACCT_CREATE);

should be

     m_Result = NetJoinDomain(NULL, ml_Domain,
                                                                 ml_OUPath, ml_UserName,
                                                                              | NETSETUP_ACCT_CREATE);  // <-------!!!!

Using '||' means using the logical 'or' instead of the bitwise 'or', thus making the parameter '1' instead of a combination of NETSETUP_JOIN_DOMAIN and NETSETUP_ACCT_CREATE, which probably skips the account creation.


Author Comment

ID: 11776829
I will wear the Dunce cap for a while on that one...  And I've looked at that line with others for longer than I should have...
Apparently it has been a while since I did bitwise ORs, and comparing it with VB code didn't help matters any...

I was able to join against a specific domain controller as well.

Many Thanks!

LVL 86

Expert Comment

ID: 11779950
>> I will wear the Dunce cap for a while on that one...  

No need for that - we have a proverb here that hits the nail on the head by stating to be "unable to spot the forest because of all those trees around you" :o)

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question