網域瀏覽器

使用 IDsBrowseDomainTree 介面,應用程式可以顯示網域瀏覽器對話方塊,並取得使用者所選取網域的 DNS 名稱。 應用程式也可以使用 IDsBrowseDomainTree 介面來取得樹系內所有網域樹狀結構與網域的相關數據。

IDsBrowseDomainTree 介面的實例是藉由使用 CLSID_DsDomainTreeBrowser 類別標識符呼叫 CoCreateInstance 來建立,如下所示。

IDsBrowseDomainTree::SetComputer 方法可用來指定哪些計算機和認證是用來擷取網域數據的基礎。 在特定 IDsBrowseDomainTree 實例上呼叫 SetComputer必須先呼叫 IDsBrowseDomainTree::FlushCachedDomains才能再次呼叫 SetComputer。

IDsBrowseDomainTree ::BrowseTo 方法可用來顯示網域瀏覽器對話方塊。 當使用者選取網域並按下 [確定] 按鈕時,IDsBrowseDomainTree::BrowseTo 會傳回S_OK,而 ppszTargetPath 參數包含所選網域的名稱。 當不再需要名稱字串時,呼叫端必須藉由呼叫 CoTaskMemFree 釋放字串。

下列程式代碼範例示範如何使用 IDsBrowseDomainTree 介面來顯示網域瀏覽器對話方塊。

#include <shlobj.h>
#include <dsclient.h>

void main(void)
{
    HRESULT     hr;

    hr = CoInitialize(NULL);
    if(FAILED(hr)) 
    {
        return;
    }

    IDsBrowseDomainTree *pDsDomains = NULL;

    hr = ::CoCreateInstance(    CLSID_DsDomainTreeBrowser,
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IDsBrowseDomainTree,
                                (void **)(&pDsDomains));
    
    if(SUCCEEDED(hr))
    {
        LPOLESTR    pstr;        

        hr = pDsDomains->BrowseTo(  GetDesktopWindow(),
                                    &pstr,
                                    0);
        
        if(S_OK == hr)
        {
            wprintf(pstr);
            wprintf(L"\n");
            CoTaskMemFree(pstr);
        }

        pDsDomains->Release();
    }

    CoUninitialize();
}

IDsBrowseDomainTree ::GetDomains 方法可用來取得網域樹狀結構數據。 網域數據是在 DOMAINTREE 結構中提供。 DOMAINTREE 結構包含結構的大小和樹狀結構中的定義域元素數目。 DOMAINTREE 結構也包含一或多個 DOMAINDESC 結構。 DOMAINDESC 包含定義域樹狀結構中單一元素的相關數據,包括子系和同層級數據。 您可以藉由存取每個後續 DOMAINDESC 結構的 pdNextSibling 成員來列舉網域的同層級。 存取每個後續 DOMAINDESC 結構的 pdChildList 成員,即可以類似的方式擷取網域的子系。

下列程式代碼範例示範如何使用 IDsBrowseDomainTree::GetDomains 方法來取得和存取網域樹狀結構數據

//  Add dsuiext.lib to the project.

#include "stdafx.h"
#include <shlobj.h>
#include <dsclient.h>

//The PrintDomain() function displays the domain name
void PrintDomain(DOMAINDESC *pDomainDesc, DWORD dwIndentLevel)
{
    DWORD i;

    // Display the domain name.
    for(i = 0; i < dwIndentLevel; i++)
    {
        wprintf(L"  ");
    }
    wprintf(pDomainDesc->pszName);
    wprintf(L"\n");
}

//The WalkDomainTree() function traverses the domain tree and prints the current domain name
void WalkDomainTree(DOMAINDESC *pDomainDesc, DWORD dwIndentLevel = 0)
{
    DOMAINDESC  *pCurrent;

    // Walk through the current item and any siblings it may have.
    for(pCurrent = pDomainDesc; 
        NULL != pCurrent; 
        pCurrent = pCurrent->pdNextSibling)
    {
        // Print the current domain name.
        PrintDomain(pCurrent, dwIndentLevel);

        // Walk the child tree, if one exists.
        if(NULL != pCurrent->pdChildList)
        {
            WalkDomainTree(pCurrent->pdChildList, 
                           dwIndentLevel + 1);
        }
    }
}

//  Entry point for application
int main(void)
{
    HRESULT hr;
    IDsBrowseDomainTree *pBrowseTree;
    CoInitialize(NULL);

    hr = CoCreateInstance(CLSID_DsDomainTreeBrowser,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IDsBrowseDomainTree,
                          (void**)&pBrowseTree);

    if(SUCCEEDED(hr))
    {
        DOMAINTREE  *pDomTreeStruct;
        hr = pBrowseTree->GetDomains(&pDomTreeStruct, 
                                     DBDTF_RETURNFQDN);
        if(SUCCEEDED(hr))
        {
            WalkDomainTree(&pDomTreeStruct->aDomains[0]);
            hr = pBrowseTree->FreeDomains(&pDomTreeStruct);
        }
        pBrowseTree->Release();
    }
    CoUninitialize();
}