Directory Services |
The following code example shows how to bind to a well-known object using the WKGUID binding string.
//*************************************************************************** // // BindToWellKnownObject() // // Binds to one of the well known objects in the current domain with the // current user's credentials. pwszGUID must be one of the GUID strings // defined in NTDSAPI.H, such as GUID_USERS_CONTAINER_W. // //*************************************************************************** HRESULT BindToWellKnownObject(LPCWSTR pwszGUID, IADs **ppObject) { if(NULL == ppObject) { return E_INVALIDARG; } HRESULT hr; IADs *pRoot; *ppObject = NULL; // Bind to the rootDSE object. hr = ADsOpenObject(L"LDAP://rootDSE", NULL, NULL, ADS_SECURE_AUTHENTICATION, IID_IADs, (LPVOID*)&pRoot); if(SUCCEEDED(hr)) { VARIANT var; VariantInit(&var); // Get the current domain DN. hr = pRoot->Get(CComBSTR("defaultNamingContext"), &var); if(SUCCEEDED(hr)) { // Build the binding string. LPWSTR pwszFormat = L"LDAP://<WKGUID=%s,%s>"; LPWSTR pwszPath; pwszPath = new WCHAR[lstrlenW(pwszFormat) + lstrlenW(pwszGUID) + lstrlenW(var.bstrVal)]; if(NULL != pwszPath) { wsprintfW(pwszPath, pwszFormat, pwszGUID, var.bstrVal); // Bind to the object. hr = ADsOpenObject(pwszPath, NULL, NULL, ADS_SECURE_AUTHENTICATION, IID_IADs, (LPVOID*)ppObject); delete pwszPath; } else { hr = E_OUTOFMEMORY; } VariantClear(&var); } pRoot->Release(); } return hr; }