2015-06-18 7 views
1

Iam new to C++. С помощью MSDN article iam пытается получить все компьютеры под OU в домене с помощью ADSI. Но Iam получает ошибку 8254L (FILTER_UNKNOWN). Я не уверен, что я здесь делаю неправильно. Я попытался обновить фильтр, но никаких изменений в ошибке. Пожалуйста, предложите, что я делаю неправильно здесь.ADSI Filter Все компьютеры из OU с использованием C++

Ниже приведен код, используемый для получения списка.

// ConsoleApplication3.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <objbase.h> 
#include <wchar.h> 
#include <activeds.h> 
#include <sddl.h> 
#include <comutil.h> 
#include <string.h> 
#include <stdio.h> 


HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search. 
    LPOLESTR szFilter, // Filter to find specific users. 
    // NULL returns all user objects. 
    LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found. 
    // NULL returns all set properties. 
    BOOL bIsVerbose // TRUE indicates that display all properties for the found objects. 
    // FALSE indicates that only the RDN. 
    ); 


// Entry point for the application. 
void wmain(int argc, wchar_t *argv[]) 
{ 
    // Initialize COM. 
    CoInitialize(NULL); 
    HRESULT hr = S_OK; 
    // Get rootDSE and the current user domain container distinguished name. 
    IADs *pObject = NULL; 
    IDirectorySearch *pContainerToSearch = NULL; 
    LPOLESTR szPath = new OLECHAR[MAX_PATH]; 
    BOOL bReturnVerbose = FALSE; 
    DWORD dwLength = MAX_PATH * 2; 
    LPOLESTR pszBuffer = new OLECHAR[dwLength]; 
    VARIANT var; 

    hr = ADsOpenObject(L"LDAP://rootDSE", 
     NULL, 
     NULL, 
     ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. 
     IID_IADs, 
     (void**)&pObject); 
    if (FAILED(hr)) 
    { 
     wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE.\n"); 
     if (pObject) 
      pObject->Release(); 
     return; 
    } 
    if (SUCCEEDED(hr)) 
    { 
     hr = pObject->Get(_bstr_t("defaultNamingContext"), &var); 
     if (SUCCEEDED(hr)) 
     { 
      wprintf(L"bstrVal: %s\n", var.bstrVal); 
      // Build path to the domain container. 

      // wcsncpy_s(szPath, L"LDAP://", MAX_PATH); 
      // wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath)); 


      //hr = ADsOpenObject(szPath, 
      hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=Infosys,DC=iaseries,Dc=local", 
       NULL, 
       NULL, 
       ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. 
       IID_IDirectorySearch, 
       (void**)&pContainerToSearch); 

      if (SUCCEEDED(hr)) 
      { 
       hr = FindComputers(pContainerToSearch, // IDirectorySearch pointer to domainDNS container. 
        pszBuffer, 
        NULL, // Return all properties. 
        bReturnVerbose 
        ); 
       if (SUCCEEDED(hr)) 
       { 
        if (S_FALSE == hr) 
         wprintf(L"Computer object cannot be found.\n"); 
       } 
       else if (E_ADS_INVALID_FILTER == hr) 
        wprintf(L"Cannot execute query. Invalid filter was specified.\n"); 
       else 
        wprintf(L"Query failed to run. HRESULT: %x\n", hr); 

      } 
      else 
      { 
       wprintf(L"Cannot execute query. Cannot bind to the container.\n"); 
      } 
      if (pContainerToSearch) 
       pContainerToSearch->Release(); 

     } 
     VariantClear(&var); 

    } 

    if (pObject) 
     pObject->Release(); 

    // Uninitialize COM. 
    CoUninitialize(); 
    delete[] szPath; 
    delete[] pszBuffer; 

    getchar(); 

} 


HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search. 
    LPOLESTR szFilter, // Filter for finding specific users. 
    // NULL returns all user objects. 
    LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found. 
    // NULL returns all set properties. 
    BOOL bIsVerbose // TRUE indicates that all properties for the found objects are displayed. 
    // FALSE indicates only the RDN. 
    ) 
{ 
    if (!pContainerToSearch) 
     return E_POINTER; 
    DWORD dwLength = (MAX_PATH * 2)+100; 
    // Create search filter. 
    LPOLESTR pszSearchFilter = new OLECHAR[dwLength]; 

    // Add the filter. 
    //swprintf_s(pszSearchFilter, dwLength, L"((objectClass=Computer)%s)", szFilter); 
    swprintf_s(pszSearchFilter, dwLength, L"(&(objectClass=*)(objectCategory=Computer)%s)", szFilter); 


    // Specify subtree search. 
    ADS_SEARCHPREF_INFO SearchPrefs; 
    SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE; 
    SearchPrefs.vValue.dwType = ADSTYPE_INTEGER; 
    SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE; 
    DWORD dwNumPrefs = 1; 

    // COL for iterations. 
    LPOLESTR pszColumn = NULL; 
    ADS_SEARCH_COLUMN col; 
    HRESULT hr = S_OK; 

    // Interface Pointers 
    IADs *pObj = NULL; 
    IADs * pIADs = NULL; 

    // Search handle. 
    ADS_SEARCH_HANDLE hSearch = NULL; 

    // Set search preference. 
    hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs); 
    if (FAILED(hr)) 
     return hr; 

    LPOLESTR pszBool = NULL; 
    DWORD dwBool = 0; 
    PSID pObjectSID = NULL; 
    LPOLESTR szSID = NULL; 
    LPOLESTR szDSGUID = new WCHAR[39]; 
    LPGUID pObjectGUID = NULL; 
    SYSTEMTIME systemtime; 
    DATE date; 
    VARIANT varDate; 
    LPOLESTR *pszPropertyList = NULL; 
    LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" }; 

    LPOLESTR szName = new OLECHAR[MAX_PATH]; 
    LPOLESTR szDN = new OLECHAR[MAX_PATH]; 

    VariantInit(&varDate); 

    int iCount = 0; 
    DWORD x = 0L; 



    if (!bIsVerbose) 
    { 
     // Return non-verbose list properties only. 
     hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, 
      pszNonVerboseList, 
      sizeof(pszNonVerboseList)/sizeof(LPOLESTR), 
      &hSearch 
      ); 
    } 
    else 
    { 
     if (!pszPropertiesToReturn) 
     { 
      // Return all properties. 
      hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, 
       NULL, 
       (DWORD)-1, 
       &hSearch 
       ); 
     } 
     else 
     { 
      // Specified subset. 
      pszPropertyList = pszPropertiesToReturn; 
      // Return specified properties. 
      hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, 
       pszPropertyList, 
       sizeof(pszPropertyList)/sizeof(LPOLESTR), 
       &hSearch 
       ); 
     } 
    } 
    if (SUCCEEDED(hr)) 
    { 
     // Call IDirectorySearch::GetNextRow() to retrieve the next data row. 
     hr = pContainerToSearch->GetFirstRow(hSearch); 
     if (SUCCEEDED(hr)) 
     { 
      while (hr != S_ADS_NOMORE_ROWS) 
      { 
       // Keep track of count. 
       iCount++; 
       if (bIsVerbose) 
        wprintf(L"----------------------------------\n"); 
       // Loop through the array of passed column names, 
       // print the data for each column. 

       while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS) 
       { 
        hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col); 
        if (SUCCEEDED(hr)) 
        { 
         // Print the data for the column and free the column. 
         if (bIsVerbose) 
         { 
          // Get the data for this column. 
          wprintf(L"%s\n", col.pszAttrName); 
          switch (col.dwADsType) 
          { 
          case ADSTYPE_DN_STRING: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            wprintf(L" %s\r\n", col.pADsValues[x].DNString); 
           } 
           break; 
          case ADSTYPE_CASE_EXACT_STRING: 
          case ADSTYPE_CASE_IGNORE_STRING: 
          case ADSTYPE_PRINTABLE_STRING: 
          case ADSTYPE_NUMERIC_STRING: 
          case ADSTYPE_TYPEDNAME: 
          case ADSTYPE_FAXNUMBER: 
          case ADSTYPE_PATH: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            wprintf(L" %s\r\n", col.pADsValues[x].CaseIgnoreString); 
           } 
           break; 
          case ADSTYPE_BOOLEAN: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            dwBool = col.pADsValues[x].Boolean; 
            pszBool = dwBool ? L"TRUE" : L"FALSE"; 
            wprintf(L" %s\r\n", pszBool); 
           } 
           break; 
          case ADSTYPE_INTEGER: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            wprintf(L" %d\r\n", col.pADsValues[x].Integer); 
           } 
           break; 
          case ADSTYPE_OCTET_STRING: 
           if (_wcsicmp(col.pszAttrName, L"objectSID") == 0) 
           { 
            for (x = 0; x< col.dwNumValues; x++) 
            { 
             pObjectSID = (PSID)(col.pADsValues[x].OctetString.lpValue); 
             // Convert SID to string. 
             ConvertSidToStringSid(pObjectSID, &szSID); 
             wprintf(L" %s\r\n", szSID); 
             LocalFree(szSID); 
            } 
           } 
           else if ((_wcsicmp(col.pszAttrName, L"objectGUID") == 0)) 
           { 
            for (x = 0; x< col.dwNumValues; x++) 
            { 
             // Cast to LPGUID. 
             pObjectGUID = (LPGUID)(col.pADsValues[x].OctetString.lpValue); 
             // Convert GUID to string. 
             ::StringFromGUID2(*pObjectGUID, szDSGUID, 39); 
             // Print the GUID. 
             wprintf(L" %s\r\n", szDSGUID); 
            } 
           } 
           else 
            wprintf(L" Value of type Octet String. No Conversion."); 
           break; 
          case ADSTYPE_UTC_TIME: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            systemtime = col.pADsValues[x].UTCTime; 
            if (SystemTimeToVariantTime(&systemtime, 
             &date) != 0) 
            { 
             // Pack in variant.vt. 
             varDate.vt = VT_DATE; 
             varDate.date = date; 
             VariantChangeType(&varDate, &varDate, VARIANT_NOVALUEPROP, VT_BSTR); 
             wprintf(L" %s\r\n", varDate.bstrVal); 
             VariantClear(&varDate); 
            } 
            else 
             wprintf(L" Could not convert UTC-Time.\n", pszColumn); 
           } 
           break; 
          case ADSTYPE_NT_SECURITY_DESCRIPTOR: 
           for (x = 0; x< col.dwNumValues; x++) 
           { 
            wprintf(L" Security descriptor.\n"); 
           } 
           break; 
          default: 
           wprintf(L"Unknown type %d.\n", col.dwADsType); 
          } 
         } 
         else 
         { 
#ifdef _MBCS 
          // Verbose handles only the two single-valued attributes: cn and ldapdisplayname, 
          // so this is a special case. 
          if (0 == wcscmp(L"name", pszColumn)) 
          { 
           //wcscpy_s(szName, col.pADsValues->CaseIgnoreString); 
           szName = col.pADsValues->CaseIgnoreString; 
          } 
          if (0 == wcscmp(L"distinguishedName", pszColumn)) 
          { 
           //wcscpy_s(szDN, col.pADsValues->CaseIgnoreString); 
           szDN = col.pADsValues->CaseIgnoreString; 
          } 
#endif _MBCS 
         } 
         pContainerToSearch->FreeColumn(&col); 
        } 
        FreeADsMem(pszColumn); 
       } 
       if (!bIsVerbose) 
        wprintf(L"%s\n DN: %s\n\n", szName, szDN); 
       // Get the next row. 
       hr = pContainerToSearch->GetNextRow(hSearch); 
      } 

     } 
     // Close the search handle to cleanup. 
     pContainerToSearch->CloseSearchHandle(hSearch); 
    } 
    if (SUCCEEDED(hr) && 0 == iCount) 
     hr = S_FALSE; 

    delete[] szName; 
    delete[] szDN; 
    delete[] szDSGUID; 
    delete[] pszSearchFilter; 
    return hr; 
} 

