HOWTO: Optimize OLE Server Instance Usage by Using GetObject

Last reviewed: September 29, 1997
Article ID: Q113946

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

This article gives a full explanation of the syntax of GetObject, adding to the information given in the "Programmer's Guide."

Specifically, this article explains when a new instance of the server application is instantiated, details how to best minimize server instance usage, and gives an example that shows how to best minimize excess instances of a server application.

MORE INFORMATION

The GetObject function has two parameters:

  • The first determines the instance usage.
  • The second optionally denotes the class of the desired object reference.

The basic syntax for GetObject is:

   GetObject([pathname][,class])

The pathname parameter can be the path to an existing file, "", or it can be left out. Specifying the path to an existing file causes GetObject to use an existing instance of the server application, which is either explicitly denoted in the second parameter or obtained from the registry based on the file's extension. The latter two syntax's are used to create new object references, with the added benefit over CreateObject that you can control instance usage for the server.

The following table details the instance usage for the server application and gives the appropriate syntax:

Server already running:

  Syntax: Set X = GetObject(,"MySrvr.Object")
  Result: X is reference to existing object

  Syntax: Set X = GetObject("","MySrvr.Object")
  Result: X is reference to new object

Server not running:

  Syntax: Set X = GetObject(,"MySrvr.Object")
  Result: Error 429

  Syntax: Set X = GetObject("","MySrvr.Object")
  Result: Server is started and X references new object

To establish a reference to an object and minimize the number of server application instances loaded, use GetObject with a blank first argument, and trap the error 429 when no instance is loaded. The following example demonstrates this. The sample application uses Microsoft Excel version 5.0 as its server application.

Steps to Create Example Program

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a command button (Command1) to Form1.

  3. Add the following code to the Command1_Click event procedure:

       Sub Command1_Click ()
          Dim fResult As Integer
          fResult = SmartGetObject("Excel.Application")
       End Sub
    
    

  4. Add the following code:

    Function SmartGetObject (sClass As String) As Integer

          Dim oTmpObject As Object
    

          ' If Server running, oTmpObject refers to that instance.
          ' If Server not running Error 429 is generated.
          On Error Resume Next
          Set oTmpObject = GetObject(, sClass)
          ' oTmpObject is reference to new object.
          If Err = 429 Then
    
             ' Server not running, so create a new instance:
             Set oTmpObject = GetObject("", sClass)
             ' NOTE: for Excel, you can add the next line to view the object
             ' oTmpObject.Visible = True
          ElseIf Err > 0 Then
             MsgBox Error$
             SmartGetObject = False
             Exit Function
          End If
          SmartGetObject = True
       End Function
    
    

  5. Press the F5 key to run the program. Click the Command1 button.

If a copy of Excel is already loaded, that copy is used to create the object, otherwise a new copy is loaded.
Keywords          : IAPOLE vbwin GnrlVb kbprg kbfasttip
Technology        : kbole kbvba
Version           : WINDOWS:3.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.