3

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.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341

1 Answers1

4

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.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • Thanks for confirming the answer. However, the function call seems to take several seconds! Why would that be? – Ira Baxter Apr 19 '14 at 18:02
  • @IraBaxter I suspect there's an RPC timeout involved. Search for that on msdn.com (it seems to be down as I type, unfortunately), and you'll likely find something useful. – Eric Brown Apr 20 '14 at 01:09
  • Hmm, yes, there's a rather long-winded explanation http://support.microsoft.com/kb/247811 including the fact that there is a bunch of RPC-based lookups. I didn't see an explicit reference to an RPC timeout, but I can see how if all those lookups are tried and not all of the recipients are active, something would time out. But is that the behavior one would expect under normal circumstances? The article isn't clear. It does say that something caches the answer obviously to avoid all that work the next time around. I've tried the lookup many times on our internal network; all delay. – Ira Baxter Apr 20 '14 at 03:07