ответ

0

Iam способна разобраться в проблеме. Ниже приведен код рабочего образца. Я не очень хорошо очищал код. Обновите, если вы собираетесь использовать.

// ConsoleApplication3.cpp : Defines the entry point for the console application. 
 
// 
 

 
#include "stdafx.h" 
 
#include <objbase.h> 
 
#include <wchar.h> 
 
#include <activeds.h> 
 
#include <sddl.h> 
 
#include <comutil.h> 
 
#include <string.h> 
 
#include <stdio.h> 
 

 

 
HRESULT FindComputers(IDirectorySearch *pContainerToSearch); // IDirectorySearch pointer to the container to search. 
 
\t 
 

 

 
// Entry point for the application. 
 
void wmain(int argc, wchar_t *argv[]) 
 
{ 
 
\t // Initialize COM. 
 
\t CoInitialize(NULL); 
 
\t HRESULT hr = S_OK; 
 
\t // Get rootDSE and the current user domain container distinguished name. 
 
\t IADs *pObject = NULL; 
 
\t IDirectorySearch *pContainerToSearch = NULL; 
 
\t LPOLESTR szPath = new OLECHAR[MAX_PATH]; 
 
\t BOOL bReturnVerbose = FALSE; 
 
\t DWORD dwLength = MAX_PATH * 2; 
 
\t VARIANT var; 
 
\t 
 
\t hr = ADsOpenObject(L"LDAP://rootDSE", 
 
\t \t NULL, 
 
\t \t NULL, 
 
\t \t ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. 
 
\t \t IID_IADs, 
 
\t \t (void**)&pObject); 
 
\t if (FAILED(hr)) 
 
\t { 
 
\t \t wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE.\n"); 
 
\t \t if (pObject) 
 
\t \t \t pObject->Release(); 
 
\t \t return; 
 
\t } 
 
\t if (SUCCEEDED(hr)) 
 
\t { 
 
\t \t hr = pObject->Get(_bstr_t("defaultNamingContext"), &var); 
 
\t \t if (SUCCEEDED(hr)) 
 
\t \t { 
 
\t \t \t //wprintf(L"bstrVal: %s\n", var.bstrVal); 
 
\t \t \t 
 
\t \t \t // Build path to the domain container. 
 
\t \t // wcsncpy_s(szPath, L"LDAP://", MAX_PATH); 
 
\t \t // wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath)); 
 
\t \t \t 
 
\t \t  
 
\t \t //hr = ADsOpenObject(szPath, 
 
\t \t hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=MyDept,DC=Test,Dc=com", 
 
\t \t \t \t NULL, 
 
\t \t \t \t NULL, 
 
\t \t \t \t ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. 
 
\t \t \t \t IID_IDirectorySearch, 
 
\t \t \t \t (void**)&pContainerToSearch); 
 

 
\t \t \t if (SUCCEEDED(hr)) 
 
\t \t \t { 
 
\t \t \t \t hr = FindComputers(pContainerToSearch); // IDirectorySearch pointer to domainDNS container. 
 
\t \t \t \t \t 
 
\t \t \t \t if (SUCCEEDED(hr)) 
 
\t \t \t \t { 
 
\t \t \t \t \t if (S_FALSE == hr) 
 
\t \t \t \t \t \t wprintf(L"Computer object cannot be found.\n"); 
 
\t \t \t \t } 
 
\t \t \t \t else if (E_ADS_INVALID_FILTER == hr) 
 
\t \t \t \t \t wprintf(L"Cannot execute query. Invalid filter was specified.\n"); 
 
\t \t \t \t else 
 
\t \t \t \t \t wprintf(L"Query failed to run. HRESULT: %x\n", hr); 
 

 
\t \t \t } 
 
\t \t \t else 
 
\t \t \t { 
 
\t \t \t \t wprintf(L"Cannot execute query. Cannot bind to the container.\n"); 
 
\t \t \t } 
 
\t \t \t if (pContainerToSearch) 
 
\t \t \t \t pContainerToSearch->Release(); 
 

 
\t \t } 
 
\t \t VariantClear(&var); 
 

 
\t } 
 

 
\t if (pObject) 
 
