Visual Studio Add-in: Use Vista Search directly from Visual Studio

I have used Windows Vista from last November and now I have noticed that I'm really heavy user of search. Before Vista I had a lot of problems finding my stuff because I just couldn't remember where I have saved my files. I used my own directory structures that would help me to find files easier. But still sometimes I just couldn't find them. Vista however makes this problem go away. I'll just press Windows key and start typing something relating to the item I'm looking for. And somehow my computer finds the correct results and really really fast too.

But few days ago I had and interesting idea. How could I bring that Vista search into my favorite tool: Visual Studio. So I decided to create Visual Studio Add-in that creates the query for me. I created new project with Add-in template:
Visual Studio: New project dialog 
Then I added following codes to the generated class:

 1  [DllImport("user32.dll", EntryPoint = "keybd_event")]  2  public static extern void KeyboardEvent(uint bVk,  3     uint bScan, uint dwFlags, uint dwExtraInfo);  4   5  public const uint VK_LWIN = 0x5B;  6  public const uint KEYEVENTF_EXTENDEDKEY = 0x01; 7  public const uint KEYEVENTF_KEYUP = 0x02;  8   9  public void Exec(String commandName, 10                    vsCommandExecOption executeOption, 11                   ref object varIn, ref object varOut, ref bool handled) 12  { 13    handled = false; 14    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) 15    { 16      if (commandName == "VSExplorerAddin.Connect.VSExplorerAddin") 17      { 18        handled = true; 19        EnvDTE.TextSelection selection = null; 20        try 21        { 22          selection = _applicationObject.ActiveDocument.Selection 23            as EnvDTE.TextSelection; 24          selection.GotoLine(selection.CurrentLine, false); 25          selection.EndOfLine(true); 26          String selectedText = selection.Text.Trim(); 27          selection.Text = ""; 28  29          KeyboardEvent(VK_LWIN, 0, 0, 0); 30          KeyboardEvent(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); 31          SendKeys.Send(selectedText + "{ENTER}");32        `` } 33        catch (Exception ex) 34        { 35          if (selection != null) 36          { 37            selection.Text = ex.ToString(); 38          } 39          else 40          { 41            MessageBox.Show(ex.ToString()); 42          } 43        } 44        return; 45      } 46    }47  }

Most of the stuff is put into the Exec-method, but added also KeyboardEvent (on lines 1 to 3) which is used to send the Windows key press. I needed that because I couldn't find way to send windows key press using the SendKeys. If that would be possible, it would just simplify this code a little bit more (so if you know better way then please let me know :-). But anyway the actual code is pretty simple. On lines 22 to 27 the current selection is copied into local variable and then the selection is cleared. And lines 29 and 30 just send the win key and finally on line 31 the previously stored selection is typed into the search box. And if something goes wrong (=exception is thrown) then we're setting the selection to have the error message or we'll just popup  a simple messagebox if selection is unavailable.

Do you want to see that stuff in action? Even if you don't I'm still going to show it to you :-) So first I'll just press F5 to start the project. Visual Studio actually launches new Visual Studio which can be used for testing my new addin. Then I just open any project of mine and wait it to be loaded. After that I can just type in my search word which is on line 16 (in this case word).

After I have typed in my search word I can launch my magical addin. In this case I'll use Alt-T (which opens Tools menu).
And when menu is open I'll just hit the Enter (since the addin is the first choice). After that my addin launches word as a search. Vista opens the first search result automatically because our addin sends Enter as last key press. As an end result we have Microsoft Office Word 2007 running:

This sample shows that you can easily add more functionality to Visual Studio. In this case I just added my favorite feature from Vista :) I hope you start creating you own addins rightaway! It's not that hard... believe me. Of course this could be done as a Macro as well, but I'll leave that stuff to another time.

Anyways... happy hacking!