OLE Automation in JavaScript: Application Instances | WebReference

OLE Automation in JavaScript: Application Instances


OLE Automation in JavaScript

Application Instances

Document templates are the link between an application and the document it supports. Document templates come in two flavors: single document templates, which allow one document of that type to be open at a time, and multiple document templates, which allow many documents of that type to be open. The following table lists the various types of Windows applications:

Single Document Interface (SDI) Applications Multiple Document Interface (MDI) Applications
Old-style Applications New-style Applications One Instance Multiple Instances
Old-style SDI applications can manage only a single document object at a time, but can support closing one document and then opening another document without starting another instance of the application. Such applications should always support running multiple instances of the application at the same time. From an object lifetime implementation point of view, an old-style SDI application is identical to an MDI application. In this discussion all comments referring to an MDI application also apply to an old-style SDI application unless mentioned otherwise. New-style SDI applications can manage only a single document object at a time, but do not support closing one document and then opening another document without starting another instance of the application. That is, if the active document is closed, the entire application shuts down. When a new document is opened, a new instance of the application is launched. If the user selects the "New" or "Open" options from the applications "File" menu, a new instance of the application is created to handle the additional file. Such applications always support running multiple instances of the application at the same time. An MDI application uses multiple document templates. It can manage many document objects in a single instance of the application. This type of MDI application does not support running multiple instances of the application at the same time. Only one instance of the application can exist at a given time. An MDI application uses multiple document templates. It can manage many document objects in a single instance of the application. This type of MDI application supports running multiple instances of the application at the same time. Microsoft Word is an MDI application that supports multiple instances of the application.

It isn't difficult to associate a given application with one of the preceding application types. Simply launch the application several times, and notice if a new instance is created each time. For example, we launched Microsoft Word three times, Microsoft Excel four times, Allaire HomeSite one time, and Adobe Photoshop one time. Take a look at the resulting task bar:

Multiple Instances

As you can see, Word and Excel both support multiple instances, while HomeSite and Photoshop don't. The taskbar only reflects instances of visible applications. If an application was launched through OLE Automation, it won't appear in the taskbar until it becomes visible to the user. Therefore, you should use the Ctrl+Alt+Del combination to load the Close Program dialog (the one that lets you shut down crashing applications).

Now that we're familiar with instances, let's take another look at the ActiveXObject() and GetObject() functions:

var objVar = new ActiveXObject(class);
  • If the object is registered as a single-instance object (i.e., an out-of-process ActiveX EXE), only one instance of the object can be created. Regardless of the number of times ActiveXObject() is invoked, you will obtain a reference to the same instance of the object.
  • If the object is registered as a multiple-instance object (e.g., Microsoft Word), a new instance of the object is created. The number of times ActiveXObject() is invoked reflects the total number of instances. For example, if class is Word.Application (Microsoft Word), a new instance of the application is created.

var objVar = new ActiveXObject(class, servername);
  • This statement is the same as the preceding one, but it registers the ActiveX object on a different server.

var objVar = GetObject(, class);
  • In JScript, this statement generates an error, because JScript doesn't accept an omitted argument followed by a delimiter (,). However, the purpose of this statement (as implemented in VB) is achieved in JScript by specifying an empty (zero-length) string rather than omitting the argument.
  • In VB (and VBScript), this statement returns the current instance of the object. An error is generated only if the object cannot be found.

var objVar = GetObject("", class);
  • In JScript, this statement returns the current instance of the object. An error is generated only if the object cannot be found.
  • In VB (and VBScript), this statement returns a new instance of the object, unless the object is registered as single instance, in which case the current instance is returned. In fact, this version of the GetObject() function is equivalent to the ActiveXObject() constructor.

var objVar = GetObject(pathname);
  • If the document object is not already running, a new instance of the object's server application is launched, and the application is told to open the corresponding file. Note that the argument must represent an existing file.

var objVar = GetObject(pathname, class);
  • A new instance of the application is always launched, even if the document is already open in a running instance of the application.

We've covered every possible syntax of ActiveXObject() and GetObject(). Let's sum it all up with the following table:

TaskFunction
Create a new instance of an OLE server (that is not running)ActiveXObject()
Create a new instance of an OLE server (that is already running)ActiveXObject()
Create an instance of a class created with Visual BasicActiveXObject()
Create an instance of a class on a remote machineActiveXObject()
Launch an OLE server and load an instance of a subobject (e.g., a document)GetObject()
Retrieve an existing instance of an OLE serverGetObject()

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: January 10, 2000
Revised: January 10, 2000

URL: http://www.webreference.com/js/column55/instances.html