Cometlib -- The Open Interface to the Comet File System.
VB6.0 Sample.

This document will show you how to build a small VB program that:

  • Establishes a connection with CFAM through CometLib.
  • Erases and creates a keyed file.
  • Writes some records to the file.
  • Gets some information about the file such as first and last key.
  • Reads some records from the file.
  • Closes the file and terminates the connection with CometLib.
Comet MUST be running on the same computer as the Visual Basic program. The ONLY directories that are accessible are those designated as type C directories in the Comet INI file.

Here is a step-by-step guide that will get a sample program running on your own system.

  1. Open Visual Basic 6.0
  2. Click on New Project
  3. Click on Standard EXE and click OK
  4. You should get a small dialog grid on your screen.
  5. Click on the menu item view, and click toolbox to bring up the control builder.
  6. In the toolbox, click on Button.
  7. On the dialog grid, drag your mouse to make a button in the center. By default it should be called Command1 when you let go of the mouse.
  8. Double click on the button you created. You should see a window with code in it. In particular, you should see this:
    Private Sub Command1_Click()

    End Sub

  9. Under the project menu, click References.
  10. Scroll down and check “CometLib 1.0 Type Library”, and click OK.
  11. Use the clipboard to copy the following code between the Private Sub line and the End Sub line.
    Dim f As CometLib.CometFiles

    On Error GoTo CantInit

    Set f = New CometLib.CometFiles

    f.Initialize ("Hello")

    On Error Resume Next

    Dim lun As Integer
    Dim file As String
    Dim fdir As String
    Dim record As String
    Dim key As String
    Dim func As String

    Dim i As Integer
    Dim last As String

    Dim Output As String

    lun = 43
    file = "hello"
    fdir = "tmp"

    ' Create the file

    f.Erase file, fdir

    If f.CreateKeyedFile(file, fdir, 65, 34) = False Then

    func = "Create "
    GoTo Bad
    End If

    f.Close (lun)

    If f.Open(lun, file, fdir) = False Then

    func = "Open "
    GoTo Bad
    End If

    ' Build the file

    For i = 111 To 222

    f.WriteInit (lun)
    record = "Hello World, this is record "
    f.PutRecField lun, record, 0, 30
    f.PutRecField lun, i, 30, 5
    key = "** " & i
    f.Write lun, key
    Next i

         ' Test the file -- get some info from it

    f.Position lun, "** 140"

    key = f.FirstKey(lun)
    Output = Output & "First key = " & key & vbNewLine
    key = f.NextKey(lun)
    Output = Output & "Next key = " & key & vbNewLine
    key = f.PrevKey(lun)
    Output = Output & "Prev key = " & key & vbNewLine
    key = f.LastKey(lun)
    Output = Output & "Last key = " & key & vbNewLine

    For i = 1 To 10

    key = f.NextKey(lun)
    If key = "" Then
    func = "Nextkey"
    GoTo Bad
    End If
    If f.ReadNext(lun) = False Then
    func = "Read "
    GoTo Bad
    End If
    record = f.GetRecField(lun, 0, 65)
    Output = Output & RTrim(key) & " -- " & RTrim(record) & vbNewLine
    Next i

    MsgBox (Output)

    On Error Resume Next ' No errors allowed here
    f.Close (lun)
    f.Terminate
    set f = Nothing
    End

    Bad:

    last = f.GetCometExcpString(f.LastCometError)
    MsgBox (func & "got func " & f.LastCometError & " -- " & last)
    End

    CantInit:

    If MsgBox("Error loading CometLib, make sure Comet is loaded and click Retry. Click Cancel to exit.", vbRetryCancel, "Is Comet Loaded?") = vbRetry Then Resume
    End

  1. That is the whole program.
  2. On the Build Menu, select Build solution. This will compile the project.
  3. Now, let’s go through this program in some finer detail.
The first two lines make a Comet File System object.
Dim f As CometLib.CometFiles
Set f = New CometLib.CometFiles
You only need one of these objects in your program. I just called the object f, but you could call it anything you want. From this point on, anything you want to do with the Comet File System, you proceed with “f.”. These two statements really belong outside of a click event. They really establish the connection to the object and this should happen when the application starts up. I put them inside of a click event for clarity only.

The next line:

f.Initialize("Hello-World")
Tells CometLib to actually make the connection with CFAM and uses the string “Hello-World” to identify this connection. Any short string of characters will do, but don’t use embedded blanks in the name.

Most of the code from here should be easy to decipher. If you use the object browser, you can see all of the methods that are available. We think it is rather complete.

Most methods return a Boolean. This may be used as an indication of a comet error. If the call is not placed in an if statement, the result is thrown away as in the erase call.

f.Erase file, fdir
In this case I chose to ignore errors. The following create call
If f.CreateKeyedFile(file, fdir, 65, 34) = False Then
excp = "Create "
GoTo Bad
End If
Will Goto the label “Bad:” if there were some sort of comet error on the create.

Other methods such as the key methods return strings.

key = f.FirstKey(lun)
Output = Output & "First key = " & key & vbNewLine
key = f.NextKey(lun)
Output = Output & "Next key = " & key & vbNewLine
key = f.PrevKey(lun)
Output = Output & "Prev key = " & key & vbNewLine
key = f.LastKey(lun)
Output = Output & "Last key = " & key & vbNewLine
If the string is empty, It may be assumed that the program has reached the end of file, or beginning of file, or there are no keys in the file depending on which method is called.

The last section to understand is how to properly shutdown the program. This is accomplished at the label “Done:”

Done:

On Error Resume Next       ' No errors allowed here
f.Close(lun)
f.Terminate
set f = Nothing
End
Here your program should close all open files, Terminate the CometLib connection and clear the value of the object. This code should be executed when your application ends.