CGI Programming & Visual Basic

by Ofer Laor





Listing One

Private Function ReadPOST() As String

    Static sText As String

    If (sText <> "") Then

        ReadPOST = sText

        Exit Function

    End If

    Dim lWritten As Long, lReserved As Long

    Dim hConsole As Long

    

    sText = Space(MaxPost)

    lReserved = 0

    If (hConsole = 0) Then

        hConsole = GetStdHandle(STD_INPUT_HANDLE)

    End If

    ReadFile hConsole, sText, Len(sText) - 1, lWritten, lReserved

    sText = Trim$(sText)

    sText = Left$(sText, Len(sText) - 1) ' kill \0

    ReadPOST = sText

End Function





Listing Two

Public Function ReadParam(sName As String) As String

On Error GoTo EndReadParam ' parameter that hasn't shown

    ReadParam = ""

    ReadParam = cParameters(sName).Value

EndReadParam:

    Exit Function

End Function



Listing Three

For Each param in cgi.cParameters.

If param.Name Like "Quantity######" then

        ... do something ...

    End If

Next





Listing Four

Option Explicit

Sub main()

    Dim cgi As New oCGI2

    

    cgi.WriteCGI "<HTML>"

    cgi.WriteCGI "<H1> HELLO WORLD! </H1>"

    cgi.WriteCGI "</HTML>"

End Sub





Listing Five

Option Explicit

Sub main()

    Dim cgi As New oCGI2

    cgi.ProcessCGI Command

    

    Dim sHTML As String

    sHTML = "<HTML>"

    sHTML = sHTML + "<H1> HELLO " + cgi.ReadParam("Name")

    sHTML = sHTML + " WORLD! </H1>"

    sHTML = sHTML + "</HTML>"

    

    cgi.WriteCGI sHTML

End Sub





Listing Six

Option Explicit

Sub main()

    Dim cgi As oCGI2

    Set cgi = New oCGI2

    

    cgi.ProcessCGI Command

    cgi.WriteCookie "UserName", cgi.ReadParam("UserName"), _

        cgi.ReadServerName, cgi.ReadScriptName

        

    cgi.WriteCGI "<HTML>"

    cgi.WriteCGI "The cookie has been set! <BR>"

    cgi.WriteCGI "</HTML>"

End Sub



Listing Seven

Declare Function oCGI2Register Lib "oCGI2.dll" Alias _

          "DllRegisterServer" () As Long



Sub Main()

    On Error GoTo NormalErr

    oCGI2Register



    Dim cgi As New oCGI2

    cgi.ProcessCGI Command



  ... REST OF APPLICATION HERE ...



    End

NormalErr:

    Beep

    End

End Sub



Listing Eight

Option Explicit

Declare Function oCGI2Register Lib "oCGI2.dll" Alias _

      "DllRegisterServer" () As Long

Dim cgi As New oCGI2

Sub HelloWorld()

  Select Case (cgi.ReadParam("type"))

    Case "jpeg"

       Open App.Path + "\helloworld.jpg" For Binary Access Read As #1

       PipeMIME "image/jpeg", 1

       Close #1

    Case ""

        cgi.WriteCGI "<HTML><HEAD>"

        cgi.WriteCGI "<TITLE>Hello World </TITLE></HEAD>"

        cgi.WriteCGI "<BODY BGCOLOR=#aabbcc><CENTER>"

        cgi.WriteCGI "<H1>Hello World!</H1>"

        cgi.WriteCGI "<IMG SRC=""" + cgi.ReadScriptName

        cgi.WriteCGI "?type=jpeg"">"

        cgi.WriteCGI "</CENTER></BODY></HTML>"

  End Select

End Sub



Sub PipeMIME(sMime As String, iFile As Integer)

    cgi.WriteCGI "Content-type: " + sMime, False

    cgi.WriteCGI vbCrLf, False

    cgi.WriteCGI vbCrLf, False

    Dim sOut As String

    On Error GoTo FILEEOF

    Do While Not EOF(1)

        sOut = Input(1, iFile)

        cgi.WriteCGI sOut, False

    Loop

FILEEOF:

    Exit Sub

End Sub



Sub Main()

    On Error GoTo NormalErr



    cgi.ProcessCGI Command

    HelloWorld

    End

NormalErr:

    Beep

    oCGI2Register

    End

End Sub



3



