Visual Basic.NET Example
From CometWiki
Cometlib -- The Open Interface to the Comet File System.
VB.NET 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.
Open Visual Studio .net Click on New Project Make sure the project type is Visual Basic Projects, make the name “hello” and put it in any folder you wish. Click on the Windows Application template and click OK. You should get a small dialog grid on your screen. Click on the menu item view, and click toolbox to bring up the control builder. In the toolbox, click on Button. On the dialog grid, drag your mouse to make a button in the center. By default it should be called Button1 when you let go of the mouse. Double click on the button you created. You should see a window with code in it. In particular, you will see a line that starts like this: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Under the project menu, click Add Reference select the COM tab and double click “CometLib 1.0 Type Library”, and click OK. 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 excp 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 Not f.CreateKeyedFile(file, fdir, 65, 34) Then
excp = "Create " GoTo Bad End If f.Close(lun)
If Not f.Open(lun, file, fdir) Then
excp = "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 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 excp = "Nextkey" GoTo Bad End If If Not f.ReadNext(lun) Then excp = "Read " GoTo Bad End If record = f.GetRecField(lun, 0, 65) Output = Output & RTrim(key) & " -- " & RTrim(record) & vbNewLine Next i MsgBox(Output)
Done: On Error Resume Next ' No errors allowed here f.Close(lun) f.Terminate() f = Nothing End Bad: last = f.GetCometExcpString(f.LastCometError) MsgBox(excp & "got excp " & f.LastCometError & " -- " & last) GoTo Done 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
That is the whole program. On the Build Menu, select Build solution. This will compile the project. 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 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 Not f.CreateKeyedFile(file, fdir, 65, 34) 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() 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.