Greetings,
I'm trying to join a computer to a domain by using NetJoinDomain from
netapi32.dll. I found the signature on pinvoke.net, but am having difficulty
getting it to work. I was hoping someone could check my code to make sure
that I am doing it right -- (this is one of my first forays into interop).
[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
public static extern int NetJoinDomain(
string lpServer,
string lpDomain,
string lpAccountOU,
string lpAccount,
string lpPassword,
int fJoinOptions);
[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
}
I am calling the NetJoinDomain method like so:
int retval = NetJoinDomain(null,
"Domain",
"OU...",
"Domain\DCAdmin",
"DCPassword",
(int) (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_ACCT_CREATE));
I get a return code of 2691 - NERR_SetupAlreadyJoined . Now, the machine is
already a member of an NT domain, which I want to move to a new AD domain, so
I tried adding the option of NETSETUP_DOMAIN_JOIN_IF_JOINED . However, this
results in a 1355 - ERROR_NO_SUCH_DOMAIN . I've tried other option
combinations, but without success.
Now, I know the domain exists, and I can Join the new domain successfully
using NETDOM.exe, so I'm wondering what else I can be doing wrong. I was
thinking maybe I am passing in the join options parameter incorrectly, but I
am not sure of another way to do it. Any ideas?
Thanks,
Peter
Dmytro Lapshyn [MVP] - 29 Nov 2005 07:14 GMT
Hi,
The first thing to check would be:
Declare an int variable and assign the result of ORing the join options flag
to it. Then run the code in the debugger, set the breakpoint on this line
and check whether you get a correct combination of options in the numeric
terms.
Also, try to pass NULL as the lpAccountOU, if possible - it's optional
according to MSDN.
Further. JoinOptions.NETSETUP_ACCT_CREATE looks suspicious - are you sure
you really want to create a new account in the target domain?
And, last but definitely not least, you say:
"Now, the machine is already a member of an NT domain, which I want to move
to a new AD domain".
I believe you first should leave the current domain (at least this is what
one has to do when moving a machine to another domain manually), and only
then join a new domain.

Signature
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
> Greetings,
>
[quoted text clipped - 53 lines]
> Thanks,
> Peter