How to get distinguished name for a member computer when it's not connected to DC with C++

2.8k Views Asked by At

I would normally get the distinguished name of a member computer on the Active Directory network by calling:

TCHAR buff[256];
buff[0] = 0;
DWORD dwSz = SIZEOF(buff);
GetComputerObjectName(NameFullyQualifiedDN, buff, &dwSz);

But the issue happens when that member computer is currently not connected to a DC. For instance, if I take my work laptop home it will not have access to the local AD we have at work and the GetComputerObjectName will fail. (I believe with the error code 1355 or ERROR_NO_SUCH_DOMAIN.)

So my question is, is there any way to get the distinguished name of a member computer in that situation?

3

There are 3 best solutions below

0
On

Hmm. So no one has any idea, hah?

My only wild guess would be get it off of this registry key for the GPOs: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Distinguished-Name

So what do you think?

0
On

Maybe GetComputerNameEx with the ComputerNameDnsFullyQualified NameType has less of a dependency on communicating with a DC?

1
On

If you don't mind me saying, I think that what you're trying to do is somewhat flawed.

The FQDN is only really valid when the member machine is connected to the domain. If you are logged-onto the machine offline (which from your description I assume must be the case) the machine cannot any longer be thought of as having a FQDN on the domain.

After all, while you're offline, some crafty sys admin might move the machine's account in AD so that the next time you log into the domain from it it's FQDN has changed.

The registry key you cite certainly does give the FQDN but, while the machine is offline, should I think be thought of as a cache of the last recorded value.

If I were you I'd handle the exception thrown by GetComputerObjectName() and use something like gethostname() instead.

Cheers, Ian.