Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

NetJoinDomain (netapi32)
 
.
Summary
The NetJoinDomain function joins a computer to a workgroup or domain.

C# Signature:

[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
static extern int NetJoinDomain(
          string lpServer,
          string lpDomain,
          string lpAccountOU,
          string lpAccount,
          string lpPassword,
          JoinOptions NameType);

VB Signature:

Declare Function NetJoinDomain Lib "Netapi32.dll" _
        (ByVal lpServer As String, _
        ByVal lpDomain As String, _
        ByVal lpAccountOU As String, _
        ByVal lpAccount As String, _
        ByVal lpPassword As String, _
        ByVal fJoinOptions As Integer) As Integer

User-Defined Types:

        [Flags]
        public enum JoinOptions
        {
          NETSETUP_JOIN_DOMAIN = 0x00000001,
          NETSETUP_ACCT_CREATE = 0x00000002,
          NETSETUP_ACCT_DELETE = 0x00000004,
          NETSETUP_WIN9X_UPGRADE = 0x00000010,
          NETSETUP_DOMAIN_JOIN_IF_JOINED = 0x00000020,
          NETSETUP_JOIN_UNSECURE = 0x00000040,
          NETSETUP_MACHINE_PWD_PASSED = 0x00000080,
          NETSETUP_DEFER_SPN_SET = 0x10000000
        }

Notes:

Be careful to specify CharSet.Unicode as otherwise the strings will be encoded as Ansi whereas the function only supports Unicode.

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes or one of the system error codes.

Return code Description:

ERROR_INVALID_PARAMETER
A parameter is incorrect.
ERROR_NO_SUCH_DOMAIN
The specified domain did not exist.
NERR_SetupAlreadyJoined
The computer is already joined to a domain.
NERR_InvalidWorkgroupName
The specified workgroup name is not valid.

Tips & Tricks:

To join a machine to a domain the same way that netdom.exe does, use the flags NETSETUP_JOIN_DOMAIN and NETSETUP_ACCT_CREATE. (I determined this by using a debugger to watch which flags netdom used.)

NOTE
If the machine is already in a domain use (JoinOptions.NETSETUP_JOIN_DOMAIN + JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED). JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED used on it's own always returns NERR_SetupAlreadyJoined.

Sample Code:

C# Sample:

    public class join
    {

    [DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
    static extern uint NetJoinDomain(
      string lpServer,
      string lpDomain,
      string lpAccountOU,
      string lpAccount,
      string lpPassword,
      JoinOptions NameType);

    [Flags]
    enum JoinOptions
    {
        NETSETUP_JOIN_DOMAIN = 0x00000001,
        NETSETUP_ACCT_CREATE = 0x00000002,
        NETSETUP_ACCT_DELETE = 0x00000004,
        NETSETUP_WIN9X_UPGRADE = 0x00000010,
        NETSETUP_DOMAIN_JOIN_IF_JOINED = 0x00000020,
        NETSETUP_JOIN_UNSECURE = 0x00000040,
        NETSETUP_MACHINE_PWD_PASSED = 0x00000080,
        NETSETUP_DEFER_SPN_SET = 0x10000000
    }

    public static uint domainjoin(string server, string domain, string OU, string account, string password)
    {
        try
        {
        uint value1 = NetJoinDomain(server, domain, OU, account, password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED | JoinOptions.NETSETUP_ACCT_CREATE));
        return value1;
        }
        catch (Exception e)
        {
        MessageBox.Show(e.Message);
        return 11;
        }
    }
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions