Many times when we are using PInvoke, the return value of the function specifying the error code might return some generic error that will leave the programmer scratching his head. We cannot demand from DLL producers to return meaningful error for each possible case and what happened today was just one example of this happening. I hope that my sad example will prevent critical sanity loss to someone who encounters similar problem.
In order to implement domain management, we are using netapi32 functions:
[DllImport(“netapi32.dll”, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint NetJoinDomain(string server, string domain, string accountOU, string account, string password, uint joinOptions);
And the call to this function will look somewhat like that:
Win32Imports.NetJoinDomain(null, “domain.local”, “OU=MyUnit,DC=domain,DC=local” @”domain.local\administrator”, “password”, 0x23);
The code was running smoothly at the production environment for a while, until for some obscure reason NetJoinDomain started returning “File Not Found” error. I will not get into all details of my investigation, but the bottom line is that apparently the OU that I specified in the call was erased at the domain controller – and this is the “file” that wasn’t found. Reinstating the required OU helped the application return being happy and hitting the man responsible for OU deletion with the big hammer helped me return being happy.
Hopefully, this story will help someone who runs into this error code when joining the domain.