Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2192 lines
62 KiB

  1. // adsqryDoc.cpp : implementation of the CAdsqryDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "adsqDoc.h"
  5. #include "adsdsrc.h"
  6. #include "oledberr.h"
  7. #include "adsdb.h"
  8. #include "csyntax.h"
  9. #include "newquery.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #ifdef BUILD_FOR_NT40
  16. typedef DWORD DWORD_PTR;
  17. #endif
  18. /***********************************************************
  19. Function:
  20. Arguments:
  21. Return:
  22. Purpose:
  23. Author(s):
  24. Revision:
  25. Date:
  26. ***********************************************************/
  27. CADsDataSource::CADsDataSource( )
  28. {
  29. m_pSearchPref = NULL;
  30. }
  31. /***********************************************************
  32. Function:
  33. Arguments:
  34. Return:
  35. Purpose:
  36. Author(s):
  37. Revision:
  38. Date:
  39. ***********************************************************/
  40. CADsDataSource::~CADsDataSource( )
  41. {
  42. if( NULL != m_pSearchPref )
  43. {
  44. FreeADsMem( m_pSearchPref );
  45. }
  46. }
  47. /***********************************************************
  48. Function: CADsDataSource::SetQueryParameters
  49. Arguments:
  50. Return:
  51. Purpose:
  52. Author(s):
  53. Revision:
  54. Date:
  55. ***********************************************************/
  56. /*void CADsDataSource::SetQueryParameters( CString& strSource,
  57. CString& strQuery,
  58. CString& strAttributes,
  59. CString& strScope,
  60. CString& strUserName,
  61. CString& strPassword,
  62. BOOL bEncryptPassword,
  63. BOOL bUseSQL )
  64. {
  65. m_strSource = strSource;
  66. m_strQuery = strQuery;
  67. m_strAttributes = strAttributes;
  68. m_strScope = strScope;
  69. m_strUserName = strUserName;
  70. m_strPassword = strPassword;
  71. m_bEncryptPassword = bEncryptPassword;
  72. m_bUseSQL = bUseSQL;
  73. } */
  74. void CADsDataSource::SetQueryParameters( SEARCHPREF* pSearchPref )
  75. {
  76. if( NULL == m_pSearchPref )
  77. {
  78. m_pSearchPref = (SEARCHPREF*) AllocADsMem( sizeof(SEARCHPREF) );
  79. }
  80. *m_pSearchPref = *pSearchPref;
  81. }
  82. /***********************************************************
  83. Function: CADsDataSource::GetColumnsCount
  84. Arguments:
  85. Return:
  86. Purpose:
  87. Author(s):
  88. Revision:
  89. Date:
  90. ***********************************************************/
  91. int CADsDataSource::GetColumnsCount( int nRow )
  92. {
  93. ASSERT( FALSE );
  94. return 0;
  95. }
  96. /***********************************************************
  97. Function: CADsDataSource::GetColumnsCount
  98. Arguments:
  99. Return:
  100. Purpose:
  101. Author(s):
  102. Revision:
  103. Date:
  104. ***********************************************************/
  105. BOOL CADsDataSource::GetValue( int nRow, int nColumn, CString& )
  106. {
  107. return FALSE;
  108. }
  109. /***********************************************************
  110. Function: CADsDataSource::GetColumnsCount
  111. Arguments:
  112. Return:
  113. Purpose:
  114. Author(s):
  115. Revision:
  116. Date:
  117. ***********************************************************/
  118. BOOL CADsDataSource::GetValue( int nRow, CString& strColumn, CString& )
  119. {
  120. return FALSE;
  121. }
  122. /***********************************************************
  123. Function: CADsDataSource::GetColumnsCount
  124. Arguments:
  125. Return:
  126. Purpose:
  127. Author(s):
  128. Revision:
  129. Date:
  130. ***********************************************************/
  131. BOOL CADsDataSource::GetADsPath( int nRow, CString& )
  132. {
  133. ASSERT( FALSE );
  134. return FALSE;
  135. }
  136. /***********************************************************
  137. Function: CADsDataSource::GetColumnsCount
  138. Arguments:
  139. Return:
  140. Purpose:
  141. Author(s):
  142. Revision:
  143. Date:
  144. ***********************************************************/
  145. BOOL CADsDataSource::GetColumnText( int nRow, int nColumn, CString& )
  146. {
  147. ASSERT( FALSE );
  148. return FALSE;
  149. }
  150. /***********************************************************
  151. Function: CADsDataSource::GetColumnsCount
  152. Arguments:
  153. Return:
  154. Purpose:
  155. Author(s):
  156. Revision:
  157. Date:
  158. ***********************************************************/
  159. BOOL CADsDataSource::Refresh( void )
  160. {
  161. return FALSE;
  162. }
  163. /***********************************************************
  164. Function: CADsDataSource::GetColumnsCount
  165. Arguments:
  166. Return:
  167. Purpose:
  168. Author(s):
  169. Revision:
  170. Date:
  171. ***********************************************************/
  172. BOOL CADsDataSource::RunTheQuery( void )
  173. {
  174. ASSERT( FALSE );
  175. return FALSE;
  176. }
  177. /*****************************************************************************
  178. Function: CADsOleDBDataSource::CADsOleDBDataSource
  179. Arguments:
  180. Return:
  181. Purpose:
  182. Author(s):
  183. Revision:
  184. Date:
  185. ***********************************************************/
  186. CADsOleDBDataSource::CADsOleDBDataSource( )
  187. {
  188. CoGetMalloc(MEMCTX_TASK, &m_pIMalloc);
  189. m_pData = NULL;
  190. m_pBindStatus = NULL;
  191. m_hRows = NULL;
  192. m_pIRowset = NULL;
  193. m_pIColsInfo = NULL;
  194. m_pAccessor = NULL;
  195. m_nColumnsCount = 0;
  196. m_prgColInfo = NULL;
  197. m_szColNames = NULL;
  198. }
  199. /*****************************************************************************
  200. Function: CADsOleDBDataSource::~CADsOleDBDataSource
  201. Arguments:
  202. Return:
  203. Purpose:
  204. Author(s):
  205. Revision:
  206. Date:
  207. ***********************************************************/
  208. CADsOleDBDataSource::~CADsOleDBDataSource( )
  209. {
  210. DestroyInternalData( );
  211. m_pIMalloc->Release( );
  212. }
  213. /*****************************************************************************
  214. Function: CADsOleDBDataSource::GetColumnsCount
  215. Arguments:
  216. Return:
  217. Purpose:
  218. Author(s):
  219. Revision:
  220. Date:
  221. ***********************************************************/
  222. int CADsOleDBDataSource::GetColumnsCount( int nRow )
  223. {
  224. if( m_nColumnsCount )
  225. {
  226. return (int)(m_nColumnsCount - m_nAddOne);
  227. }
  228. else
  229. {
  230. return 0;
  231. }
  232. }
  233. /*****************************************************************************
  234. Function: CADsOleDBDataSource::GetColumnText
  235. Arguments:
  236. Return:
  237. Purpose:
  238. Author(s):
  239. Revision:
  240. Date:
  241. ***********************************************************/
  242. BOOL CADsOleDBDataSource::GetColumnText( int nRow, int nColumn, CString& rValue )
  243. {
  244. nColumn += m_nAddOne;
  245. if( nColumn < 0 || nColumn >= (int)m_nColumnsCount )
  246. {
  247. ASSERT( FALSE );
  248. return FALSE;
  249. }
  250. if( FALSE )
  251. {
  252. rValue = _T("ADsPath");
  253. }
  254. else
  255. {
  256. TCHAR szColumnName[ MAX_PATH ];
  257. Convert( szColumnName, m_prgColInfo[ nColumn + 1].pwszName );
  258. rValue = szColumnName;
  259. }
  260. return TRUE;
  261. }
  262. /*****************************************************************************
  263. Function: CADsOleDBDataSource::CreateAccessorHelper
  264. Arguments:
  265. Return:
  266. Purpose:
  267. Author(s):
  268. Revision:
  269. Date:
  270. *****************************************************************************/
  271. BOOL CADsOleDBDataSource::CreateAccessorHelp( void )
  272. {
  273. DBBINDING* prgBindings = NULL;
  274. HRESULT hResult;
  275. ULONG i;
  276. IAccessor* pIAccessor = NULL;
  277. prgBindings = (DBBINDING *) LocalAlloc(
  278. LPTR,
  279. sizeof(DBBINDING) * (m_nColumnsCount)
  280. );
  281. if(NULL == prgBindings)
  282. return FALSE; //SMITHA return E_OUTOFMEMORY;
  283. //
  284. // Set up rest of the attributes
  285. //
  286. for ( i=0; i < m_nColumnsCount ; i++)
  287. {
  288. prgBindings[i].iOrdinal = i+1;
  289. prgBindings[i].obValue = sizeof(Data)*i + offsetof(Data, obValue);
  290. prgBindings[i].obLength = sizeof(Data)*i + offsetof(Data, obLength);
  291. prgBindings[i].obStatus = sizeof(Data)*i + offsetof(Data, status);
  292. prgBindings[i].dwPart = DBPART_VALUE|DBPART_LENGTH|DBPART_STATUS;
  293. //prgBindings[i].dwMemOwner = DBMEMOWNER_PROVIDEROWNED;
  294. prgBindings[i].wType = m_prgColInfo[i+1].wType;
  295. prgBindings[i].dwFlags = 0;
  296. if( prgBindings[i].wType & DBTYPE_BYREF )
  297. {
  298. prgBindings[i].dwMemOwner = DBMEMOWNER_PROVIDEROWNED;
  299. }
  300. else
  301. {
  302. prgBindings[i].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
  303. }
  304. if( m_prgColInfo[i+1].wType == DBTYPE_DATE ||
  305. m_prgColInfo[i+1].wType == DBTYPE_I8 )
  306. {
  307. prgBindings[i].obValue = sizeof(Data)*i + offsetof(Data, obValue2);
  308. }
  309. }
  310. hResult= m_pIRowset->QueryInterface( IID_IAccessor, (void**) &pIAccessor );
  311. ASSERT( SUCCEEDED( hResult ) );
  312. //
  313. // With the bindings create the accessor
  314. //
  315. hResult = pIAccessor->CreateAccessor(
  316. DBACCESSOR_ROWDATA,
  317. m_nColumnsCount,
  318. prgBindings,
  319. 0,
  320. &m_hAccessor,
  321. m_pBindStatus
  322. );
  323. ASSERT( SUCCEEDED( hResult ) );
  324. if( FAILED( hResult ) )
  325. {
  326. AfxMessageBox( _T("IAccessor::CreateAccessor failed") );
  327. }
  328. pIAccessor->Release();
  329. LocalFree(prgBindings);
  330. return( SUCCEEDED( hResult ) );
  331. }
  332. /*****************************************************************************
  333. Function: CADsOleDBDataSource::SetQueryCredentials
  334. Arguments:
  335. Return:
  336. Purpose:
  337. Author(s):
  338. Revision:
  339. Date:
  340. *****************************************************************************/
  341. HRESULT CADsOleDBDataSource::SetQueryCredentials( IDBInitialize* pInit,
  342. ICommandText* pCommand )
  343. {
  344. DBPROPSET aPropSet;
  345. DBPROP arrProp[ 20 ];
  346. HRESULT hResult;
  347. int nIter, nIdx;
  348. ASSERT( !( (NULL != pInit) && (NULL != pCommand) ) );
  349. for ( nIter = 0; nIter < 20; nIter++)
  350. {
  351. VariantInit( &(arrProp[ nIter ].vValue ) );
  352. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_BSTR;
  353. arrProp[ nIter ].dwOptions = DBPROPOPTIONS_REQUIRED;
  354. }
  355. if( NULL != pInit )
  356. {
  357. IDBProperties* pIProp = NULL;
  358. if( !_tcslen(m_pSearchPref->szUserName) )
  359. {
  360. return S_OK;
  361. }
  362. hResult = pInit->QueryInterface( IID_IDBProperties, (void**)&pIProp );
  363. ASSERT( SUCCEEDED( hResult ) );
  364. if( FAILED( hResult ) )
  365. return hResult;
  366. nIter = 0;
  367. if( _tcslen(m_pSearchPref->szUserName) )
  368. {
  369. arrProp[ nIter ].dwPropertyID = DBPROP_AUTH_USERID;
  370. V_BSTR( &(arrProp[ nIter ].vValue) ) = AllocBSTR( m_pSearchPref->szUserName );
  371. nIter++;
  372. arrProp[ nIter ].dwPropertyID = DBPROP_AUTH_PASSWORD;
  373. V_BSTR( &(arrProp[ nIter ].vValue) ) = AllocBSTR( m_pSearchPref->szPassword );
  374. nIter++;
  375. arrProp[ nIter ].dwPropertyID = DBPROP_AUTH_ENCRYPT_PASSWORD;
  376. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_BOOL;
  377. if( m_pSearchPref->bEncryptPassword )
  378. {
  379. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_TRUE;
  380. }
  381. else
  382. {
  383. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_FALSE;
  384. }
  385. nIter++;
  386. aPropSet.rgProperties = arrProp;
  387. aPropSet.cProperties = nIter;
  388. aPropSet.guidPropertySet = DBPROPSET_DBINIT;
  389. hResult = pIProp->SetProperties( 1, &aPropSet );
  390. ASSERT( S_OK == hResult );
  391. for ( nIdx = 0; nIdx < nIter ; nIdx++ )
  392. {
  393. VariantClear( &(arrProp[ nIdx ].vValue ) );
  394. }
  395. nIter = 0;
  396. }
  397. pIProp->Release( );
  398. }
  399. if( NULL != pCommand )
  400. {
  401. ICommandProperties* pCommandProp = NULL;
  402. hResult = pCommand->QueryInterface( IID_ICommandProperties,
  403. (void**) &pCommandProp );
  404. ASSERT( SUCCEEDED( hResult ) );
  405. if( FAILED( hResult ) )
  406. return hResult;
  407. nIter = 0;
  408. // Initialize the VARIANTs and the options in rgProps.
  409. //***************************************************************************
  410. if( -1 != m_pSearchPref->nAsynchronous )
  411. {
  412. arrProp[ nIter ].dwPropertyID = ADSIPROP_ASYNCHRONOUS;
  413. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_BOOL;
  414. if( m_pSearchPref->nAsynchronous )
  415. {
  416. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_TRUE;
  417. }
  418. else
  419. {
  420. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_FALSE;
  421. }
  422. nIter++;
  423. }
  424. //***************************************************************************
  425. if( -1 != m_pSearchPref->nAttributesOnly )
  426. {
  427. arrProp[ nIter ].dwPropertyID = ADSIPROP_ATTRIBTYPES_ONLY;
  428. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_BOOL;
  429. if( m_pSearchPref->nAttributesOnly )
  430. {
  431. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_TRUE;
  432. }
  433. else
  434. {
  435. V_BOOL( &(arrProp[ nIter ].vValue) ) = VARIANT_FALSE;
  436. }
  437. nIter++;
  438. }
  439. //***************************************************************************
  440. if( -1 != m_pSearchPref->nTimeOut )
  441. {
  442. arrProp[ nIter ].dwPropertyID = ADSIPROP_TIMEOUT;
  443. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  444. V_I4( &(arrProp[ nIter ].vValue ) ) = m_pSearchPref->nTimeOut;
  445. nIter++;
  446. }
  447. //***************************************************************************
  448. if( -1 != m_pSearchPref->nTimeLimit )
  449. {
  450. arrProp[ nIter ].dwPropertyID = ADSIPROP_TIME_LIMIT;
  451. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  452. V_I4( &(arrProp[ nIter ].vValue ) ) = m_pSearchPref->nTimeLimit;
  453. nIter++;
  454. }
  455. //***************************************************************************
  456. if( -1 != m_pSearchPref->nSizeLimit )
  457. {
  458. arrProp[ nIter ].dwPropertyID = ADSIPROP_SIZE_LIMIT;
  459. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  460. V_I4( &(arrProp[ nIter ].vValue ) ) = m_pSearchPref->nSizeLimit;
  461. nIter++;
  462. }
  463. //***************************************************************************
  464. if( -1 != m_pSearchPref->nPageSize )
  465. {
  466. arrProp[ nIter ].dwPropertyID = ADSIPROP_PAGESIZE;
  467. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  468. V_I4( &(arrProp[ nIter ].vValue ) ) = m_pSearchPref->nPageSize;
  469. nIter++;
  470. }
  471. //***************************************************************************
  472. if( _tcslen( m_pSearchPref->szScope ) )
  473. {
  474. arrProp[ nIter ].dwPropertyID = ADSIPROP_SEARCH_SCOPE;
  475. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  476. if( !_tcsicmp( m_pSearchPref->szScope, _T("subtree") ) )
  477. {
  478. V_I4( &(arrProp[ nIter ].vValue ) ) = ADS_SCOPE_SUBTREE;
  479. }
  480. else if( !_tcsicmp( m_pSearchPref->szScope, _T("onelevel") ) )
  481. {
  482. V_I4( &(arrProp[ nIter ].vValue ) ) = ADS_SCOPE_ONELEVEL;
  483. }
  484. else
  485. {
  486. V_I4( &(arrProp[ nIter ].vValue ) ) = ADS_SCOPE_BASE;
  487. }
  488. nIter++;
  489. }
  490. //***************************************************************************
  491. if( -1 != m_pSearchPref->nChaseReferrals )
  492. {
  493. arrProp[ nIter ].dwPropertyID = ADSIPROP_CHASE_REFERRALS;
  494. V_VT( &(arrProp[ nIter ].vValue ) ) = VT_I4;
  495. if( m_pSearchPref->nChaseReferrals )
  496. {
  497. V_I4( &(arrProp[ nIter ].vValue) ) = ADS_CHASE_REFERRALS_ALWAYS;
  498. }
  499. else
  500. {
  501. V_I4( &(arrProp[ nIter ].vValue) ) = ADS_CHASE_REFERRALS_NEVER;
  502. }
  503. nIter++;
  504. }
  505. // Create the initialization structure.
  506. if( nIter )
  507. {
  508. aPropSet.rgProperties = arrProp;
  509. aPropSet.cProperties = nIter;
  510. aPropSet.guidPropertySet = DBPROPSET_ADSISEARCH;
  511. hResult = pCommandProp->SetProperties( 1, &aPropSet );
  512. ASSERT( S_OK == hResult );
  513. for ( nIdx = 0; nIdx < nIter ; nIdx++ )
  514. {
  515. VariantClear( &(arrProp[ nIdx ].vValue ) );
  516. }
  517. ASSERT( SUCCEEDED( hResult ) );
  518. }
  519. pCommandProp->Release( );
  520. }
  521. return hResult;
  522. }
  523. /*****************************************************************************
  524. Function: CADsOleDBDataSource::RunTheQuery
  525. Arguments:
  526. Return:
  527. Purpose:
  528. Author(s):
  529. Revision:
  530. Date:
  531. *****************************************************************************/
  532. BOOL CADsOleDBDataSource::RunTheQuery( void )
  533. {
  534. HRESULT hResult;
  535. IDBInitialize * pIDBInit = NULL;
  536. IDBCreateSession * pIDBCS = NULL;
  537. IDBCreateCommand * pIDBCreateCommand= NULL;
  538. ICommandText * pICommandText = NULL;
  539. ICommand * pICommand = NULL;
  540. DBBINDSTATUS * pMyStatus = NULL;
  541. CString strCommandText;
  542. BSTR bstrCommandText;
  543. HCURSOR aCursor, oldCursor;
  544. aCursor = LoadCursor( NULL, IDC_WAIT );
  545. oldCursor = SetCursor( aCursor );
  546. m_nCurrentRow = -1;
  547. m_nFirstRow = -1;
  548. m_nLastRow = -1;
  549. m_bNoMoreData = FALSE;
  550. m_hRows = NULL;
  551. m_nAddOne = 0;
  552. m_strAttributes = m_pSearchPref->szAttributes;
  553. if( !_tcsicmp( m_pSearchPref->szAttributes, _T("*") ) )
  554. {
  555. CString strTemp = _T("ADsPath,");
  556. strTemp += m_pSearchPref->szAttributes;
  557. m_strAttributes = strTemp;
  558. m_nAddOne = 1;
  559. }
  560. if( !m_pSearchPref->bUseSQL )
  561. {
  562. strCommandText = m_pSearchPref->szSource;
  563. if( strCommandText[ 0 ] != _T('<') )
  564. {
  565. CString strTemp;
  566. strTemp = _T("<");
  567. strTemp = strTemp + strCommandText;
  568. strTemp = strTemp + _T(">");
  569. strCommandText = strTemp;
  570. }
  571. strCommandText += _T(';');
  572. strCommandText += m_pSearchPref->szQuery;
  573. strCommandText += _T(';');
  574. strCommandText += m_strAttributes;
  575. /*if( _tcslen( m_pSearchPref->szScope ) )
  576. {
  577. strCommandText += _T(';');
  578. strCommandText += m_pSearchPref->szScope;
  579. }*/
  580. }
  581. else
  582. {
  583. strCommandText = _T("SELECT ");
  584. strCommandText += m_strAttributes;
  585. strCommandText += _T(" FROM '");
  586. strCommandText += m_pSearchPref->szSource;
  587. strCommandText += _T("'");
  588. strCommandText += _T(" WHERE ");
  589. strCommandText += m_pSearchPref->szQuery;
  590. }
  591. bstrCommandText = AllocBSTR( strCommandText.GetBuffer( strCommandText.GetLength( ) + 1 ) );
  592. //
  593. // Instantiate a data source object for LDAP provider
  594. //
  595. while( TRUE )
  596. {
  597. hResult = CoCreateInstance( CLSID_ADsDSOObject,
  598. 0,
  599. CLSCTX_INPROC_SERVER,
  600. IID_IDBInitialize,
  601. (void **)&pIDBInit
  602. );
  603. ASSERT( SUCCEEDED( hResult ) );
  604. if(FAILED(hResult))
  605. {
  606. TRACE(_T("CoCreateInstance failed \n"));
  607. AfxMessageBox( _T("CoCreateInstance failed") );
  608. break;
  609. }
  610. //
  611. // Initialize the Data Source
  612. //
  613. hResult = SetQueryCredentials( pIDBInit, NULL );
  614. ASSERT( SUCCEEDED( hResult ) );
  615. hResult = pIDBInit->Initialize();
  616. ASSERT( SUCCEEDED( hResult ) );
  617. if(FAILED(hResult))
  618. {
  619. TRACE(_T("IDBIntialize::Initialize failed \n"));
  620. AfxMessageBox( _T("IDBIntialize::Initialize failed") );
  621. break;
  622. }
  623. hResult = pIDBInit->QueryInterface(
  624. IID_IDBCreateSession,
  625. (void**) &pIDBCS);
  626. ASSERT( SUCCEEDED( hResult ) );
  627. if(FAILED(hResult))
  628. {
  629. TRACE(_T("QueryInterface for IDBCreateSession failed \n"));
  630. AfxMessageBox( _T("QueryInterface for IDBCreateSession failed") );
  631. break;
  632. }
  633. pIDBInit->Release( );
  634. pIDBInit = NULL;
  635. //
  636. // Create a session returning a pointer to its CreateCommand interface
  637. //
  638. hResult = pIDBCS->CreateSession(
  639. NULL,
  640. IID_IDBCreateCommand,
  641. (LPUNKNOWN*) &pIDBCreateCommand
  642. );
  643. ASSERT( SUCCEEDED( hResult ) );
  644. if(FAILED(hResult))
  645. {
  646. TRACE( _T("IDBCreateSession::CreateSession failed \n") );
  647. AfxMessageBox( _T("IDBCreateSession::CreateSession failed") );
  648. break;
  649. }
  650. pIDBCS->Release( );
  651. pIDBCS = NULL;
  652. //
  653. // Create a command from the session object
  654. //
  655. hResult = pIDBCreateCommand->CreateCommand(
  656. NULL,
  657. IID_ICommandText,
  658. (LPUNKNOWN*) &pICommandText
  659. );
  660. ASSERT( SUCCEEDED( hResult ) );
  661. if(FAILED(hResult))
  662. {
  663. TRACE( _T(" IDBCreateCommand::CreateCommand failed\n") );
  664. AfxMessageBox( _T("IDBCreateCommand::CreateCommand failed") );
  665. break;
  666. }
  667. pIDBCreateCommand->Release( );
  668. pIDBCreateCommand = NULL;
  669. hResult = SetQueryCredentials( NULL, pICommandText );
  670. ASSERT( SUCCEEDED( hResult ) );
  671. if( !m_pSearchPref->bUseSQL )
  672. {
  673. hResult = pICommandText->SetCommandText(
  674. DBGUID_LDAPDialect,
  675. bstrCommandText
  676. );
  677. }
  678. else
  679. {
  680. hResult = pICommandText->SetCommandText(
  681. DBGUID_DBSQL,
  682. bstrCommandText
  683. );
  684. }
  685. ASSERT( SUCCEEDED( hResult ) );
  686. if(FAILED(hResult))
  687. {
  688. TRACE(_T("ICommandText::CommandText failed \n"));
  689. AfxMessageBox( _T("ICommandText::CommandText failed") );
  690. break;
  691. }
  692. hResult = pICommandText->QueryInterface(
  693. IID_ICommand,
  694. (void**) &pICommand);
  695. ASSERT( SUCCEEDED( hResult ) );
  696. if(FAILED(hResult))
  697. {
  698. TRACE(_T("QueryInterface for ICommand failed \n") );
  699. AfxMessageBox( _T("QueryInterface for ICommand failed ") );
  700. break;
  701. }
  702. pICommandText->Release();
  703. pICommandText = NULL;
  704. //
  705. // Do the Query and get back a rowset
  706. //
  707. pICommand->AddRef( );
  708. pICommand->Release( );
  709. hResult = pICommand->Execute(
  710. NULL,
  711. IID_IRowset,
  712. NULL,
  713. NULL,
  714. (LPUNKNOWN *)&m_pIRowset
  715. );
  716. ASSERT( SUCCEEDED( hResult ) );
  717. if(FAILED(hResult))
  718. {
  719. TRACE(_T("ICommand::Execute failed \n"));
  720. AfxMessageBox( _T("ICommand::Execute failed") );
  721. pICommand->Release( );
  722. pICommand = NULL;
  723. break;
  724. }
  725. m_pIRowset->AddRef( );
  726. m_pIRowset->Release( );
  727. pICommand->Release( );
  728. pICommand = NULL;
  729. hResult = m_pIRowset->QueryInterface(
  730. IID_IColumnsInfo,
  731. (void**) &m_pIColsInfo
  732. );
  733. ASSERT( SUCCEEDED( hResult ) );
  734. if(FAILED(hResult))
  735. {
  736. TRACE(_T("QueryInterface for IColumnsInfo failed \n"));
  737. AfxMessageBox( _T("QueryInterface for IColumnsInfo failed") );
  738. break;
  739. }
  740. hResult = m_pIColsInfo->GetColumnInfo(
  741. &m_nColumnsCount,
  742. &m_prgColInfo,
  743. &m_szColNames
  744. );
  745. ASSERT( SUCCEEDED( hResult ) );
  746. if(FAILED(hResult))
  747. {
  748. TRACE( _T("IColumnsInfo::GetColumnInfo failed \n") );
  749. AfxMessageBox( _T("IColumnsInfo::GetColumnInfo failed") );
  750. break;
  751. }
  752. //
  753. // The no. of attributes is one less than the no. of columns because of
  754. // the Bookmark column
  755. //
  756. m_nColumnsCount--;
  757. m_pData = (Data *) LocalAlloc( LPTR, sizeof(Data) * m_nColumnsCount );
  758. m_pBindStatus = (DBBINDSTATUS *) LocalAlloc(
  759. LPTR,
  760. sizeof(DBBINDSTATUS) * m_nColumnsCount
  761. );
  762. /* SMITHA hResult = CreateAccessorHelp( );
  763. if(FAILED(hResult))
  764. {
  765. TRACE(_T("CreateAccessorHelper failed \n"));
  766. break;
  767. }*/
  768. if (!CreateAccessorHelp( ))
  769. {
  770. TRACE(_T("CreateAccessorHelper failed \n"));
  771. break;
  772. }
  773. break;
  774. }
  775. SysFreeString( bstrCommandText );
  776. SetCursor( oldCursor );
  777. return SUCCEEDED( hResult );
  778. }
  779. /*****************************************************************************
  780. Function: CADsOleDBDataSource::AdvanceCursor
  781. Arguments:
  782. Return:
  783. Purpose:
  784. Author(s):
  785. Revision:
  786. Date:
  787. *****************************************************************************/
  788. BOOL CADsOleDBDataSource::AdvanceCursor( void )
  789. {
  790. HRESULT hResult;
  791. DBCOUNTITEM cRowsObtained;
  792. HCURSOR aCursor, oldCursor;
  793. aCursor = LoadCursor( NULL, IDC_WAIT );
  794. oldCursor = SetCursor( aCursor );
  795. if( !m_bNoMoreData )
  796. {
  797. cRowsObtained = 0;
  798. if( m_hRows )
  799. {
  800. ULONG refCount[ ROWS ];
  801. DBROWSTATUS rowStat[ ROWS ];
  802. m_pIRowset->ReleaseRows( ROWS,
  803. m_hRows,
  804. NULL,
  805. refCount,
  806. rowStat );
  807. m_pIMalloc->Free( m_hRows );
  808. m_hRows = NULL;
  809. }
  810. TRACE( _T("Ask for %d rows starting from %d\n"), ROWS, m_nLastRow + 1 );
  811. hResult = m_pIRowset->GetNextRows(
  812. NULL,
  813. 0,
  814. ROWS,
  815. //1L,
  816. &cRowsObtained,
  817. &m_hRows
  818. );
  819. TRACE( _T("After asking for %d rows starting from %d\n"), ROWS, m_nLastRow + 1 );
  820. if( SUCCEEDED( hResult ) )
  821. {
  822. m_nFirstRow = m_nLastRow + 1;
  823. m_nLastRow = (int)(m_nFirstRow + cRowsObtained - 1);
  824. }
  825. m_bNoMoreData = (cRowsObtained != ROWS);
  826. }
  827. SetCursor( oldCursor );
  828. return TRUE;
  829. }
  830. /*****************************************************************************
  831. Function: CADsOleDBDataSource::GetADsPath
  832. Arguments:
  833. Return:
  834. Purpose:
  835. Author(s):
  836. Revision:
  837. Date:
  838. ***********************************************************/
  839. BOOL CADsOleDBDataSource::GetADsPath( int nRow, CString& rPath )
  840. {
  841. ASSERT( nRow < m_ADsPath.GetSize( ) );
  842. if( ! (nRow < m_ADsPath.GetSize( ) ) )
  843. {
  844. return FALSE;
  845. }
  846. rPath = m_ADsPath.GetAt( nRow );
  847. return TRUE;
  848. }
  849. /*****************************************************************************
  850. Function: CADsOleDBDataSource::GetValue
  851. Arguments:
  852. Return:
  853. Purpose:
  854. Author(s):
  855. Revision:
  856. Date:
  857. ***********************************************************/
  858. BOOL CADsOleDBDataSource::GetValue( int nRow, int nColumn, CString& rValue )
  859. {
  860. HRESULT hResult = S_OK;
  861. nColumn += m_nAddOne;
  862. ASSERT( nRow >= 0 );
  863. if( nRow < 0 )
  864. {
  865. return FALSE;
  866. }
  867. ASSERT( nColumn >=0 && nColumn < (int)m_nColumnsCount );
  868. if( nColumn < 0 || nColumn >= (int)m_nColumnsCount )
  869. {
  870. return FALSE;
  871. }
  872. if( !BringRowInBuffer( nRow ) )
  873. return FALSE;
  874. if( m_nCurrentRow != nRow )
  875. {
  876. hResult = m_pIRowset->GetData( m_hRows[nRow - m_nFirstRow],
  877. m_hAccessor,
  878. (void*)m_pData );
  879. m_nCurrentRow = nRow;
  880. ReadADsPath( );
  881. }
  882. if( SUCCEEDED( hResult) )
  883. {
  884. rValue = ExtractData( nColumn );
  885. }
  886. else
  887. {
  888. rValue = _T("Error");
  889. }
  890. return TRUE;
  891. }
  892. /*****************************************************************************
  893. Function: CADsOleDBDataSource::ReadADsPath
  894. Arguments:
  895. Return:
  896. Purpose:
  897. Author(s):
  898. Revision:
  899. Date:
  900. ***********************************************************/
  901. void CADsOleDBDataSource::ReadADsPath( void )
  902. {
  903. CString strADsPath;
  904. strADsPath = ExtractData( 0 );
  905. m_ADsPath.Add( strADsPath );
  906. }
  907. /*****************************************************************************
  908. Function: CADsOleDBDataSource::ExtractData
  909. Arguments:
  910. Return:
  911. Purpose:
  912. Author(s):
  913. Revision:
  914. Date:
  915. ***********************************************************/
  916. CString CADsOleDBDataSource::ExtractData( int nColumn )
  917. {
  918. ULONG i;
  919. TCHAR szValue[ 2048 ] = _T("Error");
  920. HRESULT hResult;
  921. i = nColumn;
  922. if( m_pData[i].status == DBSTATUS_S_ISNULL )
  923. return CString(_T("<No value>") );
  924. if( !( m_prgColInfo[i+1].dwFlags & DBCOLUMNFLAGS_ISNULLABLE &&
  925. m_pData[i].status == DBSTATUS_S_ISNULL))
  926. {
  927. switch(m_prgColInfo[i+1].wType)
  928. {
  929. case DBTYPE_I8:
  930. LARGE_INTEGERToString( szValue, (LARGE_INTEGER*)&m_pData[i].obValue2 );
  931. break;
  932. case DBTYPE_I4:
  933. wsprintf( szValue, _T("%ld"), (DWORD_PTR) m_pData[i].obValue );
  934. break;
  935. case DBTYPE_BOOL:
  936. /*SMITHA wsprintf( szValue, _T("%s"),
  937. (VARIANT_TRUE == (VARIANT_BOOL) m_pData[i].obValue) ? _T("TRUE") : _T("FALSE") );*/
  938. _tcscpy( szValue, (VARIANT_TRUE == (VARIANT_BOOL) m_pData[i].obValue) ? _T("TRUE") : _T("FALSE") );
  939. break;
  940. case DBTYPE_DATE:
  941. {
  942. VARIANT varTemp, varString;
  943. //SMITHA HRESULT hResult;
  944. VariantInit( &varString );
  945. VariantInit( &varTemp );
  946. V_VT( &varTemp ) = VT_DATE;
  947. V_DATE( &varTemp ) = (DATE) m_pData[i].obValue2;
  948. hResult= VariantChangeType( &varString, &varTemp, VARIANT_NOVALUEPROP, VT_BSTR );
  949. ASSERT( SUCCEEDED( hResult ) );
  950. if( SUCCEEDED( hResult ) )
  951. {
  952. Convert( szValue, V_BSTR( &varString ) );
  953. VariantClear( &varString );
  954. }
  955. break;
  956. }
  957. case DBTYPE_STR | DBTYPE_BYREF:
  958. if( NULL != (char *)m_pData[i].obValue )
  959. {
  960. //SMITHA wsprintf( szValue, _T("%s"), (char *)m_pData[i].obValue );
  961. _tcscpy(szValue, (TCHAR *) m_pData[i].obValue);
  962. }
  963. else
  964. {
  965. _tcscpy( szValue, _T("NULL") );
  966. }
  967. break;
  968. case DBTYPE_BYTES | DBTYPE_BYREF:
  969. {
  970. TCHAR szTemp[ 16 ];
  971. ULONG ulIter;
  972. BYTE* pByte;
  973. _ltot ( m_pData[i].obLength, szTemp, 10 );
  974. _tcscpy ( szValue, _T("[") );
  975. _tcscat ( szValue, szTemp );
  976. _tcscat ( szValue, _T("] ") );
  977. pByte = (BYTE*) (m_pData[i].obValue);
  978. for( ulIter = 0; ulIter < m_pData[i].obLength && _tcslen( szValue ) < 2000; ulIter++ )
  979. {
  980. BYTE bVal;
  981. bVal = pByte[ ulIter ];
  982. _itot( (int)bVal , szTemp, 16 );
  983. _tcscat ( szValue, _T("x") );
  984. _tcscat ( szValue, szTemp );
  985. _tcscat ( szValue, _T(" ") );
  986. }
  987. }
  988. break;
  989. case DBTYPE_WSTR | DBTYPE_BYREF:
  990. if( NULL != (WCHAR *)m_pData[i].obValue )
  991. {
  992. wsprintf( szValue, _T("%S"), (WCHAR *) m_pData[i].obValue );
  993. }
  994. else
  995. {
  996. _tcscpy( szValue, _T("NULL") );
  997. }
  998. break;
  999. case DBTYPE_VARIANT | DBTYPE_BYREF:
  1000. {
  1001. ULONG dwSLBound;
  1002. ULONG dwSUBound, j;
  1003. void HUGEP *pArray;
  1004. VARIANT *pVariant;
  1005. pArray = NULL;
  1006. pVariant = (VARIANT*) m_pData[i].obValue;
  1007. if( NULL == pVariant )
  1008. _tcscpy( szValue, _T("ERROR!!! m_pData[i].obValue is NULL") );
  1009. while( TRUE && (NULL != pVariant) )
  1010. {
  1011. ASSERT( V_VT( pVariant ) & VT_ARRAY );
  1012. if( ! (V_VT( pVariant ) & VT_ARRAY ) )
  1013. break;
  1014. hResult = SafeArrayGetLBound( V_ARRAY(pVariant),
  1015. 1,
  1016. (long FAR *) &dwSLBound );
  1017. ASSERT( SUCCEEDED( hResult ) );
  1018. if( FAILED( hResult ) )
  1019. break;
  1020. hResult = SafeArrayGetUBound( V_ARRAY(pVariant),
  1021. 1,
  1022. (long FAR *) &dwSUBound );
  1023. ASSERT( SUCCEEDED( hResult ) );
  1024. if( FAILED( hResult ) )
  1025. break;
  1026. hResult = SafeArrayAccessData( V_ARRAY(pVariant),
  1027. &pArray );
  1028. ASSERT( SUCCEEDED( hResult ) );
  1029. if( FAILED( hResult ) )
  1030. break;
  1031. _tcscpy( szValue, _T("") );
  1032. for ( j = dwSLBound; j <= dwSUBound; j++ )
  1033. {
  1034. TCHAR szTemp[ 1024 ];
  1035. switch( pVariant->vt & ~VT_ARRAY )
  1036. {
  1037. case VT_BSTR:
  1038. Convert( szTemp, ( (BSTR *)pArray )[j] );
  1039. break;
  1040. case VT_I8:
  1041. LARGE_INTEGERToString( szTemp, &( (LARGE_INTEGER *)pArray )[j] );
  1042. break;
  1043. case VT_I4:
  1044. _ltot( ((DWORD *) pArray)[j], szTemp, 10 );
  1045. break;
  1046. case VT_BOOL:
  1047. _tcscpy( szTemp, (((VARIANT_BOOL *) pArray)[j]) == VARIANT_TRUE ?
  1048. _T("TRUE") : _T("FALSE") );
  1049. break;
  1050. case VT_VARIANT:
  1051. {
  1052. VARIANT* pVar;
  1053. pVar = ((VARIANT *)pArray) + j;
  1054. switch( V_VT( pVar ) )
  1055. {
  1056. case VT_BSTR:
  1057. Convert( szTemp, V_BSTR( pVar ) );
  1058. break;
  1059. case VT_I4:
  1060. _ltot( V_I4( pVar ), szTemp, 10 );
  1061. break;
  1062. case VT_BOOL:
  1063. _tcscpy( szTemp, V_BOOL( pVar ) == VARIANT_TRUE ?
  1064. _T("TRUE") : _T("FALSE") );
  1065. break;
  1066. default:
  1067. ASSERT( FALSE ) ;
  1068. _tcscpy( szTemp, _T("Unsupported") );
  1069. break;
  1070. }
  1071. break;
  1072. }
  1073. default:
  1074. _tcscpy( szTemp, _T("Unsupported") );
  1075. }
  1076. if( _tcslen( szValue) + _tcslen( szTemp ) < 2040 )
  1077. {
  1078. if( j != dwSLBound )
  1079. {
  1080. _tcscat( szValue, _T("# ") );
  1081. }
  1082. _tcscat( szValue, szTemp );
  1083. }
  1084. }
  1085. SafeArrayUnaccessData( V_ARRAY(pVariant) );
  1086. break;
  1087. }
  1088. break;
  1089. }
  1090. default:
  1091. wsprintf( szValue, _T("Don't know how to convert") );
  1092. break;
  1093. }
  1094. }
  1095. else
  1096. {
  1097. _tcscpy( szValue, _T("NA") );
  1098. }
  1099. return CString( szValue );
  1100. }
  1101. /*****************************************************************************
  1102. Function: CADsOleDBDataSource::BringRowInBuffer
  1103. Arguments:
  1104. Return:
  1105. Purpose:
  1106. Author(s):
  1107. Revision:
  1108. Date:
  1109. ***********************************************************/
  1110. BOOL CADsOleDBDataSource::BringRowInBuffer( int nRow )
  1111. {
  1112. if( nRow >= m_nFirstRow && nRow <= m_nLastRow )
  1113. return TRUE;
  1114. if( m_nLastRow == -1 )
  1115. AdvanceCursor( );
  1116. while( nRow > m_nLastRow && !m_bNoMoreData )
  1117. AdvanceCursor( );
  1118. if (nRow >= m_nFirstRow && nRow <= m_nLastRow)
  1119. {
  1120. return TRUE;
  1121. }
  1122. return FALSE;
  1123. }
  1124. /*****************************************************************************
  1125. Function: CADsOleDBDataSource::DestroyInternalData
  1126. Arguments:
  1127. Return:
  1128. Purpose:
  1129. Author(s):
  1130. Revision:
  1131. Date:
  1132. ***********************************************************/
  1133. void CADsOleDBDataSource::DestroyInternalData( void )
  1134. {
  1135. if( m_hRows )
  1136. {
  1137. m_pIMalloc->Free( m_hRows );
  1138. }
  1139. if( m_pBindStatus )
  1140. {
  1141. LocalFree( m_pBindStatus );
  1142. }
  1143. if( m_pData )
  1144. {
  1145. LocalFree( m_pData );
  1146. }
  1147. if( m_pIRowset )
  1148. {
  1149. m_pIRowset->Release( );
  1150. }
  1151. if( m_pIColsInfo )
  1152. {
  1153. m_pIColsInfo->Release( );
  1154. }
  1155. if( m_pAccessor )
  1156. {
  1157. m_pAccessor->Release( );
  1158. }
  1159. if( m_prgColInfo )
  1160. {
  1161. m_pIMalloc->Free( m_prgColInfo );
  1162. }
  1163. if( m_szColNames )
  1164. {
  1165. m_pIMalloc->Free( m_szColNames );
  1166. }
  1167. }
  1168. /*****************************************************************************
  1169. Function:
  1170. Arguments:
  1171. Return:
  1172. Purpose:
  1173. Author(s):
  1174. Revision:
  1175. Date:
  1176. *****************************************************************************/
  1177. CADsSearchDataSource::CADsSearchDataSource( )
  1178. {
  1179. m_pSearch = NULL;
  1180. m_nCurrentRow = -1;
  1181. m_hSearch = NULL;
  1182. }
  1183. /*****************************************************************************
  1184. Function:
  1185. Arguments:
  1186. Return:
  1187. Purpose:
  1188. Author(s):
  1189. Revision:
  1190. Date:
  1191. *****************************************************************************/
  1192. CADsSearchDataSource::~CADsSearchDataSource( )
  1193. {
  1194. if( m_pSearch )
  1195. {
  1196. m_pSearch->CloseSearchHandle( m_hSearch );
  1197. m_pSearch->Release( );
  1198. }
  1199. }
  1200. /*****************************************************************************
  1201. Function:
  1202. Arguments:
  1203. Return:
  1204. Purpose:
  1205. Author(s):
  1206. Revision:
  1207. Date:
  1208. *****************************************************************************/
  1209. int CADsSearchDataSource::GetColumnsCount( int nRow )
  1210. {
  1211. BOOL bOK;
  1212. bOK = BringRowInBuffer( nRow );
  1213. if( bOK )
  1214. {
  1215. return (int)m_strColumns.GetSize( );
  1216. }
  1217. else
  1218. {
  1219. return 0;
  1220. }
  1221. }
  1222. /*****************************************************************************
  1223. Function: CADsSearchDataSource::GetValue
  1224. Arguments:
  1225. Return:
  1226. Purpose:
  1227. Author(s):
  1228. Revision:
  1229. Date:
  1230. *****************************************************************************/
  1231. BOOL CADsSearchDataSource::GetValue( int nRow, int nColumn, CString& )
  1232. {
  1233. return FALSE;
  1234. }
  1235. /*****************************************************************************
  1236. Function: CADsSearchDataSource::GetValue
  1237. Arguments:
  1238. Return:
  1239. Purpose:
  1240. Author(s):
  1241. Revision:
  1242. Date:
  1243. *****************************************************************************/
  1244. BOOL CADsSearchDataSource::GetValue( int nRow, CString& strColumn, CString& rValue )
  1245. {
  1246. HRESULT hResult;
  1247. BSTR bstrColumnName;
  1248. ADS_SEARCH_COLUMN aSearchColumn;
  1249. ADS_ATTR_INFO aAttrDef;
  1250. BOOL bOK = TRUE;
  1251. COleDsSyntax* pSyntax;
  1252. rValue = _T("<No value>");
  1253. bOK = BringRowInBuffer( nRow );
  1254. if( bOK )
  1255. {
  1256. bstrColumnName = AllocBSTR( strColumn.GetBuffer( 256 ) );
  1257. hResult = m_pSearch->GetColumn( m_hSearch,
  1258. bstrColumnName,
  1259. &aSearchColumn );
  1260. if( SUCCEEDED( hResult ) )
  1261. {
  1262. aAttrDef.pszAttrName = aSearchColumn.pszAttrName;
  1263. aAttrDef.dwADsType = aSearchColumn.dwADsType;
  1264. aAttrDef.pADsValues = aSearchColumn.pADsValues;
  1265. aAttrDef.dwNumValues = aSearchColumn.dwNumValues;
  1266. pSyntax = NULL;
  1267. pSyntax = GetSyntaxHandler( aAttrDef.dwADsType, rValue );
  1268. if( pSyntax )
  1269. {
  1270. hResult = pSyntax->Native2Value( &aAttrDef, rValue );
  1271. delete pSyntax;
  1272. }
  1273. hResult = m_pSearch->FreeColumn( &aSearchColumn );
  1274. }
  1275. SysFreeString( bstrColumnName );
  1276. }
  1277. return bOK;
  1278. }
  1279. /*****************************************************************************
  1280. Function: CADsSearchDataSource::GetADsPath
  1281. Arguments:
  1282. Return:
  1283. Purpose:
  1284. Author(s):
  1285. Revision:
  1286. Date:
  1287. *****************************************************************************/
  1288. BOOL CADsSearchDataSource::GetADsPath( int nRow, CString& rPath )
  1289. {
  1290. ASSERT( nRow < m_ADsPath.GetSize( ) );
  1291. if( ! (nRow < m_ADsPath.GetSize( ) ) )
  1292. {
  1293. return FALSE;
  1294. }
  1295. rPath = m_ADsPath.GetAt( nRow );
  1296. return TRUE;
  1297. }
  1298. /*****************************************************************************
  1299. Function: CADsSearchDataSource::GetColumnText
  1300. Arguments:
  1301. Return:
  1302. Purpose:
  1303. Author(s):
  1304. Revision:
  1305. Date:
  1306. *****************************************************************************/
  1307. BOOL CADsSearchDataSource::GetColumnText( int nRow, int nColumn, CString& rColumn )
  1308. {
  1309. BOOL bOK = TRUE;
  1310. bOK = BringRowInBuffer( nRow );
  1311. if( bOK )
  1312. {
  1313. rColumn = m_strColumns[ nColumn ];
  1314. }
  1315. return bOK;
  1316. }
  1317. /*****************************************************************************
  1318. Function: CADsSearchDataSource::RunTheQuery
  1319. Arguments:
  1320. Return:
  1321. Purpose:
  1322. Author(s):
  1323. Revision:
  1324. Date:
  1325. *****************************************************************************/
  1326. BOOL CADsSearchDataSource::RunTheQuery( void )
  1327. {
  1328. HRESULT hResult;
  1329. HCURSOR aCursor, oldCursor;
  1330. aCursor = LoadCursor( NULL, IDC_WAIT );
  1331. oldCursor = SetCursor( aCursor );
  1332. while( TRUE )
  1333. {
  1334. hResult = CreateSearchInterface( );
  1335. if( FAILED( hResult ) )
  1336. break;
  1337. hResult = SetSearchPreferences( );
  1338. if( FAILED( hResult ) )
  1339. break;
  1340. hResult = SetAttributesName( );
  1341. break;
  1342. }
  1343. SetCursor( oldCursor );
  1344. m_bNoMoreData = FALSE;
  1345. return (S_OK == hResult);
  1346. }
  1347. /*****************************************************************************
  1348. Function: CADsSearchDataSource::SetAttributesName
  1349. Arguments:
  1350. Return:
  1351. Purpose:
  1352. Author(s):
  1353. Revision:
  1354. Date:
  1355. *****************************************************************************/
  1356. HRESULT CADsSearchDataSource::SetAttributesName( void )
  1357. {
  1358. HRESULT hResult = E_FAIL;
  1359. BSTR bstrSearchFilter;
  1360. DWORD dwNumAttributes = (DWORD)-1;
  1361. BSTR ppszAttributes[ 128 ];
  1362. int nIterator;
  1363. TCHAR szAttribute[ 128 ];
  1364. CString strAttr;
  1365. CString strAttributes;
  1366. bstrSearchFilter = AllocBSTR( m_pSearchPref->szQuery );
  1367. strAttributes = m_pSearchPref->szAttributes;
  1368. // first, we need to figure out how many attributes are requested
  1369. strAttributes.TrimLeft( );
  1370. strAttributes.TrimRight( );
  1371. if( strAttributes.CompareNoCase( _T("*") ) )
  1372. {
  1373. dwNumAttributes = 1;
  1374. _tcscpy( szAttribute, _T("") );
  1375. for( nIterator = 0; nIterator < strAttributes.GetLength( ) ; nIterator++ )
  1376. {
  1377. if( strAttributes[ nIterator ] == _T(',') )
  1378. {
  1379. strAttr = szAttribute;
  1380. strAttr.TrimLeft( );
  1381. strAttr.TrimRight( );
  1382. ppszAttributes[ dwNumAttributes - 1 ] = AllocBSTR( strAttr.GetBuffer( 128 ) );
  1383. dwNumAttributes++;
  1384. _tcscpy( szAttribute, _T("") );
  1385. }
  1386. else
  1387. {
  1388. TCHAR szChars[ 2 ];
  1389. szChars[ 1 ] = _T('\0');
  1390. szChars[ 0 ] = strAttributes[ nIterator ];
  1391. _tcscat( szAttribute, szChars );
  1392. if( nIterator == strAttributes.GetLength( ) - 1 )
  1393. {
  1394. strAttr = szAttribute;
  1395. strAttr.TrimLeft( );
  1396. strAttr.TrimRight( );
  1397. ppszAttributes[ dwNumAttributes - 1 ] = AllocBSTR( strAttr.GetBuffer( 128 ) );
  1398. }
  1399. }
  1400. }
  1401. }
  1402. hResult = m_pSearch->ExecuteSearch(
  1403. bstrSearchFilter,
  1404. ppszAttributes,
  1405. dwNumAttributes,
  1406. &m_hSearch
  1407. );
  1408. if( FAILED( hResult ) )
  1409. {
  1410. TRACE(_T("ExecuteSearch failed with %lx \n"), hResult);
  1411. AfxMessageBox( _T("ExecuteSearch failed") );
  1412. }
  1413. for( nIterator = 0; nIterator < (int)dwNumAttributes ; nIterator++ )
  1414. {
  1415. SysFreeString( ppszAttributes[ nIterator ] );
  1416. }
  1417. SysFreeString( bstrSearchFilter );
  1418. return hResult;
  1419. }
  1420. /*****************************************************************************
  1421. Function:
  1422. Arguments:
  1423. Return:
  1424. Purpose:
  1425. Author(s):
  1426. Revision:
  1427. Date:
  1428. *****************************************************************************/
  1429. /*HRESULT CADsSearchDataSource::SetSearchPreferences( )
  1430. {
  1431. CSearchPreferencesDlg aSearchPref;
  1432. ADS_SEARCHPREF_INFO arrSearchPref[ 20 ];
  1433. int nSearchPrefCount = 0;
  1434. HRESULT hResult;
  1435. if( aSearchPref.DoModal( ) != IDOK )
  1436. {
  1437. return E_FAIL;
  1438. }
  1439. //***************************************************************************
  1440. if( !aSearchPref.m_strAsynchronous.IsEmpty( ) )
  1441. {
  1442. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_ASYNCHRONOUS;
  1443. arrSearchPref[nSearchPrefCount].vValue.dwType= ADSTYPE_BOOLEAN;
  1444. if( !aSearchPref.m_strAsynchronous.CompareNoCase( _T("Yes") ) )
  1445. {
  1446. arrSearchPref[nSearchPrefCount].vValue.Boolean = TRUE;
  1447. }
  1448. else
  1449. {
  1450. arrSearchPref[nSearchPrefCount].vValue.Boolean = FALSE;
  1451. }
  1452. nSearchPrefCount++;
  1453. }
  1454. //***************************************************************************
  1455. if( !aSearchPref.m_strAttributesOnly.IsEmpty( ) )
  1456. {
  1457. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
  1458. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_BOOLEAN;
  1459. if( !aSearchPref.m_strAttributesOnly.CompareNoCase( _T("Yes") ) )
  1460. {
  1461. arrSearchPref[nSearchPrefCount].vValue.Boolean = TRUE;
  1462. }
  1463. else
  1464. {
  1465. arrSearchPref[nSearchPrefCount].vValue.Boolean = FALSE;
  1466. }
  1467. nSearchPrefCount++;
  1468. }
  1469. //***************************************************************************
  1470. if( !aSearchPref.m_strDerefAliases.IsEmpty( ) )
  1471. {
  1472. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_DEREF_ALIASES;
  1473. arrSearchPref[nSearchPrefCount].vValue.dwType= ADSTYPE_INTEGER;
  1474. if( !aSearchPref.m_strDerefAliases.CompareNoCase( _T("Yes") ) )
  1475. {
  1476. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_DEREF_ALWAYS;
  1477. }
  1478. else
  1479. {
  1480. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_DEREF_NEVER;
  1481. }
  1482. nSearchPrefCount++;
  1483. }
  1484. //***************************************************************************
  1485. if( !aSearchPref.m_strTimeOut.IsEmpty( ) )
  1486. {
  1487. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_TIMEOUT;
  1488. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1489. arrSearchPref[nSearchPrefCount].vValue.Integer = _ttoi( aSearchPref.m_strTimeOut.GetBuffer( 16 ) );
  1490. nSearchPrefCount++;
  1491. }
  1492. //***************************************************************************
  1493. if( !aSearchPref.m_strTimeLimit.IsEmpty( ) )
  1494. {
  1495. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_TIME_LIMIT;
  1496. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1497. arrSearchPref[nSearchPrefCount].vValue.Integer = _ttoi( aSearchPref.m_strTimeLimit.GetBuffer( 16 ) );
  1498. nSearchPrefCount++;
  1499. }
  1500. //***************************************************************************
  1501. if( !aSearchPref.m_strSizeLimit.IsEmpty( ) )
  1502. {
  1503. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_SIZE_LIMIT;
  1504. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1505. arrSearchPref[nSearchPrefCount].vValue.Integer = _ttoi( aSearchPref.m_strSizeLimit.GetBuffer( 16 ) );
  1506. nSearchPrefCount++;
  1507. }
  1508. //***************************************************************************
  1509. if( !aSearchPref.m_strPageSize.IsEmpty( ) )
  1510. {
  1511. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  1512. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1513. arrSearchPref[nSearchPrefCount].vValue.Integer = _ttoi( aSearchPref.m_strPageSize.GetBuffer( 16 ) );
  1514. nSearchPrefCount++;
  1515. }
  1516. //***************************************************************************
  1517. if( !aSearchPref.m_strScope.IsEmpty( ) )
  1518. {
  1519. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  1520. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1521. if (!aSearchPref.m_strScope.CompareNoCase( _T("Base" ) ) )
  1522. {
  1523. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_BASE;
  1524. }
  1525. if (!aSearchPref.m_strScope.CompareNoCase( _T("OneLevel" ) ) )
  1526. {
  1527. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_ONELEVEL;
  1528. }
  1529. if (!aSearchPref.m_strScope.CompareNoCase( _T("Subtree" ) ) )
  1530. {
  1531. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_SUBTREE;
  1532. }
  1533. nSearchPrefCount++;
  1534. }
  1535. hResult = m_pSearch->SetSearchPreference( arrSearchPref, nSearchPrefCount );
  1536. ASSERT( SUCCEEDED( hResult ) );
  1537. return hResult;
  1538. } */
  1539. /*****************************************************************************
  1540. Function:
  1541. Arguments:
  1542. Return:
  1543. Purpose:
  1544. Author(s):
  1545. Revision:
  1546. Date:
  1547. *****************************************************************************/
  1548. HRESULT CADsSearchDataSource::SetSearchPreferences( )
  1549. {
  1550. ADS_SEARCHPREF_INFO arrSearchPref[ 20 ];
  1551. int nSearchPrefCount = 0;
  1552. HRESULT hResult;
  1553. //***************************************************************************
  1554. if( -1 != m_pSearchPref->nAsynchronous )
  1555. {
  1556. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_ASYNCHRONOUS;
  1557. arrSearchPref[nSearchPrefCount].vValue.dwType= ADSTYPE_BOOLEAN;
  1558. arrSearchPref[nSearchPrefCount].vValue.Boolean = (BOOLEAN) m_pSearchPref->nAsynchronous;
  1559. nSearchPrefCount++;
  1560. }
  1561. //***************************************************************************
  1562. if( -1 != m_pSearchPref->nAttributesOnly )
  1563. {
  1564. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
  1565. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_BOOLEAN;
  1566. arrSearchPref[nSearchPrefCount].vValue.Boolean = (BOOLEAN) m_pSearchPref->nAttributesOnly;
  1567. nSearchPrefCount++;
  1568. }
  1569. //***************************************************************************
  1570. if( -1 != m_pSearchPref->nDerefAliases )
  1571. {
  1572. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_DEREF_ALIASES;
  1573. arrSearchPref[nSearchPrefCount].vValue.dwType= ADSTYPE_INTEGER;
  1574. if( m_pSearchPref->nDerefAliases )
  1575. {
  1576. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_DEREF_ALWAYS;
  1577. }
  1578. else
  1579. {
  1580. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_DEREF_NEVER;
  1581. }
  1582. nSearchPrefCount++;
  1583. }
  1584. //***************************************************************************
  1585. if( -1 != m_pSearchPref->nTimeOut )
  1586. {
  1587. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_TIMEOUT;
  1588. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1589. arrSearchPref[nSearchPrefCount].vValue.Integer = m_pSearchPref->nTimeOut;
  1590. nSearchPrefCount++;
  1591. }
  1592. //***************************************************************************
  1593. if( -1 != m_pSearchPref->nTimeLimit )
  1594. {
  1595. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_TIME_LIMIT;
  1596. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1597. arrSearchPref[nSearchPrefCount].vValue.Integer = m_pSearchPref->nTimeLimit;
  1598. nSearchPrefCount++;
  1599. }
  1600. //***************************************************************************
  1601. if( -1 != m_pSearchPref->nSizeLimit )
  1602. {
  1603. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_SIZE_LIMIT;
  1604. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1605. arrSearchPref[nSearchPrefCount].vValue.Integer = m_pSearchPref->nSizeLimit;
  1606. nSearchPrefCount++;
  1607. }
  1608. //***************************************************************************
  1609. if( -1 != m_pSearchPref->nPageSize )
  1610. {
  1611. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  1612. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1613. arrSearchPref[nSearchPrefCount].vValue.Integer = m_pSearchPref->nPageSize;
  1614. nSearchPrefCount++;
  1615. }
  1616. //***************************************************************************
  1617. if( _tcslen( m_pSearchPref->szScope ) )
  1618. {
  1619. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  1620. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1621. if( !_tcsicmp( m_pSearchPref->szScope, _T("Base" ) ) )
  1622. {
  1623. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_BASE;
  1624. }
  1625. if( !_tcsicmp( m_pSearchPref->szScope, _T("OneLevel" ) ) )
  1626. {
  1627. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_ONELEVEL;
  1628. }
  1629. if( !_tcsicmp( m_pSearchPref->szScope, _T("Subtree" ) ) )
  1630. {
  1631. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_SCOPE_SUBTREE;
  1632. }
  1633. nSearchPrefCount++;
  1634. }
  1635. //***************************************************************************
  1636. if( -1 != m_pSearchPref->nChaseReferrals )
  1637. {
  1638. arrSearchPref[nSearchPrefCount].dwSearchPref = ADS_SEARCHPREF_CHASE_REFERRALS;
  1639. arrSearchPref[nSearchPrefCount].vValue.dwType = ADSTYPE_INTEGER;
  1640. if( m_pSearchPref->nChaseReferrals )
  1641. {
  1642. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_CHASE_REFERRALS_ALWAYS;
  1643. }
  1644. else
  1645. {
  1646. arrSearchPref[nSearchPrefCount].vValue.Integer = ADS_CHASE_REFERRALS_NEVER;
  1647. }
  1648. nSearchPrefCount++;
  1649. }
  1650. hResult = m_pSearch->SetSearchPreference( arrSearchPref, nSearchPrefCount );
  1651. ASSERT( S_OK == hResult );
  1652. return hResult;
  1653. }
  1654. /*****************************************************************************
  1655. Function:
  1656. Arguments:
  1657. Return:
  1658. Purpose:
  1659. Author(s):
  1660. Revision:
  1661. Date:
  1662. *****************************************************************************/
  1663. HRESULT CADsSearchDataSource::CreateSearchInterface( )
  1664. {
  1665. BSTR bstrPath = NULL;
  1666. HRESULT hResult = E_FAIL;
  1667. bstrPath = AllocBSTR( m_pSearchPref->szSource );
  1668. if( _tcslen( m_pSearchPref->szUserName ) )
  1669. {
  1670. BSTR bstrOpenAs = NULL;
  1671. BSTR bstrPassword = NULL;
  1672. LONG lControlCode = 0L;
  1673. bstrOpenAs = AllocBSTR( m_pSearchPref->szUserName );
  1674. if (_tcslen(m_pSearchPref->szPassword))
  1675. bstrPassword = AllocBSTR( m_pSearchPref->szPassword );
  1676. else
  1677. bstrPassword = NULL;
  1678. if ((NULL != bstrOpenAs) && (0 == _wcsicmp( bstrOpenAs, L"NULL" )))
  1679. {
  1680. SysFreeString(bstrOpenAs);
  1681. bstrOpenAs = NULL;
  1682. }
  1683. if ((NULL != bstrPassword) && (0 == _wcsicmp( bstrPassword, L"NULL" )))
  1684. {
  1685. SysFreeString(bstrPassword);
  1686. bstrPassword = NULL;
  1687. }
  1688. if( m_pSearchPref->bEncryptPassword )
  1689. lControlCode = lControlCode | ADS_SECURE_AUTHENTICATION;
  1690. hResult = ADsOpenObject( bstrPath,
  1691. bstrOpenAs,
  1692. bstrPassword,
  1693. lControlCode, IID_IDirectorySearch, (void**)&m_pSearch );
  1694. if (NULL != bstrOpenAs)
  1695. SysFreeString( bstrOpenAs );
  1696. if (NULL != bstrPassword)
  1697. SysFreeString( bstrPassword );
  1698. }
  1699. else
  1700. {
  1701. hResult = ADsGetObject( bstrPath, IID_IDirectorySearch, (void**)&m_pSearch );
  1702. }
  1703. ASSERT( SUCCEEDED( hResult ) );
  1704. if (NULL != bstrPath)
  1705. SysFreeString( bstrPath );
  1706. return hResult;
  1707. }
  1708. /*****************************************************************************
  1709. Function:
  1710. Arguments:
  1711. Return:
  1712. Purpose:
  1713. Author(s):
  1714. Revision:
  1715. Date:
  1716. *****************************************************************************/
  1717. BOOL CADsSearchDataSource::BringRowInBuffer( int nRow )
  1718. {
  1719. HRESULT hResult = E_FAIL;
  1720. if( NULL == m_pSearch )
  1721. {
  1722. m_bNoMoreData = TRUE;
  1723. }
  1724. if( m_bNoMoreData )
  1725. return FALSE;
  1726. if( m_nCurrentRow == nRow )
  1727. return TRUE;
  1728. if( m_nCurrentRow != -1 )
  1729. {
  1730. ASSERT( nRow >= m_nCurrentRow );
  1731. }
  1732. while( m_nCurrentRow != nRow )
  1733. {
  1734. m_bNoMoreData = TRUE;
  1735. TRACE( _T("Asking for row %d\n"), m_nCurrentRow + 1);
  1736. hResult = m_pSearch->GetNextRow( m_hSearch );
  1737. if( hResult != S_OK )
  1738. break;
  1739. m_bNoMoreData = FALSE;
  1740. if( hResult == S_ADS_NOMORE_ROWS )
  1741. {
  1742. m_bNoMoreData = TRUE;
  1743. }
  1744. m_nCurrentRow++;
  1745. }
  1746. if( hResult == S_OK )
  1747. {
  1748. ReadColumnNames( nRow );
  1749. }
  1750. return ( hResult == S_OK );
  1751. }
  1752. /*****************************************************************************
  1753. Function:
  1754. Arguments:
  1755. Return:
  1756. Purpose:
  1757. Author(s):
  1758. Revision:
  1759. Date:
  1760. *****************************************************************************/
  1761. BOOL CADsSearchDataSource::ReadColumnNames( int nRow )
  1762. {
  1763. HRESULT hResult = E_FAIL;
  1764. WCHAR* pszColumnName;
  1765. TCHAR szColumn[ 256 ];
  1766. ASSERT( nRow == m_nCurrentRow );
  1767. if( nRow != m_nCurrentRow )
  1768. {
  1769. return FALSE;
  1770. }
  1771. m_strColumns.RemoveAll( );
  1772. ReadADsPath( );
  1773. while( TRUE )
  1774. {
  1775. hResult = m_pSearch->GetNextColumnName( m_hSearch, &pszColumnName );
  1776. //if( SUCCEEDED( hResult ) )
  1777. if( S_OK == hResult )
  1778. {
  1779. Convert( szColumn, pszColumnName );
  1780. m_strColumns.Add( szColumn );
  1781. FreeADsStr( pszColumnName );
  1782. }
  1783. //if( FAILED( hResult ) )
  1784. else
  1785. {
  1786. break;
  1787. }
  1788. }
  1789. return TRUE;
  1790. }
  1791. /*****************************************************************************
  1792. Function:
  1793. Arguments:
  1794. Return:
  1795. Purpose:
  1796. Author(s):
  1797. Revision:
  1798. Date:
  1799. *****************************************************************************/
  1800. void CADsSearchDataSource::ReadADsPath( void )
  1801. {
  1802. HRESULT hResult;
  1803. BSTR bstrColumnName;
  1804. ADS_SEARCH_COLUMN aSearchColumn;
  1805. ADS_ATTR_INFO aAttrDef;
  1806. BOOL bOK = TRUE;
  1807. COleDsSyntax* pSyntax;
  1808. CString rValue = _T("");
  1809. bstrColumnName = AllocBSTR( _T("ADsPath") );
  1810. hResult = m_pSearch->GetColumn( m_hSearch,
  1811. bstrColumnName,
  1812. &aSearchColumn );
  1813. SysFreeString( bstrColumnName );
  1814. if( SUCCEEDED( hResult ) )
  1815. {
  1816. aAttrDef.pszAttrName = aSearchColumn.pszAttrName;
  1817. aAttrDef.dwADsType = aSearchColumn.dwADsType;
  1818. aAttrDef.pADsValues = aSearchColumn.pADsValues;
  1819. aAttrDef.dwNumValues = aSearchColumn.dwNumValues;
  1820. pSyntax = new COleDsBSTR;
  1821. hResult = pSyntax->Native2Value( &aAttrDef, rValue );
  1822. delete pSyntax;
  1823. hResult = m_pSearch->FreeColumn( &aSearchColumn );
  1824. }
  1825. m_ADsPath.Add( rValue );
  1826. }
  1827.