IADsObjectOptions 介面
IADsObjectOptions 介面可讓您直接存取設定和擷取提供者特定選項。
其中一個 Active Directory 物件選項是傳回伺服器的主機名。 下列程式代碼範例會使用 介面來擷取全域編錄伺服器的主機名。
HRESULT GetGCServerName(VARIANT *vGCServer)
{
HRESULT hr = S_OK
HRESULT hre = S_OK;
IADsContainer *pContainer = NULL;
IUnknown *pUnk = NULL;
IEnumVARIANT *pEnum = NULL;
IDispatch *pDisp = NULL;
IADsObjectOptions *pOpt = NULL;
VARIANT var;
ULONG lFetch = 0;
VariantInit(&var);
// Bind to the global catalog using a serverless bind.
hr = ADsOpenObject(L"GC:", NULL, NULL,
ADS_SECURE_AUTHENTICATION,
IID_IADsContainer, (void**) &pContainer );
if (FAILED(hr))
return (hr);
hr = pContainer->get__NewEnum(&pUnk);
if (SUCCEEDED(hr))
{
hr = pUnk->QueryInterface(IID_IEnumVARIANT, (void**) &pEnum);
if (SUCCEEDED(hr))
{
// Enumerate.
hr = pEnum->Next(1, &var, &lFetch);
if (SUCCEEDED(hr))
{
while (SUCCEEDED(hr))
{
if (lFetch == 1)
{
pDisp = V_DISPATCH(&var);
hre = pDisp->QueryInterface(
IID_IADsObjectOptions,
(void**)&pOpt);
if (pDisp)
pDisp->Release();
}
VariantClear(&var);
hr = pEnum->Next(1, &var, &lFetch);
}
// S_FALSE indicates that the row was read properly.
if (hr == S_FALSE)
hr = hre;
}
if (SUCCEEDED(hr))
{
// There is a valid pOpt, so request the server name.
VariantInit(vGCServer);
hr = pOpt->GetOption(ADS_OPTION_SERVERNAME,vGCServer);
}
}
}
// Cleanup.
VariantClear(&var);
if (pOpt)
pOpt->Release();
if (pEnum)
pEnum->Release();
if (pUnk)
pUnk->Release();
if (pContainer)
pContainer->Release();
return (hr);
}