系結至已知物件的範例程序代碼
下列 C++ 程式代碼範例示範如何使用 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;
}