Developing with Visual Basic 6


Visual Basic 6 is still a favorite with developers today because it makes building applications easy, when they compare it to a language such as C, and still produces a native executable. Many organizations have documented the developer productivity benefits of using Visual Basic 6. You can produce both desktop and Web applications with equal ease. The design interface makes working with databases simple. In fact, if there's a problem with Visual Basic, it's that the product does too much and ends up hiding low-level functionality that developers need. Consequently, this is the language to use if you need a native code application to access Google Web Services and developer productivity is high on your list of priorities. The following sections describe how you can use Visual Basic 6 to build a Google Web Services application.

Getting the Microsoft SOAP Toolkit

Visual Studio 6 arrived on the scene well before the SOAP arrived on the scene. Consequently, none of the languages in Visual Studio 6 includes native support for SOAP. To add SOAP support, download the Microsoft SOAP Toolkit 3.0 from http://www.microsoft.com/downloads/details.aspx?FamilyId=C943C0DD-CEEC-4088-9753-86F052EC8450. Once you download the toolkit, install it on your system. Make sure you have Visual Studio 6 installed before you install the toolkit so the installation routine can make any required changes to your Visual Studio 6 setup. Note that accessing Google Web Services doesn't require all of the features provided in the Microsoft SOAP Toolkit, but it's a good idea to install everything in case you want to perform other types of SOAP development later.

Note  

Microsoft still offers the Microsoft SOAP Toolkit 2.0 SP2 for download. This version of the toolkit works fine with the examples in this book. In fact, you can download this version of the toolkit from http://www.microsoft.com/downloads/details.aspx?FamilyId=147ED727-0BE8-48A1-B1DA-D50B1EA582CB&displaylang=en. However, it's better to use the newer version of the Microsoft SOAP Toolkit if possible and the version-specific information in this section reflects that toolkit's contents.

The Microsoft SOAP Toolkit contains a number of files that you need to know about before you can create an application with it. Table 6.1 provides a list of the files (including DLLs) provided in the Microsoft SOAP Toolkit. This table tells how you'll use the DLLs within an application and the utilities to create application resources or perform testing. Note that some filenames include version information that might vary from the version you have installed. In addition, you might find the files in a slightly different location and some toolkit versions could include files not listed in Table 6.1.

Table 6.1: Files provided with the Microsoft SOAP Toolkit

DLL Name

Location

Description

MSSOAP1.DLL and MSSOAP30.DLL

\Program Files\Common Files\MSSoap\Binaries

This is the first SOAP DLL that you'll use on a high-level API access client or server. Use the MSSOAP30.DLL file for version 3.0 access. The Microsoft SOAP Library contains a number of classes, but you'll always begin by creating either a SoapClient or SoapServer object. These objects support a single method call, Init() , that tells SOAP which WSDL file to use. Other classes perform tasks such as serializing data for output and reading input. This is the DLL that you'll use for simple requests with Google Web Services.

MSSOAPR.DLL and MSSOAPR3.DLL

\Program Files\Common Files\MSSoap\Binaries\Resources\1033

This file contains resources for SOAP use in general and normally you won't need to reference it directly. The MSSOAPR3.DLL file contains resources specifically used by the 3.0 version of the toolkit.

MsSoapT3.EXE

\Program Files\MSSOAP\Binaries

Use this file to start the Microsoft SOAP Trace utility. In most cases, you won't need this diagnostic tool when working with Google Web Services. However, it's helpful to know how to use it so that you can troubleshoot problems when they occur.

SOAPIS30.DLL

\Program Files\Common Files\MSSoap\Binaries

Use this DLL to add WSDL support to your Internet Information Server (IIS). The "Specifying an ISAPI Listener" topic of the SOAP Toolkit 3.0 help file tells how to add this file to the server. It's unlikely that you'll use this file with Google Web Services, unless you create a referral type application where a user uses a Web service application on your Web server to access Google Web Services.

WHSC30.DLL

\Program Files\Common Files\MSSoap\Binaries

This DLL contains the Windows HTTP SOAP Connector Library. This library provides SOAP connector service to a remote location. You'll also use this DLL to receive and send low-level SOAP messages. Use this library for complex requests, especially when working with searches, to avoid receiving unwanted data from the server. Avoid using this library with Windows 9x systems ”use the Windows Internet Connector Library instead.

WISC10.DLL and WISC30.DLL

\Program Files\Common Files\MSSoap\Binaries

These DLLs contain the Windows Internet Connector Library. The version 3.0 toolkit includes the WISC30.DLL file. Both DLLs provide essentially the same services as the Windows HTTP Library Connector described earlier. This lower performance library works with Windows 98, Windows ME, Windows NT, and Windows 2000. As with the Windows HTTP SOAP Connector Library, use this DLL for complex requests.

