5

In my application I need to know if the computer is the primary domain controller of a domain, so I need to know the domain of the computer to call NetGetDCName function.

Thanks.

EDIT: The problem is related with the DCOM authentication so I need to know the domain to use the DOMAIN\USERNAME in case of a PDC or COMPUTER\USERNAME if I need to use the local authentication database of the computer.

Jesus Fernandez
  • 1,170
  • 2
  • 10
  • 23

6 Answers6

17

The NetWkstaGetInfo() function returns either the domain name or the workgroup of the computer, and is therefore not a reliable way to determine if the computer is a member of a domain.

The GetComputerNameEx() function will help, used with the ComputerNameDnsDomain parameter, as shown below. This will return an empty string if the computer is in a workgroup, or the DNS name of the domain:

DWORD bufSize = MAX_PATH;
TCHAR domainNameBuf[ MAX_PATH ];

GetComputerNameEx( ComputerNameDnsDomain, domainNameBuf, &bufSize );
marco.m
  • 4,573
  • 2
  • 26
  • 41
chrfrenning
  • 191
  • 1
  • 4
7

I would consider using NetWkstaGetInfo() and pass the local computer name is that first parameter.

#include <Lmwksta.h>
#include <StrSafe.h>

WCHAR domain_name[256];
WKSTA_INFO_100 info = {0};
if (NERR_Success == NetWkstaGetInfo(L"THIS-COMPUTER", 100, &info) && 
    SUCCEEDED(StringCchCopy(domain_name, ARRAYSIZE(domain_name), info.wki100_langroup))) {
    // use domain_name here...
}
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
3

You can use the NetWkstaGetInfo Function do this.

If you pass in null for the computer name it returns info about the local computer.

It will return a WKSTA_INFO_100 instance, which includes the domain name.

Scott Wisniewski
  • 24,561
  • 8
  • 60
  • 89
3

If you just want to know if the machine the code is running is the primary domain controller I think your best option is NetServerGetInfo. If you pass 101 as the level parameter it returns an SERVER_INFO_101 structure. Then look for its sv101_type member:

sv101_type

The type of software the computer is running. This member can be one of the following values.

(...)

SV_TYPE_DOMAIN_CTRL: A primary domain controller.

Rômulo Ceccon
  • 10,081
  • 5
  • 39
  • 47
2

There is a specific function to determine the join status of a Workstation. https://learn.microsoft.com/en-gb/windows/desktop/api/lmjoin/nf-lmjoin-netgetjoininformation

THere can be 3 statussus, 'Joined' to a workgroup (The good old Windows 3.0 networking) status == NetSetupWorkgroupName, or joined to a domain status == NetSetupDomainName or unjoined (standalone) status == NetSetupUnjoined

So if you know this, you can call the respective required functions in a reliable way.

Egbert Nierop
  • 2,066
  • 1
  • 14
  • 16
0

Finally I have used this code. It works in local machine, executed remotely nStatus gives a ACCESS_DENIED error.

NET_API_STATUS nStatus;
TOleString oleServerName=strServerName.c_str();
DWORD dwLevel=101;
LPSERVER_INFO_101 pBufServer=NULL;
LPWKSTA_INFO_100 pBufWksta = NULL;

nStatus=NetServerGetInfo(oleServerName, dwLevel,
    (LPBYTE*)&pBufServer);
if(nStatus==NERR_Success &&
    (pBufServer->sv101_type & SV_TYPE_DOMAIN_CTRL))
{
    dwLevel=100;
    nStatus=NetWkstaGetInfo(oleServerName, 100,
        (LPBYTE*)&pBufWksta);

    if(nStatus==NERR_Success)
    {
        AnsiString strDomain(pBufWksta->wki100_langroup);

        m_pgServerConnection->SetDomain(strDomain);
    }
}

Thanks to all :)

Jesus Fernandez
  • 1,170
  • 2
  • 10
  • 23