Wednesday, May 2, 2007

Using Google Desktop from .NET

In this post I'm going to show how to use the Google Desktop COM API from .NET .

Google Desktop provides a couple of ways to query its index: using the local HTTP server or using the COM Query API . In this post the COM API will be used from C#.

The first step to start working with this API in .NET is to import the typelib


C:\temp\tst>tlbimp <google-desktop-path>\GoogleDesktopAPI2.dll


This will generate the file GoogleDesktopAPILib.dll.


The next thing you need to do is to register the application that will be using the COM API. Here's the code to register the application (taken from the JScript examples ):


using GoogleDesktopAPILib;

...

static int Register() {
int cookie;
object[] description = new object[]{
"Title",
" tests",
"Description",
"Simple tests",
"Icon",
"My Icon@1"
};

string myGuid = "{5323E036-345C-4323-548D-32AA55603215}";
GoogleDesktopRegistrar registar
= new GoogleDesktopRegistrar();


registar.StartComponentRegistration(myGuid,description);
object regObjObj =
registar.GetRegistrationInterface(
"GoogleDesktop.QueryRegistration");

IGoogleDesktopRegisterQueryPlugin q =
regObjObj as IGoogleDesktopRegisterQueryPlugin;

if (q == null) {
throw new Exception("Registration problem");
}

cookie = q.RegisterPlugin(myGuid,true);

registar.FinishComponentRegistration();

return cookie;
}




Registration needs to be executed just once. The returned cookie value could be stored somewhere and reused when the application is executed. For example the JScript demos in the GD SDK use the registry to store the cookie. In this demo I'm going to use a text file to store the cookie.


static int GetRegistrationCookie() {
int cookie;
FileInfo cookieFile = new FileInfo(CookieFileName);
if (cookieFile.Exists)
{
StreamReader reader =
new StreamReader(cookieFile.FullName);
string line = reader.ReadLine();
cookie = int.Parse(line.Trim());
reader.Close();
}
else
{
cookie = Register();
StreamWriter writer =
new StreamWriter(CookieFileName);
writer.WriteLine( cookie.ToString() );
writer.Close();

}
return cookie;
}


Now that we have the registration cookie we can call the Query API . This API is accessed with the GoogleDesktopQueryAPI class. This class has a Query method which receives the registration cookie, a string with the query, and some options. Here's the source for our Main method of a program which receives a query from the command line and prints the location of the matching resources:


public static void Main(string[] args)
{
if (args.Length == 1 && args[0] == "-unregister") {
UnRegister();
} else {
int cookie = GetRegistrationCookie();
string queryString = string.Join(" ",args);

GoogleDesktopQueryAPI queryAPI =
new GoogleDesktopQueryAPI();

IGoogleDesktopQueryResultSet rs =
queryAPI.Query(cookie,queryString,null,null);

IGoogleDesktopQueryResultItem2 i;
while ((i = (IGoogleDesktopQueryResultItem2)rs.Next()) != null) {
Console.WriteLine(i.GetProperty("uri"));
}
}
}


There's a lot of properties that can be extracted from a result item depending on its schema, for example if the result is a mail message.

Code for this post can be found here.

9 comments:

Jorge Monasterio said...

Cool.

Can you post the Unregister() function?

Thanks,
Jorge.

Anonymous said...

get "Your administrative policy does not allow installing Google Plug-ins." in GoogleDesktopRegistrar.StartComponentRegistration()

abhinith said...

I reviewed the code and able to understand how it works.But after applying i am getting error like

associated data is missing(0x80040002(OLE_E_ENUM_NOMORE)in queryApi.Query(cookie,queryString,null,null) line.

Can u fix this Problem.Waiting for ur answer

Luis Diego Fallas said...

Sadly at this moment I don't have access to a computer with VS.NET and Google desktop I need some time to find the problem.

abhinith said...

Sir,
I am developing an application using C#.Using C# we have to pass string as argument to Google desktop using query API. In query Api i am passing string,so that it searches that word in the number of text files.But problem is I am getting problem with Query API line as ArgumentException Unhandled.

Can u give me some idea about queryAPI.Query line and its perfect format.Do we have to add Execute() for execution?
Please reply,its urgent....

Luis Diego Fallas said...

I added a link to the code of a complete example. Maybe your problem is related to the 'cookie' parameter. Check out the example.

abhinith said...

Thanks for posting the code.It will help me alot for my proj.
Did you use GD_SDK?If yes which version of GD_SDK did you use & where? Please reply.Thank you once again...

Luis Diego Fallas said...

I didn't use the SDK directly, just generated the interop assembly as specified above.

Rafidheen said...

Hi,
i am getting this error
"Can't enumerate any more, because the associated data is missing (Exception from HRESULT: 0x80040002 (OLE_E_ENUM_NOMORE))"

from the below line
IGoogleDesktopQueryResultSet rs =
queryAPI.Query(cookie , queryString, elementString, null);

Please help to resolve this issue