This article provides an easy to use alternative to querying Google via your program through the Google API and SOAP. Many people don't wish to take the time to download the official Google API package and integrate it into their project, so this simple class makes querying Google for search results child's play.
Before You Begin
Before you jump into this article and code, there are several things I'd like to address. I'll keep the brevity of each at a non-intrusive level.
#1: The most important
Google is a wonderful service provided to us free of charge, please respect this and refrain from abusing Google. Try to keep your queries minimal, don't bombard Google's servers with thousands of needless requests.
#2: Susceptible to change
Google definitely isn't immune to change. If Google heavily modifies the format in which results are returned, this class may fail to retrieve the proper results. Such a possibility is very rare in the short-term future, but if it does occur, I'll update
CGoogle to comply with any new formatting standards.
#3: A note about MFC
I'm certain there are many of you weeping (I can dream, can't I?) as you learn this class is MFC dependent. But don't runaway, there is good news! Very shortly, I'll provide a non-MFC class version, which will be my personal preference over the MFC dependency.
Using the code
In the past, I've been criticized for publishing articles which are needlessly long, so I'll attempt to restrain myself on this article. Particularly because there isn't much to explain, using Google is simple, so shouldn't accessing it with your program be simple as well? My opinion revolves around an obvious yes.
Throughout this class are six different public access functions, but most likely you'll only use four of them. I'll start with the most important function first.
If and when you're ready to search Google, you employ
PerformSearch to do the work for you. The function takes only one parameter, which is a character array containing your search term(s).
PerformSearch returns true on success and false on failure.
You can enable or disable your search to be parentally safe with
SetSafeSearch. By default, safe search is disabled.
Getting the Result Count
When you are ready to retrieve the results, you'll need to know how many results were found so you do not unintentionally step out of bounds. Calling
GetResultCount will clearly return the number of results found.
After you've called
PerformSearch, you'll surely want to retrieve individual results for further processing, you may do so with either the subscript operator or by using
GetAt will return a pointer to the result located at the given zero-based index; if the result does not exist at the specified position,
NULL is returned.
The subscript operator will return a reference to the corresponding result, however, it isn't bounds safe (meaning it will fail if the passed index is less than zero or greater than or equal to
The base return type shared by both is
GoogleResult, which is a structure conveniently containing the result's URL (
cstrURL), title (
cstrTitle), and description (
But what is an article without examples? Certainly not appealing? For those of you that may find yourselves in a predicament when it comes to using this class, perhaps this keen example will provide itself to be a useful reference:
/* The following will search google for "The Code Project"
and display the first 3 results in message boxes */
if (!google.PerformSearch( "\"The Code Project\"" ))
return false; // Failed
size_t nResultCount = google.GetResultCount();
for ( size_t n=0; n < nResultCount && n < 3; ++n)
::MessageBox( NULL, google[n].cstrTitle, "Result Title",
::MessageBox( NULL, google[n].cstrURL, "Result URL",
::MessageBox( NULL, google[n].cstrDesc, "Result Description",
- March 23, 2005 - Began work on
- March 24, 2005 - Submitted
CGoogle to The Code Project.
- April 06, 2005 - Fixed minor bounds bug.
- April 10, 2005 - Fixed bug affecting description-less results.
- November 7, 2005 - Updated code to be compatible with new Google search results.
- November 29, 2005 - Updated code to be compatible with new Google search results.