Beispielcode zum Aktualisieren des Schemacaches
Die folgenden C/C++-Funktionen rufen den DNS-Namen des Schemas master (GetSchemaMasterDNSName) ab und aktualisieren den Schemacache am Schema master (UpdateSchemaMasterCache). Um diese Funktionen zu verwenden, rufen Sie GetSchemaMasterDNSName auf, um das Schema master DNS-Namen abzurufen, übergeben Sie das Schema master DNS-Namen an UpdateSchemaMasterCache, und rufen Sie CoTaskMemFree für die Zeichenfolge auf, die das Schema master DNS-Namen enthält.
Weitere Informationen finden Sie unter Aktualisieren des Schemacaches.
HRESULT UpdateSchemaMasterCache(LPOLESTR szSchemaMasterDNSName)
{
if (!szSchemaMasterDNSName)
return E_POINTER;
HRESULT hr = E_FAIL;
IADs *pObject = NULL;
VARIANT var;
LPOLESTR szPath = new OLECHAR[MAX_PATH];
wcscpy_s(szPath,L"LDAP://");
wcscat_s(szPath,szSchemaMasterDNSName);
wcscat_s(szPath,L"/rootDSE");
hr = ADsOpenObject(szPath,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pObject);
if (SUCCEEDED(hr))
{
VariantInit(&var);
var.vt = VT_I4;
var.lVal = 1L;
hr = pObject->Put(CComBSTR("schemaUpdateNow"), var);
if (SUCCEEDED(hr))
{
hr = pObject->SetInfo();
if (SUCCEEDED(hr))
MessageBox(NULL,L"Updated",L"Extend Schema Wizard",MB_OK|MB_ICONEXCLAMATION);
else
MessageBox(NULL,L"Update Failed",L"Extend Schema Wizard",MB_OK|MB_ICONEXCLAMATION);
}
}
if (pObject)
pObject->Release();
return hr;
}
HRESULT GetSchemaMasterDNSName(LPOLESTR *pszSchemaMasterDNSName)
{
if (!pszSchemaMasterDNSName)
return E_POINTER;
HRESULT hr = E_FAIL;
IADs *pObject = NULL;
IADs *pTempSchema = NULL;
IADs *pNTDS = NULL;
IADs *pServer = NULL;
BSTR bstrParent;
LPOLESTR szPath = new OLECHAR[MAX_PATH];
VARIANT var, varRole,varComputer;
hr = ADsOpenObject(L"LDAP://rootDSE",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pObject);
if (SUCCEEDED(hr))
{
hr = pObject->Get(CComBSTR("schemaNamingContext"), &var);
if (SUCCEEDED(hr))
{
wcscpy_s(szPath,L"LDAP://");
wcscat_s(szPath,var.bstrVal);
hr = ADsOpenObject(szPath,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pTempSchema);
if (SUCCEEDED(hr))
{
// Read the fsmoRoleOwner attribute to see which server is the schema master.
hr = pTempSchema->Get(CComBSTR("fsmoRoleOwner"), &varRole);
if (SUCCEEDED(hr))
{
// fsmoRoleOwner attribute returns the nTDSDSA object.
// The parent is the server object.
// Bind to NTDSDSA object and get the parent object.
wcscpy_s(szPath,L"LDAP://");
wcscat_s(szPath,varRole.bstrVal);
hr = ADsOpenObject(szPath,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pNTDS);
if (SUCCEEDED(hr))
{
hr = pNTDS->get_Parent(&bstrParent);
if (SUCCEEDED(hr))
{
// Bind to server object
// and get the reference to the computer object.
wcscpy_s(szPath,bstrParent);
hr = ADsOpenObject(szPath,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pServer);
if (SUCCEEDED(hr))
{
// Get the dns name of the server.
hr = pServer->Get(CCOmBSTR("dNSHostName"), &varComputer);
if (SUCCEEDED(hr))
{
*pszSchemaMasterDNSName = (OLECHAR *)CoTaskMemAlloc (sizeof(OLECHAR)*(wcslen(varComputer.bstrVal)+1));
if (*pszSchemaMasterDNSName)
{
wcscpy_s(*pszSchemaMasterDNSName, varComputer.bstrVal);
}
else
{
hr = E_OUTOFMEMORY;
}
}
VariantClear(&varComputer);
}
if (pServer)
pServer->Release();
}
SysFreeString(bstrParent);
}
if (pNTDS)
pNTDS->Release();
}
VariantClear(&varRole);
}
if (pTempSchema)
pTempSchema->Release();
}
VariantClear(&var);
}
if (pObject)
pObject->Release();
return hr;
}