Exemple de code pour la liaison à des objets connus
L’exemple de code C++ suivant montre comment établir une liaison à un objet connu à l’aide de la chaîne de liaison WKGUID.
//*********************************************************
//
// BindToWellKnownObject()
//
// Binds to one of the well-known objects in the current domain
// with the current user 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)
{
swprintf_s(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;
}