搜尋樹系的範例程序代碼
本主題包含搜尋樹系的範例程序代碼。
下列 C/C++ 程式代碼範例會系結至全域編錄的根目錄,並列舉單一物件,也就是樹系的根目錄,以便用來搜尋整個樹系。
Set gc = GetObject("GC:")
For each child in gc
Set entpr = child
Next
' Now entpr is an object that can be used
' to search the entire forest.
下列 C/C++ 程式代碼範例包含一個函式,其會傳回 用來搜尋整個樹系的 IDirectorySearch 指標。
此函式會執行無伺服器系結至全域編錄的根目錄、列舉單一專案,此專案是樹系的根目錄,可用來搜尋整個樹系、呼叫 QueryInterface 以取得物件的 IDirectorySearch 指標,並傳回呼叫端用來搜尋樹系的指標。
HRESULT GetGC(IDirectorySearch **ppDS)
{
HRESULT hr;
IEnumVARIANT *pEnum = NULL;
IADsContainer *pCont = NULL;
VARIANT var;
IDispatch *pDisp = NULL;
ULONG lFetch;
// Set IDirectorySearch pointer to NULL.
*ppDS = NULL;
// First, bind to the GC: namespace container object.
hr = ADsOpenObject(TEXT("GC:"),
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADsContainer,
(void**)&pCont);
if (FAILED(hr)) {
_tprintf(TEXT("ADsOpenObject failed: 0x%x\n"), hr);
goto cleanup;
}
// Get an enumeration interface for the GC container to enumerate the
// contents. The actual GC is the only child of the GC container.
hr = ADsBuildEnumerator(pCont, &pEnum);
if (FAILED(hr)) {
_tprintf(TEXT("ADsBuildEnumerator failed: 0x%x\n"), hr);
goto cleanup;
}
// Now enumerate. There is only one child of the GC: object.
hr = pEnum->Next(1, &var, &lFetch);
if (FAILED(hr)) {
_tprintf(TEXT("ADsEnumerateNext failed: 0x%x\n"), hr);
goto cleanup;
}
// Get the IDirectorySearch pointer.
if (( hr == S_OK ) && ( lFetch == 1 ) )
{
pDisp = V_DISPATCH(&var);
hr = pDisp->QueryInterface( IID_IDirectorySearch, (void**)ppDS);
}
cleanup:
if (pEnum)
ADsFreeEnumerator(pEnum);
if (pCont)
pCont->Release();
if (pDisp)
(pDisp)->Release();
return hr;
}