Question

I'd like to know how to determine, in a running application, the domain controller/primary domain controller of the Windows workstation or server on which the application is running, using Win32 APIs.

In particular, given a machine's hostname, I want to find the name of the authoritative source of resolving that hostname to a particular machine. (I think that's the domain controller; my knowledge of this area is pretty weak so I may be asking the question the wrong way.).

I've seen a C# code fragment that purportedly does this, but don't know if there is any relation to Win32 APIs. There's lots of "how to get DC" web pages, but they are all invoking command scripts, not the APIs.

Happy to have code, but willing to do the homework on extracting the steps, if somebody points me in the right direction.

Is there an analog in Linux? (e.g., native calls to find a name server? I'm not assuming a Linux context with a Windows domain controller).

(Aha... just discovered this question: Get the domain name of a computer from Windows API. Will dig into it some more. EDIT: Maybe the function I want is NetDCName? Where do I get the parameters that it wants?).

EDIT April 19: I coded/tested NetDCName using Eric's hints. Yes, it produces the domain controller name when there is one, and an error signal when there is not, which is just the right functional behavior.

However, the function call seems to take several seconds! Why would that be? That puts an unacceptable, user-visible delay in a check I'm trying to do.

Was it helpful?

Solution

NetGetDCName is one option; if you need more functionality, DsGetDcName is also an option.

The MSDN documentation clearly states that NULL is used to indicate the default, so

nStatus = NetGetDCName(NULL, NULL, (LPBYTE *) &lpDcName);

would return the domain controller for the default domain on the local computer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top