Directory Services

Example Code for Getting the Distinguished Name of the Domain

The following code example uses ADSI with Visual Basic to obtain the distinguished name of the domain that the local computer is a member of by using serverless binding.

[Visual Basic]
Dim rootDSE As IADs
Dim DistinguishedName As String
 
Set rootDSE = GetObject("LDAP://rootDSE")
DistinguishedName = "LDAP://" & rootDSE.Get("defaultNamingContext")

The following code example uses ADSI with C++ to obtain the distinguished name of the domain that the local computer is a member of by using serverless binding.

[C++]
IADs *pads;
hr = ADsGetObject(  L"LDAP://rootDSE",
					IID_IADs, 
					(void**)&pads);

if(SUCCEEDED(hr))
{
	VARIANT var;

	VariantInit(&var);

	hr = pads->Get(CComBSTR("defaultNamingContext"), &var);
	if(SUCCEEDED(hr))
	{
		if(VT_BSTR == var.vt)
		{
			wprintf(var.bstrVal);
	}
	
		VariantClear(&var);
}

	pads->Release();
}