Session D-WEB3

Advanced Internet and
Visual FoxPro

Rod Paddock
Dash Point Software, Inc


Introduction

The Internet has arrived and Visual FoxPro is leading the pack with some of the most powerful Internet tools. This session will discuss the tools that can be used by the Visual FoxPro developer to implement Internet solutions now. In this session you will learn:

Using The Internet Control Pack

Microsoft has developed a set of ActiveX controls that can be used by Visual FoxPro developers to create Web client components. These controls come in a package known as the Internet Control Pack. Some of the controls included in the Internet Control Pack include:

The Internet Control Pack can be downloaded from Microsoft's web sight at http://www.microsoft.com. After downloading and installing the ICP you can begin using the controls provided to create your own web solutions.

Creating Your Own Browser

One of the first things you will want to do with the Internet control pack is to create your own Web Browser. You can create your own browser with only 1 line of code. Here are the steps to take to create your own browser:

  1. Create a new form. Set its caption to "My Browser"
  2. Add an HTML Active X control to your form. You do this by selecting the OLE container control from the Form Controls Toolbar, selecting the ActiveX option and selecting the proper control from the list (see following figure). Set the name property of this control to oleHTMLControl.
  3. Add a label to your form. Set the caption property to "Please enter the URL To go to"
  4. Add a text box with the name txtURL. Set the value of this text box to "http://www.microsoft.com/vfoxpro"
  5. Add a command button. Set the caption of this button to: "Load". Go into the click event of the button and add the following code: Thisform.oleHTMLControl.RequestDoc(Alltrim(Thisform.txtURL.Value))

The following illustration shows your browser with the Microsoft Visual Foxpro Home Page loaded.

As you can see this is quite powerful technology. What are some of the other uses for this technology. Well you could enable people to look up the status of a package sent by Fedex by tapping the power of their web pages. The second form shows how to use the capabilities of the Fedex tracking system from your applications.

The basic concept behind this form is to gather the parameters necessary to feed the CGI script that the Fedex tracking system uses. Their particular script uses three parameters: The package ID number, the country shipped to and the shipdate. The following code shows how you can specify a URL with parameters gathered from your VFP forms.

lcRequest = "http://www.fedex.com/cgi-bin/track_it?trk_num="
lcRequest = lcRequest + Alltrim(Thisform.txtPackageId.Value)
lcRequest = lcRequest + "&dest_cntry=" + Thisform.cboCountry.Value
lcRequest = lcRequest + "&ship_date=" + Alltrim(Thisform.txtShipDate.Value)
Thisform.oleHTMLControl.RequestDoc(lcRequest)

As you can see just by being a little creative you can harness the power of web pages developed by other companies. Can you think of a few special links you would like to integrate into your applications ?

Creating Your Own Web Server Application

Visual FoxPro 5.0 comes with a tool known as the Internet Wizard. This tool allows you to create database enables web applications using Visual FoxPro. The Internet Wizard comes with equipped with two distinct components the WWWPAGE.APP and SERVER.APP. WWWPAGE.APP is the Wizard that you can use to create your own web pages. SERVER.APP is the application that processes these requests. This session demonstrates the steps necessary to replace the SERVER.APP application with your own server.

Steps To Creating Your Own Web Server

The server application provided with Visual FoxPro has a very simple architecture. It simply polls for a file, processes the contents of that file and returns an HTML page. This process can be diagrammed as follows:

This architecture works pretty well so we'll stick with it for our application.

Polling for Requests

The first step in creating your own server application is to set up a polling process that looks for files passed from the VFPCGI application. The VFPCGI application sends files to the TEMP directory on the computer you are running on. To create this polling process do the following:

  1. Create a new for with a timer control on it.
  2. Set the timer.Interval property to 1000 which will poll for a file every second. For finer resolution set the number lower.
  3. Add an edit box with the name edtProcessedFiles
  4. Add a new method to the form called Process file.
  5. Add a text field with the name txtTempPath, set its Value property to "c:\temp\"
  6. Add the following code to the Timer event of the Timer control you added:

*-- .atn files are written to the windows temp directory
*-- This directory is specified by a text box object on this form

LOCAL lcWildCard, lcAtnfile
*-- Create the wildcard search
lcWildCard=ALLTRIM(Thisform.txtTemppath.Value) + "*.atn"

*-- Look for files
lcAtnFile=SYS(2000,lcWildCard)

IF EMPTY(lcAtnFile)
RETURN
ELSE
*-- Set processing message
Thisform.edtProcessedfiles.Value = Thisform.edtProcessedfiles.Value + lcAtnFile + Chr(13) + Chr(10)

*-- Erase the ATN file
Erase (ALLTRIM(Thisform.txtTemppath.Value) + lcAtnFile)

