Browse DevX
Sign up for e-mail newsletters from DevX


Find Anything with Vista's Windows Desktop Search : Page 3

Lose your dependency on third-party search engines by integrating your applications with Vista's Windows Desktop Search.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using the COM Interface
In addition to querying WDS through OLE DB Provider, you can also use an unmanaged COM interface to interact with WDS API. I'll show you an example that uses the COM component to convert an Advanced Query Syntax (AQS) query to a SQL query that can then be sent to the ExecuteReader() helper method.

AQS is a commonly-used query syntax that lets end-users define targeted search queries. It's supported in a number of Microsoft products, including Windows Vista, Outlook 2007, and Windows Desktop Search. AQS syntax is very similar to SharePoint's search syntax, if you're familiar with that. Here's an example AQS search query that returns all files authored by "Thiru" that include the word "Longhorn."

Longhorn author:Thiru kind:everything store:files

For details on AQS, refer to the MSDN documentation.

Translating AQS to WDS
Because Windows supports AQS-based searches in so many places, it would be handy to provide users with the ability to search using AQS—meaning you need to convert the AQS query to a SQL query before sending it to the OLE DB search provider. Fortunately much of the work is built into the API already via the ISearchQueryHelper interface defined in the SearchAPI.tlb type library. This interface has a method called GenerateSQLFromUserQuery that converts an AQS query to a SQL query appropriate for the WDS OLE DB Provider.

Here are the steps to use the ISearchQueryHelper interface methods from within a Windows Forms application:

  1. Create the interop assembly by importing the SearchAPI.tlb type library (located in the Lib folder of the Windows Vista SDK) using the type library import utility (tlbimp.exe)
  2. Reference the managed assembly created in step 1 from within your .NET application
  3. Import the namespace and start writing code against the various classes
To create the interop assembly, open up the Windows SDK command prompt and enter the following command on a single line:

tlbimp "C:\Program Files\Microsoft SDKs\Windows\v6.0\Lib\SearchAPI.tlb" /out:"C:\WDSManaged.dll"

After referencing the resulting assembly from your project, import the namespace at the top of the form as follows:

using WDSManaged;

Now, add a button named btnConvertAQS to the default Windows form from the previous project, and modify its Click event as follows:

private void btnConvertAQS_Click(object sender, EventArgs e) { CSearchManager manager = new CSearchManagerClass(); CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); string query = queryHelper.GenerateSQLFromUserQuery(txtInput.Text); OleDbDataReader reader = ExecuteReader(query); lstResults.Items.Clear(); while (reader.Read()) { lstResults.Items.Add(reader.GetString(0)); } reader.Close(); }

The preceding code starts by creating an instance of the search manager:

CSearchManager manager = new CSearchManagerClass();

Through the search manager class, you can get a reference to the SystemIndex catalog by calling the GetCatalog() method:

CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");

Using the catalog manager instance, you can then get a reference to the query helper class that exposes a helper method named GenerateSQLFromUserQuery() to convert an AQS query to a SQL query:

CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); string query = queryHelper.GenerateSQLFromUserQuery(txtInput.Text);

Figure 2. WDS Search API Output: Here's the search results after converting the user-entered AQS Query into SQL.
With that SQL query in hand, you can then execute the query and display the results in the list box using the code described in the previous section:

OleDbDataReader reader = ExecuteReader(query); lstResults.Items.Clear(); while (reader.Read()) { lstResults.Items.Add(reader.GetString(0)); }

Figure 2 shows the sample output generated by the form.

In this article, you saw two ways using to interact programmatically with WDS 3.0 from within your .NET applications. You also had a brief look at the new OLE DB Search Provider introduced with WDS and the steps involved in executing queries through this new provider. Finally, you saw how to translate an AQS query to a SQL query that witll execute through WDS.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Thanks for your registration, follow us on our social networks to keep up-to-date