[RESOLVED] Obtain name of any domain controller
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Obtain name of any domain controller

  1. #1
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    [RESOLVED] Obtain name of any domain controller

    The API NetGetDCName() returns the name of the primary domain controller (PDC) in the specified domain. Does any guru know how to easily obtain the name of any domain controller (primary or any of the backup domain controllers (BDC)) please?

    The problem I've come across is that whilst every domain is supposed to have a PDC, if the PDC is unavailable then the domain still works mainly OK with the BDCs. However, NetGetDCName() fails as there is no PDC available. We had a PDC major hardware failure and some code that relies upon finding the name of a DC didn't work reporting errors.

    NetGetAnyDCName() returns the name of any DC for a domain that is directly trusted by the specified server - which doesn't work in this case as the computer running the program isn't a member of the domain. I can obtain the name of a domain controller by using NetServerEnum() and looking for a computer of type SV_TYPE_DOMAIN_CTRL or SV_TYPE_DOMAIN_BAKCTRL. But this seems a long winded way of doing it. Is there an easier way (not using DNS style names) please ?

    PS When one of the BDCs was promoted to be the PDC everything worked OK again.
    Last edited by 2kaud; March 1st, 2014 at 07:49 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,888

    Re: Obtain name of any domain controller

    DsGetDcName() - But that requires DNS style operation.

    If that doesn't work, then the answer is either "you are out of luck" or "create your own (B)DC locator service" (which can be as easy as a hardcoded list of servers if you just need it to work on your own network).

    "NetServerEnum" won't work for all cases. (in particular, it will likely fail in a DNS environment). It might be close enough if you just need it to work on your domain.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: Obtain name of any domain controller

    Thanks. That's what I reckoned. I'm looking into DsGetDcName() and the other Directory Service APIs as they may prove to be better - but all this legacy code is based around the Netxxx set of APIs! I've now coded my own DC locator but I just wondered if I'd missed some obscure API that did the job.

    I haven't previously found any instance of NetServerEnum not working. Could you eleborate a little about its failure in a DNS environment please.

    PS I've confirmed with the network people that all our domains are Active Directory using DDNS (mixture of 2003/2008). The last of our NT style domains were replaced a few years ago. This legacy code is from the NT style domain era. Yes, it probably should be re-done for AD. I've added it to the 'would be nice to' list!
    Last edited by 2kaud; March 3rd, 2014 at 12:18 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: Obtain name of any domain controller

    NetServerEnum function
    The NetServerEnum function depends on the browser service being installed and running. If no browser servers are found, then NetServerEnum fails with ERROR_NO_BROWSER_SERVERS_FOUND.

    If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same function
    So, MSDN does not say a word of the function possibly not working in AD environment.
    Best regards,
    Igor

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: Obtain name of any domain controller

    Quote Originally Posted by 2kaud View Post
    I can obtain the name of a domain controller by using NetServerEnum() and looking for a computer of type SV_TYPE_DOMAIN_CTRL or SV_TYPE_DOMAIN_BAKCTRL. But this seems a long winded way of doing it. Is there an easier way (not using DNS style names) please ?
    Well, what could be easier than to call it twice with servertype SV_TYPE_DOMAIN_CTRL and SV_TYPE_DOMAIN_BAKCTRL? What it the "long winded way" you mentioned?
    Best regards,
    Igor

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: Obtain name of any domain controller

    call it twice with servertype SV_TYPE_DOMAIN_CTRL and SV_TYPE_DOMAIN_BAKCTR
    ...that is long winded!


    PS Tests using DsGetDcName() seem to indicate that this works fine.
    Last edited by 2kaud; March 4th, 2014 at 04:36 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center