*-- Process the request
Thisform.Processfile(lcAtnFile)
ENDIF

This code will: poll for files with the extension .ATN, delete the .ATN file and then pass the name of the file to the Process file method. This has one major difference from the code provided with VFP 5. It allows users to specify where the temp file path is located. The application provided with Visual FoxPro expects the TEMP directory to exist on the same drive as where the SERVER.APP application was run from.

Now that you have set up your polling algorithm you are ready to begin processing the request passed by the server. Before you can do this you need to understand what is passed to from the VFPCGI application. The VFPCGI application passes two files. The first is the ATN file that you already created a polling program for. The second is a .DAT file containing data from the web browser. Its this information that you will need to process. The following code processes the server request:

LPARAMETERS pcAtnFileName
*-- Create a series of data file names
Local lnHandle, lcContents
lcDatafile = ALLTRIM(Thisform.txtTemppath.Value) + JUSTSTEM(pcAtnFileName) + ".DAT"
lcReturnfile = ALLTRIM(Thisform.txtTemppath.Value) + JUSTSTEM(pcAtnFileName) + ".DAT"
lcAckFile = ALLTRIM(Thisform.txtTemppath.Value) + JUSTSTEM(pcAtnFileName) + ".ACK"

If File(lcDataFile)

*-- Open the passed data file
lnHandle = FOPEN(lcDataFile)
lcContents = ""

*-- Create a string with the data from the .DAT file
Do While Not FEOF(lnHandle)
lcContents = lcContents + FREAD(lnHandle,1000)
EndDo
Fclose(lnHandle)

*-- Process contents of the array
Thisform.CreateArrayOfContents(lcContents)
Thisform.CreateResponse(lcReturnfile, lcAckFile)
Else
Thisform.edtProcessedFiles.Value = Thisform.edtProcessedFiles.Value + "Data file [" + lcDataFile + "] not found." + CHR(13) + CHR(10)
Thisform.CreateError(lcReturnfile, lcAckFile)
Endif

This code opens the .DAT file, creates a string variable with the contents of the file, passes the string to a process that creates an array of the data found in the .DAT file and then returns a web page. Each of these steps is further illustrated below:

Processing the Contents of a .DAT file.

The data found in the .DAT file consists of a string delimited with ampersand characters (&). Each item delimited consists of two values: the name of the variable passed and the value passed. In order to utilize this information you need to parse it into a more usable format. This is where the CreateArrayOfContents method comes in handy. This method takes a string and parses it into the ContentsArray property found on the RodServ form. Upon doing this you can use the information to process data. The following code parses this string:

Lparameters pcContents

*-- Find out how many items we have
lnOccurs = OCCURS("&",pcContents)

*-- Size the array
Dimension This.ContentsArray[lnOccurs,3]

*-- Move through the values
For lnKount = 1 to lnOccurs

*-- Get the first item
If lnKount = 1
lnBegin = 1
lnCharsToMove = ATC("&",pcContents,1) - 1
Else
*-- Get subsequent items
lnBegin = ATC("&",pcContents,lnKount) + 1
lnCharsToMove = ATC("&",pcContents,lnKount + 1) - lnBegin
Endif

*-- Pull the string
lcstring = SUBSTR(pcContents,lnBegin,lnCharsToMove)

*-- get the Variable Name
leftValue = Substr(lcString,1,ATC("=",lcString,1) - 1)

*-- Get the value
rightValue = Right(lcString,Len(lcString) - ATC("=",lcString,1))

*-- Store the results to the Contents Array
Thisform.ContentsArray[lnKount,1] = lcString
Thisform.ContentsArray[lnKount,2] = rightValue
Thisform.ContentsArray[lnKount,3] = leftValue
EndFor

Creating a Return Page

After processing the contents of the web request you can create an HTML page to be returned to the browser. Included on your disk is a program file called HTML.PRG. This program contains a class called cHTML. This class facilitates the creation of Web Pages. The following code demonstrates creating a return page with this class:

Lparameters pcReturnfile, pcAckFile

*-- Instantiate the HTML page creation class
oHtml = createobject("cHTML")

*-- Create the title
oHTML.createtitle("German FoxPro Devcon 1996")

*-- Create the first header line
oHtml.createHeaderLine("German FoxPro DevCon 1996",1)

*-- Create the second header line
oHTML.createHeaderLine("Frankfurt Germany",2)

*-- Create hard return
oHTML.CreateHardReturn()

*-- Add some text
oHTML.createTextLine("The Server Was Passed the following information:")

*-- Return the information passed to the server
For lnKount = 1 To Alen(This.ContentsArray,1)
oHTML.createTextLine(This.ContentsArray[lnKount,1])
Endfor

*--- Create hard return
oHtml.CreateHardReturn()

*-- Return a CGI Page
lc_string = oHtml.ReturnHTMLPage(.t.)

