Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
NetJoinDomain (netapi32)
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,
          int fJoinOptions);

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:

        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


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:

A parameter is incorrect.
The specified domain did not exist.
The computer is already joined to a domain.
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.)

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!


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 directly from VS:
Terms of Use
Find References
Show Printable Version