O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A VB Client

Next, we’ve created a VB application with a multiple document interface. You can do this by hand and add the forms and modules you need, or use VB’s Application Wizard and strip out the stuff you don’t need. We’ve defined a public variable called BookServer and three public methods in the form, InitCOMServer, CloseCOMServer, and TestCOMServer, and hooked them up so that InitCOM-Server is called when the form loads:

Public BookServer As Object

Private Sub MDIForm_Load()
    InitCOMServer
    frmJournal.Show
End Sub

Private Sub MDIForm_Unload(Cancel As Integer)
    CloseCOMServer
End Sub

Sub InitCOMServer()
    'called when the program starts
    On Error GoTo InitCOMServer_error
    Set BookServer = CreateObject("Doubletalk.BookServer")
    Exit Sub
    
InitCOMServer_error:
    Dim msg As String
    msg = "There was an error trying to initialize the BookServer." + _
            "Please check that it is properly registered and try the Python " + _
            "test functions first.  The program will now abort."
    MsgBox msg
    End
    
End Sub

Sub CloseCOMServer()
    Set BookServer = Nothing
End Sub

Sub TestCOMServer()
    'just to check it is alive
    Dim hopefully_four As Integer
    hopefully_four = BookServer.Double(2)
    MsgBox "2 x 2 = " & hopefully_four & ", so your server is alive"
End Sub


Private Sub mnuToolsTestServer_Click()
    'this helps establish if the COM server is alive
    'using a minimal diagnostic function in the modMain module
    TestCOMServer
End Sub

That the COM server isn’t registered is a common error, so you need to handle this error and close ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required