*-- Create the return file
lnReturnHandle = FCREATE(pcReturnfile)

*-- Set the HTML to the return file
FPUTS(lnReturnHandle,lc_String)
FCLOSE(lnReturnHandle)

*-- Ack (Acknowledge the completion)
lnAckHandle = FCREATE(pcAckFile)
FCLOSE(lnAckHandle)

This web page returns the contents of the .DAT file passed to the server program. As you can see by the following illustration you can return quite a bit of information about the calling program:

The important aspect of this information is that you can customize your web responses according to the capabilities of the browser that is requesting data. If you know that a particular version of Netscape supports a special set of tags you can exploit that capability or you can add extra information such as sound or video if they are installed.

Using ISAPI

Until now you have explored the ideas of using the Internet Control Pack to create web browsers and creating your own Visual FoxPro CGI enables web pages. Now comes one of the coolest capabilities for developing internet enabled applications: ISAPI. ISAPI allows Web developers to instantiate OLE servers from their web pages. What this means is you can directly launch your own Visual FoxPro OLE Automation servers from your web pages, meaning your investment in Visual FoxPro and OOP is going to pay off in spades. Now how do you do this ?

To create your own Visual FoxPro/ISAPI enabled web page you need to do the following:

  1. Copy the FOXISAPI.DLL file to either your HTML Web Pages or scripts directory. I recommend using the FOXISAPI.DLL found on your session CD as the one provided with VFP seems to have problems. The one on the session CD was created by Rick Strahl and seems to do a good job.
  2. Create a web page with the following tag: <A HREF="foxisapi.dll/<OLE server application name>.<OLE Server class name>.<method name>?"> <I> FoxISAPI Test</I></A>

Your HTML code and web page will look like:

<HTML>
<HEAD>
<TITLE>German FoxPro DevCon 1996</TITLE>
<H1>German FoxPro DevCon 1996</H1>
<H2>Frankfurt, Germany</H2>
<HR>
</HEAD>
<BODY>
<A HREF="foxisapi.dll/gdcisapi2.gdc_server2.test_page?"> <I> FoxISAPI Test</I>
</A>
</BODY>
</HTML>

This will instantiate the OLE server gdcisapi2.gdc_server2 calling the method test_page. Your web page will look limne

  1. After creating your web page you need to create your web server. Create a project with the same name as the server name specified in your Web page. In this example your project will be names gcisapi2. Create a class with same name specified in your Web page. In this example your class name will be named gdc_server2. Add a method to your class with the same name as specified in your Web page. In this example your method should be called test_page. Lastly go into the Class-Class-Info dialog in Visual FoxPro and mark your class as OLE Public (See below)

  1. Now that you have created your class you can add come code to the test_page method. Add the folllowing code:

LPARAMETER lcFormVars, lcIniFile, lnReleaseFlag

*-- Make sure the HTML Class is availiable
Set Talk Off
Set Procedure To html Additive

*-- Instantiate the HTML page creation class
oHtml = createobject("cHTML")

*-- Create the title
oHTML.createtitle("German FoxPro Devcon 1996")

*-- Create the first header line
oHtml.createHeaderLine("German FoxPro DevCon 1996",1)

*-- Create the second header line
oHTML.createHeaderLine("Frankfurt Germany",2)

*-- Create hard return
oHTML.CreateHardReturn()

*-- Add some text
oHTML.createTextLine(" ISAPI Test Line")
oHTML.createTextLine(" ISAPI Test Line2")

*-- Create hard return
oHTML.CreateHardReturn()

*-- Return a CGI Page
Return oHtml.ReturnHTMLPage(.t.)

  1. Now build your application.

Tip

You can build your application as either an EXE or an OLE DLL. I recommend that you build your application as an EXE. The reason for this is because when you create your OLE server it may have bugs. OLE servers are in memory processes and it seems that the DLL version do not show any processes that you can kill where the EXE versions do.

  1. The last set of steps is to register your server with window you do this by calling one of the OLE registration tools.

If you created an OLE .EXE server you simply go to the command window and type <MYSERVER.EXE> /regserver.

If you created and OLE .DLL you need to run the regsvr32 utility provided with windows. If you are on Windows NT 4.0 you also need to run the DCOMCNFG utility to register the users for your server. Information on this process can be found in the SAMPLES\SERVERS\FOXISAPI\README.TXT file or on the west-wind.com web site.

Finally all you need to do is go to your web browser and activate the application.

Conclusion

As you can see Visual FoxPro provides a powerful combination of tools to integrate your applications with the internet. You can add a browser to your FoxPro forms with very few lines of code. You can create your own high speed CGI servers and you can use your own Visual FoxPro OLE servvers from your web pages. In fact there is not much you cannot do with Visual FoxPro.