views:

97

answers:

1

The following web page describes querying Windows Search programmatically:

http://msdn.microsoft.com/en-us/library/aa965362.aspx

Does anyone have examples using Delphi/Pascal?

Examples I have in mind are fairly simple:

  1. Search for certain file types.
  2. Search for specific text within files.
  3. Limit these above searches to a certain path.
+2  A: 

Here's one I did a while ago - be aware that it may be out of date:

const
  GENERAL_COLUMNS = '"System.Itemname", "System.Size", "System.DateCreated", "System.ItemDate",' +
                    '"System.ItemFolderPathDisplay", "System.Search.AutoSummary", "System.ItemType"';

  IMAGE_COLUMNS = '"System.Image.HorizontalSize", "System.Image.VerticalSize", '+
                  '"System.Image.BitDepth", "System.Image.Compression", '+
                  '"System.Photo.CameraModel", "System.Photo.DateTaken", "System.Photo.Flash"';
  MUSIC_COLUMNS = '"System.Music.Artist", "System.Music.Genre", "System.Music.TrackNumber", '+
                  '"System.Audio.Compression", "System.Audio.SampleRate", '+
                  '"System.DRM.IsProtected", "System.Music.AlbumTitle", "System.Rating", '+
                  '"System.Audio.EncodingBitrate"';

procedure TWDSDataSource.RetrieveDataFromDB;
var
  manager : ISearchManager;
  catalogManager : ISearchCatalogManager;
  queryHelper : ISearchQueryHelper;
  wQuery : string;
  temp : PWideChar;
  sTemp : string;
begin
  manager := CoCSearchManager.Create;
  if Succeeded(manager.GetCatalog('SystemIndex',catalogManager)) then
  begin
    if Succeeded(catalogManager.GetQueryHelper(queryHelper)) then
    begin
      if fMaxResults  0 then
        queryHelper.Set_QueryMaxResults(fMaxResults);

      queryHelper.Set_QuerySelectColumns(GENERAL_COLUMNS + ',' + MUSIC_COLUMNS + ',' + IMAGE_COLUMNS);
      queryHelper.GenerateSQLFromUserQuery(PWideChar(fQuery),temp);
      wQuery := temp;

      queryHelper.Get_ConnectionString(temp);
      sTemp := temp;
      dataset := CreateComObject(CLASS_Recordset) as _Recordset;
      dataset.CursorLocation := adUseClient;
      dataset.Open(wQuery, stemp, adOpenForwardOnly, adLockReadOnly, adCmdText);
      dataset.Set_ActiveConnection(nil);
      bDatabaseFailed := false;
    end else
      bDatabaseFailed := true;
  end else
    bDatabaseFailed := true;
end;

I think it's all pretty self explanatory, fQuery is the query you want to execute.

Regards Keith

Keith Giddings
Thanks for the sample code. What is in your USES clause for this unit?
Greg Bishop
AdoDB, Windows, SysUtils, ComObj, Graphics, Variants, ShellApi, ShlObj, AdoInt, JPeg, Utils, SearchAPILib_tlb, ThreadSafeJpeg;Most are probably irrelevant.
Keith Giddings
I found that I need the following uses clause:uses Windows, SysUtils, Classes, DB, ComObj, AdoInt, SearchAPILib_TLB, ADODB;One last follow-up (I think): What kind of component is dataset defined as? I'd assumed it is TADODataset compoenent, but I'm getting a compile error that the Open procedure has too many parameters.
Greg Bishop
I tend to use the ADO type library directly rather than using a delphi component, hence the call to CreateComObject - ie dataset is a _Recordset.
Keith Giddings
I've got it working now. Thanks so much for your help.
Greg Bishop