Finding the Exchange Organization Name Using ADSI
Finding the Exchange Organization Name Using ADSI
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release. When you are compiling the following example, you must also link with activeds.lib and adsiid.lib.
Visual C++
// Find the Exchange Organization Name Using ADSI // Find the Exchange organization name by searching Active Directory. // This can be run from any DSClient-enabled computer. #include <activeds.h> #include <stdio.h> void main() { CoInitialize(NULL); HRESULT hr = S_OK; // Get rootDSE and the distinguished name of the configuration container. IADs *pADs = NULL; IDirectorySearch *pDirSrch = NULL; LPOLESTR szPath = new OLECHAR[MAX_PATH]; VARIANT var; hr = ADsOpenObject(L"LDAP://rootDSE", NULL, NULL, // Use secure authentication. ADS_SECURE_AUTHENTICATION, IID_IADs, (void**)&pADs); if (FAILED(hr)) { wprintf(L"Not Found. Could not bind to the domain.\n"); if (pADs) pADs->Release(); return; } hr = pADs->Get(L"configurationNamingContext",&var); if (SUCCEEDED(hr)) { wcscpy(szPath,L"LDAP://"); wcscat(szPath,var.bstrVal); VariantClear(&var); // Bind to the root of the configuration context. hr = ADsOpenObject(szPath, NULL, NULL, // Use secure authentication. ADS_SECURE_AUTHENTICATION, IID_IDirectorySearch, (void**)&pDirSrch); if (SUCCEEDED(hr)) { HRESULT hr = E_FAIL; // Create the search filter. LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH]; LPOLESTR szADsPath = new OLECHAR[MAX_PATH]; wcscpy(pszSearchFilter, L"(objectCategory=msExchOrganizationContainer)"); // Search the entire subtree from the root. ADS_SEARCHPREF_INFO pSearchPrefs; pSearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE; pSearchPrefs.vValue.dwType = ADSTYPE_INTEGER; pSearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE; DWORD dwNumPrefs = 1; // COL for iterations ADS_SEARCH_COLUMN col; // Handle used for searching ADS_SEARCH_HANDLE hSearch; // Set the search preference. hr = pDirSrch->SetSearchPreference( &pSearchPrefs, dwNumPrefs); if (FAILED(hr)) return; // Set attributes to return. CONST DWORD dwAttrNameSize = 1; LPOLESTR pszAttribute[dwAttrNameSize] = {L"cn"}; // Execute the search. hr = pDirSrch->ExecuteSearch(pszSearchFilter, pszAttribute, dwAttrNameSize, &hSearch ); if (SUCCEEDED(hr)) { // Call IDirectorySearch::GetNextRow() to retrieve the next row of data. while( pDirSrch->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS ) { // Loop through the array of passed column names and // print the data for each column. for (DWORD x = 0; x < dwAttrNameSize; x++) { // Get the data for this column. hr = pDirSrch->GetColumn( hSearch, pszAttribute[x], &col ); if ( SUCCEEDED(hr) ) { wprintf(L"Organization name is: %s\n",col.pADsValues->CaseIgnoreString); } pDirSrch->FreeColumn( &col ); } } } // Close the search handle to clean up. pDirSrch->CloseSearchHandle(hSearch); } } if (pADs) { pADs->Release(); pADs = NULL; } if (pDirSrch) { pDirSrch->Release(); pDirSrch = NULL; } CoUninitialize(); }
Send us your feedback about the Microsoft Exchange Server 2003 SDK.
Build: June 2007 (2007.618.1)
© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.