\t \t pObject->Release(); 
 

 
\t // Uninitialize COM. 
 
\t CoUninitialize(); 
 
\t delete[] szPath; 
 

 
\t getchar(); 
 
\t 
 
} 
 

 

 
HRESULT FindComputers(IDirectorySearch *pContainerToSearch) // IDirectorySearch pointer to the container to search. 
 
{ 
 
\t if (!pContainerToSearch) 
 
\t \t return E_POINTER; 
 
\t DWORD dwLength = (MAX_PATH * 2); 
 
\t // Create search filter. 
 
\t LPOLESTR pszSearchFilter = new OLECHAR[dwLength]; 
 

 
\t // Add the filter. 
 
\t pszSearchFilter = L"((objectCategory=computer))"; 
 
\t 
 
\t // Specify subtree search. 
 
\t ADS_SEARCHPREF_INFO SearchPrefs; 
 
\t SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE; 
 
\t SearchPrefs.vValue.dwType = ADSTYPE_INTEGER; 
 
\t SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE; 
 
\t DWORD dwNumPrefs = 1; 
 

 
\t // COL for iterations. 
 
\t LPOLESTR pszColumn = NULL; 
 
\t ADS_SEARCH_COLUMN col; 
 
\t HRESULT hr = S_OK; 
 

 
\t // Interface Pointers 
 
\t IADs *pObj = NULL; 
 
\t IADs * pIADs = NULL; 
 

 
\t // Search handle. 
 
\t ADS_SEARCH_HANDLE hSearch = NULL; 
 

 
\t // Set search preference. 
 
\t hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs); 
 