WSDLGen3.DLL and WSDLGen3.EXE

\Program Files\MSSOAP\Binaries

The WSDL generation tools create the WSDL files used to describe Web services that you create. You never need to use this tool with Google Web Services because Google provides the required WSDL file for you. This file resides at http://soap.Google.com/schemas3/GoogleWebServices.wsdl for users in the United States and Japan, and http://soap-eu.Google.com/schemas3/GoogleWebServices.wsdl for users in the United Kingdom and Germany.

As you can see from Table 6.1, the library that you're going to use most often is the Microsoft SOAP Library. It contains everything you need to begin a conversation between client and server using the high-level API. What the table doesn't show is that you'll also need the latest version of the Microsoft XML Library. This library doesn't appear in the same directory as the rest of the SOAP files. The Microsoft SOAP Toolkit installation program automatically adds the latest version of the Microsoft XML Library to your System or System32 directory.

Tip  

One of the first places you should look when experiencing client-side errors in your SOAP application is the Microsoft XML Library. My workstation has four versions of this library installed. Although the library versions are clearly marked , you could find that you selected the wrong one by mistake. The current version of the Microsoft SOAP Toolkit relies on version 4 of the Microsoft XML Library found in MSXML4.DLL.

On those few occasions where you do need low-level API access, make sure you include one of the low-level API access libraries in Table 6.1. All of the sample programs provided with the Microsoft SOAP Toolkit rely on the Windows Internet Connector Library. This doesn't make the other selections any better or worse , it simply means you'll spend a little additional time figuring out how to use them. It helps that all the low-level libraries work about the same way, contain about the same classes with the same methods , and that you could theoretically use any of the three with the same boilerplate code.

Tip  

You might find that you have additional questions about the Microsoft SOAP Toolkit for your own needs. The microsoft.public.xml.soapsdk newsgroup is a good place to start asking questions about this product.

Adding a SOAP Toolkit Reference to Visual Basic 6

Before you can develop an application using the SOAP toolkit, you need to add a reference to it in your application. The following steps tell how to perform this task.

  1. Select the Project References command. You'll see a References dialog box such as the one shown in Figure 6.1.

  2. Locate the Microsoft SOAP Type Library 3.0 entry in the list and check it.

  3. Locate the Microsoft XML, v4.0 entry in the list and check it.

  4. Click OK.

click to expand
Figure 6.1: Add a reference to the appropriate type libraries using this dialog box.

