How To Optimize OLE Server Instance Usage by Using GetObject (113946)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0

This article was previously published under Q113946

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.

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbprogramming KB113946