Search code examples
delphipascalwindows-search

Windows Search 4 Query - Delphi Example


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.

Solution

  • 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