\t if (FAILED(hr)) 
 
\t \t return hr; 
 

 
\t LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" }; 
 

 
\t LPOLESTR szName = new OLECHAR[MAX_PATH]; 
 
\t LPOLESTR szDN = new OLECHAR[MAX_PATH]; 
 
\t \t 
 
\t int iCount = 0; 
 
\t DWORD x = 0L; 
 
\t 
 

 
\t // Return non-verbose list properties only. 
 
\t hr = pContainerToSearch->ExecuteSearch(pszSearchFilter, 
 
\t \t pszNonVerboseList, 
 
\t \t sizeof(pszNonVerboseList)/sizeof(LPOLESTR), 
 
\t \t &hSearch 
 
\t \t); 
 

 
\t if (SUCCEEDED(hr)) 
 
\t { 
 
\t \t // Call IDirectorySearch::GetNextRow() to retrieve the next data row. 
 
\t \t hr = pContainerToSearch->GetFirstRow(hSearch); 
 
\t \t if (SUCCEEDED(hr)) 
 
\t \t { 
 
\t \t \t while (hr != S_ADS_NOMORE_ROWS) 
 
\t \t \t { 
 
\t \t \t \t // Keep track of count. 
 
\t \t \t \t iCount++; 
 
\t \t \t \t 
 
\t \t \t \t // Loop through the array of passed column names, 
 
\t \t \t \t // print the data for each column. 
 

 
\t \t \t \t while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS) 
 
\t \t \t \t { 
 
\t \t \t \t \t hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col); 
 
\t \t \t \t \t if (SUCCEEDED(hr)) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t // Verbose handles only the two single-valued attributes: cn and ldapdisplayname, 
 
\t \t \t \t \t \t // so this is a special case. 
 
\t \t \t \t \t \t if (0 == wcscmp(L"name", pszColumn)) 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t //wcscpy_s(szName, col.pADsValues->CaseIgnoreString); 
 
\t \t \t \t \t \t \t szName = col.pADsValues->CaseIgnoreString; 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t if (0 == wcscmp(L"distinguishedName", pszColumn)) 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t //wcscpy_s(szDN, col.pADsValues->CaseIgnoreString); 
 
\t \t \t \t \t \t \t szDN = col.pADsValues->CaseIgnoreString; 
 
\t \t \t \t \t \t } 
 
\t \t \t 
 
\t \t \t \t \t \t pContainerToSearch->FreeColumn(&col); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t FreeADsMem(pszColumn); 
 
\t \t \t \t } 
 
\t \t \t \t 
 
\t \t \t \t wprintf(L"%s\n DN: %s\n\n", szName, szDN); 
 

 
\t \t \t \t // Get the next row. 
 
\t \t \t \t hr = pContainerToSearch->GetNextRow(hSearch); 
 
\t \t \t } 
 

 
\t \t } 
 
\t \t // Close the search handle to cleanup. 
 
\t \t pContainerToSearch->CloseSearchHandle(hSearch); 
 
\t } 
 
\t if (SUCCEEDED(hr) && 0 == iCount) 
 
\t \t hr = S_FALSE; 
 

 
\t delete[] szName; 
 
\t delete[] szDN; 
 
\t delete[] pszSearchFilter; 
 
\t return hr; 
 
}

Спасибо, Виджай

 Смежные вопросы

  • Нет связанных вопросов^_^