You also need to add the FlexGrid control to your application to make this example work properly. (Don't confuse the FlexGrid control with the DataGrid control ”both controls use the same icon but behave differently.) To add this control, right-click the Toolbox and select Components from the context menu. Check the Microsoft FlexGrid Control 6.0 option in the Components dialog box. Click OK and you'll see the component added to your Toolbox.

Performing a Search with Visual Basic 6

The Microsoft SOAP Toolkit supports two different programming strategies called the high-level Application Programming Interface (API) and the low-level API. The high-level API hides many of the details of creating an application from the developer. It requires less code to use and is easier to understand. Most language products can use the high-level API without problem. In fact, the example in this section relies on the high-level API. The low-level API requires careful construction of the SOAP message using code. You must have a very good understanding of how SOAP works to use it, but the low-level API also offers complete control over the message flow and the most flexibility. The example in the "Developing with Visual C++ 6" section of the chapter uses the low-level API to create an application that performs the same task as the Visual Basic application in this section.

Note  

You must have the Microsoft SOAP Toolkit installed on your system to work with the examples in this section. Learn more about this toolkit in the "Getting the Microsoft SOAP Toolkit" section of the chapter.

Listing 6.1 shows how to create a search request using Google Web Services. This example includes support for modifying the country of origin, the language used on the Web pages, the use of filtering, and safe searching techniques. Everything is controlled as part of the application interface. You'll find the complete source for this example in the \Chapter 06\VB6Search folder of the source code located on the Sybex Web site.

Listing 6.1: Performing a Google Search with Visual Basic 6
start example
 Private Sub btnTest_Click()      Dim Client As SoapClient30      ' SOAP Client      Dim Results As IXMLDOMSelection ' All the results.      Dim MainNode As IXMLDOMNode     ' High-level nodes.      Dim ItemList As IXMLDOMNodeList ' All the result items.      Dim ItemNode As IXMLDOMNode     ' Result item nodes.      Dim DataList As IXMLDOMNodeList ' All the data items.      Dim DataNode As IXMLDOMNode     ' One data item.      ' Initialize the SOAP client.      Set Client = New SoapClient30      Client.MSSoapInit "http://api.google.com/GoogleSearch.wsdl", _                        "GoogleSearchService", _                        "GoogleSearchPort"      ' Make the request.      Set Results = Client.doGoogleSearch(txtLicense.Text, _                                          txtKey.Text, _                                          txtIndex.Text, _                                          txtResults.Text, _                                          CBool(cbFilter.Value), _                                          txtOrigin.Text, _                                          CBool(cbSafeSearch.Value), _                                          txtLanguage.Text, _                                          "", _                                          "")      ' Search through the main results.      For Each MainNode In Results         ' Look for specific nodes.         Select Case MainNode.baseName            ' Update the starting index.            Case "endIndex"               txtIndex.Text = CStr(CInt(MainNode.Text) + 1)            ' Display the total estimated results.            Case "estimatedTotalResultsCount"               txtEstResults = MainNode.Text            ' Determine whether the results are exact.            Case "estimateIsExact"               cbEstExact.Value = CBool(MainNode.Text)            ' Get all of the result items.            Case "resultElements"               Set ItemList = MainNode.childNodes               ' Process each item in turn.               For Each ItemNode In ItemList                  ' Make sure the element is an item.                  If ItemNode.baseName = "item" Then                     ' Enter each of the column values.                     Set DataList = ItemNode.childNodes                     For Each DataNode In DataList                        ' Process each data item.                        Select Case DataNode.baseName                           ' Get the title.                           Case "title"                              fgOutput.Col = 0                              fgOutput.Text = StringToText(DataNode.Text)                           ' Get the snippet or summary.                           Case "snippet"                              fgOutput.Col = 2                              fgOutput.Text = StringToText(DataNode.Text)                           Case "summary"                              fgOutput.Col = 2                              If fgOutput.Text = "" Then                                 fgOutput.Text = _                                    StringToText(DataNode.Text)                              End If                           ... Other Columns ...                        End Select                     Next                     ' Create a new row.                     fgOutput.AddItem " "                     fgOutput.Row = fgOutput.Row + 1                  End If               Next         End Select      Next   End Sub 
end example
 

The code begins by creating a SOAP client using the MSSoapInit() method. You must supply the location of the WSDL file, the service name, and the service port as part of the input. The example shows how to use the online version of the WSDL file. This is the best option because it ensures the WSDL file is always up-to-date. You can also provide the location of the WSDL file that comes with the Google Web Services Kit. Using the local hard drive copy is the best solution when you want to troubleshoot the message traffic between Google Web Services and your application (see the "Dealing with Difficult Languages" section of the chapter for details).

The Client calls the doGoogleSearch() method using all of the available arguments. However, the client automatically detects empty arguments and passes a blank request element to Google Web Services. Notice that you must convert the check-box values, such as cbFilter , to a Boolean using the CBool() function before you pass the information to the client. The return value from this call is an IXMLDOMSelection object, Results , which you can treat as a collection in Visual Basic.

Because Visual Basic 6 treats most XML objects as collections, you can use the For Each structure to process them. Using this technique reduces code and also makes errors less likely because you don't have to keep track of loop counter variables . The code uses three processing loops : upper or main level nodes, result item level nodes, and result data level nodes.

The return value is always text and always appears in the Text property. Each of the loops looks at the baseName property to determine the node type, and then uses the Text property to process the node. The use of a text means you must perform data transformations in some cases. For example, when you want to determine the next index value, you must convert the text into a number, perform a math calculation, and then turn it back into text for display on screen using a combination of the CStr() and CInt() functions.

The application uses simple design elements, such as textboxes, for the main data elements. However, it uses an MSFlexGrid object to display the search results, including the page title and URL. The use of a grid means keeping track of the cursor position within the grid. As you can see from the code, the application uses the Col and Row properties of fgOutput to track cursor position. However, this approach might not always work when you need to create complex displays, so you need to use external variables as necessary. Because Google doesn't leave any of the result items blank, you can simply use the fgOutput.AddItem() method shown to add a new row after processing each item. Figure 6.2 shows typical output from this application.

click to expand
Figure 6.2: Typical output for a Google search when using this application

This example includes one additional feature. Notice the call to StringToText() for some of the data elements. This function performs approximately the same task as the VBA examples in Chapter 5. The only difference is that this version of the function is slightly more capable than the VBA version and detects more tag types. See Listing 5.2 and its associated explanation in the "Modifying the Google Data" section of Chapter 5 for details.




Mining Google Web Services
Mining Google Web Services: Building Applications with the Google API
ISBN: 0782143334
EAN: 2147483647
Year: 2004
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net