Click here to Skip to main content
15,176,823 members
Articles / Desktop Programming / WPF
Posted 1 Nov 2007


18 bookmarked

How to Use Windows Vista Search API from a WPF Application

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
1 Nov 2007CPOL2 min read
This article explains the proper way of using Windows Vista Search provider inside a WPF application. It incorporates DataBinding, Query provides and other useful technologies, introduces within Windows Vista and Windows Presentation Foundation


Another great service provided by Windows Vista is its integrated search. Can we use it from our WPF application? Sure we can. This is how you'll do it.


First of all, you'll need to find Windows Search API library inside Windows SDK. Locate SearchAPI.tlb and process it with tlbimp tool to create managed assembly to reference to. You'll get a file, named SeachAPILib.dll. This one, you will need. In order to use it, refer to MSDN documentation of Windows Search API 3.0. Actually, using the Search API is not much different than using database queries. However, this is not so trivial in core. Actually, the syntax of Windows Search (AQS - Advanced Query Syntax) is very different. But the search team created brilliant work to make our life easier. CSearchManager and ISeachQueryHelper - those translate AQS to SQL and, even provide us with a proper connection string.

Using the Code

First of all, we'll have to create CSeachManager:

CSearchManager cManager = new CSearchManagerClass(); 

Then, while you are in query, you'll create new ISearchQueryHelper to help you with translations and query strings:

ISearchQueryHelper cHelper = cManager.GetCatalog("SYSTEMINDEX").GetQueryHelper();
cHelper.QuerySelectColumns = "\"System.ItemNameDisplay\""; 

Well done, now. Ask Vista with old good OleDB provider:

using (cConnection = new OleDbConnection(cHelper.ConnectionString)) 
using (OleDbCommand cmd = new OleDbCommand( 
  if (cConnection.State == ConnectionState.Open) 
   using (OleDbDataReader reader = cmd.ExecuteReader()) 
     while (!reader.IsClosed && reader.Read()) 

Brilliant. Let's move it into WPF by creating DependencyObject, that provides us with all we need for search - search string and array of results:

public static readonly DependencyProperty SearchTextProperty = 
    ("SearchText", typeof(string), typeof(VistaSearchProviderHelper), 
new UIPropertyMetadata(default(string),
new PropertyChangedCallback(OnSearchTextChanged)));

static void OnSearchTextChanged
    (DependencyObject sender, DependencyPropertyChangedEventArgs e)
 ToAbortFlag = IsWorkingFlag;
 if (workerCallback == null) workerCallback = new WaitCallback(doSearch);
 searchObjState.SearchString = e.NewValue.ToString();
//if (searchObjState.SearchString.Length > 2)
  workerCallback.BeginInvoke(searchObjState, null, null);
public string SearchText
  get { return GetValue(SearchTextProperty).ToString(); }
  set { SetValue(SearchTextProperty, value); }
static ThreadSafeObservableCollection<string> m_results;
public ReadOnlyObservableCollection<string> Results
 get { return new ReadOnlyObservableCollection<string>(m_results); }

This code uses a custom class named ThreadSafeObservableCollection, introduced earlier in my blog.

The only thing we should do now is to bind input and output to the XAML presentation:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=searchStr}">
  <ObjectDataProvider ObjectType="l:VistaSearchProviderHelper"/>
 <TextBox Name="searchStr" Text="{Binding Path=SearchText, 
    UpdateSourceTrigger=PropertyChanged }" FontSize="30"/>
 <ListBox ItemsSource="{Binding Path=Results}" />


Now we can search our Windows Vista from a custom WPF application. This is not the final application for sure. There is a long way to go to make it work perfectly, but this is the beginning of how to do it.


  • 1st November, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tamir Khason
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.

To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions

-- There are no messages in this forum --