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.

285 lines
8.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6. // PARTICULAR PURPOSE.
  7. //
  8. // Copyright 1998-1999 Microsoft Corporation. All Rights Reserved.
  9. //
  10. // PROGRAM: isrchdmp.cx
  11. //
  12. // PURPOSE: Illustrates a minimal query using Indexing Service.
  13. //
  14. // PLATFORM: Windows NT
  15. //
  16. //--------------------------------------------------------------------------
  17. #ifndef UNICODE
  18. #define UNICODE
  19. #endif //ndef UNICODE
  20. #include <stdio.h>
  21. #include <windows.h>
  22. #define OLEDBVER 0x0250 // need the command tree definitions
  23. #define DBINITCONSTANTS
  24. #include <oledberr.h>
  25. #include <oledb.h>
  26. #include <cmdtree.h>
  27. #include <ntquery.h>
  28. #include <filter.h>
  29. #include "isearch.h"
  30. #include "array.hxx"
  31. //+-------------------------------------------------------------------------
  32. //
  33. // Template: XInterface
  34. //
  35. // Synopsis: Template for managing ownership of interfaces
  36. //
  37. //--------------------------------------------------------------------------
  38. template<class T> class XInterface
  39. {
  40. public:
  41. XInterface( T * p = 0 ) : _p( p ) {}
  42. ~XInterface() { if ( 0 != _p ) _p->Release(); }
  43. T * operator->() { return _p; }
  44. T * GetPointer() const { return _p; }
  45. IUnknown ** GetIUPointer() { return (IUnknown **) &_p; }
  46. T ** GetPPointer() { return &_p; }
  47. void ** GetQIPointer() { return (void **) &_p; }
  48. T * Acquire() { T * p = _p; _p = 0; return p; }
  49. private:
  50. T * _p;
  51. };
  52. typedef void (__stdcall * PFnCIShutdown)(void);
  53. typedef HRESULT (__stdcall * PFnMakeISearch)( ISearchQueryHits ** ppSearch,
  54. DBCOMMANDTREE * pRst,
  55. WCHAR const * pwcPath );
  56. PFnCIShutdown g_pCIShutdown = 0;
  57. PFnMakeISearch g_pMakeISearch = 0;
  58. //+-------------------------------------------------------------------------
  59. //
  60. // Function: DoQuery
  61. //
  62. // Synopsis: Creates and executes a query, then displays the results.
  63. //
  64. // Arguments: [pwcQueryCatalog] - Catalog name over which query is run
  65. // [pwcQueryMachine] - Machine name on which query is run
  66. // [pwcQueryRestrition] - The actual query string
  67. // [fDisplayTree] - TRUE to display the command tree
  68. //
  69. // Returns: HRESULT result of the query
  70. //
  71. //--------------------------------------------------------------------------
  72. HRESULT DoQuery(
  73. WCHAR const * pwcFilename,
  74. WCHAR const * pwcQueryRestriction )
  75. {
  76. // Create an OLE DB query tree from a text restriction
  77. DBCOMMANDTREE * pTree;
  78. HRESULT hr = CITextToSelectTree( pwcQueryRestriction, // the query itself
  79. &pTree, // resulting tree
  80. 0, // no custom properties
  81. 0, // no custom properties
  82. GetSystemDefaultLCID() ); // default locale
  83. if ( FAILED( hr ) )
  84. return hr;
  85. // Make the ISearchQueryHits object
  86. XInterface<ISearchQueryHits> xISearch;
  87. hr = g_pMakeISearch( xISearch.GetPPointer(),
  88. pTree,
  89. 0 );
  90. if ( FAILED( hr ) )
  91. return hr;
  92. XInterface<IFilter> xIFilter;
  93. hr = LoadIFilter( pwcFilename, 0, xIFilter.GetQIPointer() );
  94. if ( FAILED( hr ) )
  95. return hr;
  96. ULONG ulFlags;
  97. hr = xIFilter->Init( IFILTER_INIT_CANON_PARAGRAPHS |
  98. IFILTER_INIT_CANON_HYPHENS |
  99. IFILTER_INIT_APPLY_INDEX_ATTRIBUTES,
  100. 0,
  101. 0,
  102. &ulFlags );
  103. if ( FAILED( hr ) )
  104. return hr;
  105. hr = xISearch->Init( xIFilter.GetPointer(), ulFlags );
  106. if ( FAILED( hr ) )
  107. return hr;
  108. //
  109. // Retrieve all the hit info. the info is wrt output from the IFilter.
  110. // a separate pass over a different IFilter is needed to match up
  111. // text to the hit info.
  112. //
  113. TArray<FILTERREGION> aHits;
  114. ULONG cRegions;
  115. FILTERREGION* aRegion;
  116. hr = xISearch->NextHitOffset( &cRegions, &aRegion );
  117. while ( S_OK == hr )
  118. {
  119. for ( ULONG i = 0; i < cRegions; i++ )
  120. aHits.Append( aRegion[i] );
  121. CoTaskMemFree( aRegion );
  122. hr = xISearch->NextHitOffset( &cRegions, &aRegion );
  123. }
  124. for ( ULONG i = 0; i < aHits.Count(); i++ )
  125. printf( "hit %d, chunk %d start %d extent %d\n",
  126. i, aHits[i].idChunk, aHits[i].cwcStart, aHits[i].cwcExtent );
  127. return hr;
  128. } //DoQuery
  129. //+-------------------------------------------------------------------------
  130. //
  131. // Function: Usage
  132. //
  133. // Synopsis: Displays information about how to use the app and exits
  134. //
  135. //--------------------------------------------------------------------------
  136. void Usage()
  137. {
  138. printf( "usage: isrchdmp query [/f:filename]\n\n" );
  139. printf( " query an Indexing Service query\n" );
  140. printf( " /f:filename filename to search\n" );
  141. exit( -1 );
  142. } //Usage
  143. HINSTANCE GetFunctions()
  144. {
  145. HINSTANCE h = LoadLibrary( L"query.dll" );
  146. if ( 0 != h )
  147. {
  148. #ifdef _WIN64
  149. char const * pcCIShutdown = "?CIShutdown@@YAXXZ";
  150. char const * pcMakeISearch = "?MakeISearch@@YAJPEAPEAUISearchQueryHits@@PEAVCDbRestriction@@PEBG@Z";
  151. #else
  152. char const * pcCIShutdown = "?CIShutdown@@YGXXZ";
  153. char const * pcMakeISearch = "?MakeISearch@@YGJPAPAUISearchQueryHits@@PAVCDbRestriction@@PBG@Z";
  154. #endif
  155. g_pCIShutdown = (PFnCIShutdown) GetProcAddress( h, pcCIShutdown );
  156. if ( 0 == g_pCIShutdown )
  157. {
  158. FreeLibrary( h );
  159. return 0;
  160. }
  161. g_pMakeISearch = (PFnMakeISearch) GetProcAddress( h, pcMakeISearch );
  162. if ( 0 == g_pMakeISearch )
  163. {
  164. FreeLibrary( h );
  165. return 0;
  166. }
  167. }
  168. return h;
  169. } //GetFunctions
  170. //+-------------------------------------------------------------------------
  171. //
  172. // Function: wmain
  173. //
  174. // Synopsis: Entry point for the app. Parses command line arguments
  175. // and issues a query.
  176. //
  177. // Arguments: [argc] - Argument count
  178. // [argv] - Arguments
  179. //
  180. //--------------------------------------------------------------------------
  181. extern "C" int __cdecl wmain( int argc, WCHAR * argv[] )
  182. {
  183. WCHAR const * pwcFilename = 0;
  184. WCHAR const * pwcRestriction = 0; // no default restriction
  185. // Parse command line parameters
  186. for ( int i = 1; i < argc; i++ )
  187. {
  188. if ( L'-' == argv[i][0] || L'/' == argv[i][0] )
  189. {
  190. WCHAR wc = (WCHAR) toupper( argv[i][1] );
  191. if ( ':' != argv[i][2] && 'D' != wc )
  192. Usage();
  193. if ( 'F' == wc )
  194. pwcFilename = argv[i] + 3;
  195. else
  196. Usage();
  197. }
  198. else if ( 0 != pwcRestriction )
  199. Usage();
  200. else
  201. pwcRestriction = argv[i];
  202. }
  203. // A query restriction is necessary. Fail if none is given.
  204. if ( 0 == pwcRestriction )
  205. Usage();
  206. // Load query.dll entrypoints
  207. HINSTANCE h = GetFunctions();
  208. if ( 0 == h )
  209. {
  210. printf( "can't load query.dll entrypoints\n" );
  211. return -1;
  212. }
  213. HRESULT hr = CoInitialize( 0 );
  214. // Run the query
  215. if ( SUCCEEDED( hr ) )
  216. {
  217. hr = DoQuery( pwcFilename, pwcRestriction );
  218. g_pCIShutdown();
  219. CoUninitialize();
  220. }
  221. if ( FAILED( hr ) )
  222. {
  223. printf( "the query '%ws' failed with error %#x\n",
  224. pwcRestriction, hr );
  225. return -1;
  226. }
  227. FreeLibrary( h );
  228. printf( "done!\n" );
  229. return 0;
  230. } //wmain