Directory Services |
The DsGetRdnW function retrieves the key and value of the first relative distinguished name and a pointer to the next relative distinguished name from a distinguished name string.
DWORD WINAPI DsGetRdnW( LPCWCH* ppDN, DWORD* pcDN, LPCWCH* ppKey, DWORD* pcKey, LPCWCH* ppVal, DWORD* pcVal );
Return Code | Description |
---|---|
ERROR_DS_NAME_UNPARSEABLE | The distinguished name in the ppDN parameter cannot be parsed. |
ERROR_INVALID_PARAMETER | One or more parameters are invalid. |
The following code example demonstrates how to use the DsGetRdnW function to print all of the relative distinguished names in a distinguished name string.
#include <windows.h> #include <stdio.h> #include <ntdsapi.h> #include <atlbase.h> // Get the rootDSE. HRESULT hr; CComPtr<IADs> spRoot; hr = ADsOpenObject(L"LDAP://rootDSE", NULL, NULL, ADS_SECURE_AUTHENTICATION, IID_IADs, (void**)&spRoot); if(SUCCEEDED(hr)) { CComVariant svar; // Get the DN of the domain the local machine is in. hr = spRoot->Get(CComBSTR(L"defaultNamingContext"), &svar); if(SUCCEEDED(hr)) { LPCWCH pwszDN; LPCWCH pwszKey; LPCWCH pwszVal; DWORD dwDNLen; DWORD dwKeyLen; DWORD dwValLen; pwszDN = svar.bstrVal; dwDNLen = wcslen(pwszDN); while(ERROR_SUCCESS == DsGetRdnW( &pwszDN, &dwDNLen, &pwszKey, &dwKeyLen, &pwszVal, &dwValLen)) { if(0 == dwKeyLen || 0 == dwValLen) { // An error occurred. Attempt to retrieve the next RDN. continue; } LPWSTR pwszTemp = new WCHAR[max(dwKeyLen, dwValLen) + 1]; if(NULL != pwszTemp) { // Copy the key. wcsncpy(pwszTemp, pwszKey, dwKeyLen); // Null-terminate the string. pwszTemp[dwKeyLen] = 0; // Print the key. wprintf(pwszTemp); wprintf(L":"); // Copy the value. wcsncpy(pwszTemp, pwszVal, dwValLen); // Null-terminate the string. pwszTemp[dwValLen] = 0; // Print the value. wprintf(pwszTemp); wprintf(L"\n"); delete pwszTemp; } // Exit the loop when the end of the DN has been reached. if(0 == dwDNLen) { break; } } } spRoot.Release(); }
Client: Included in Windows XP and
Windows 2000 Professional.
Server: Included in Windows Server 2003 and
Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension
on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Ntdsapi.h.
Library: Use Ntdsapi.lib.