The ASP Emporium
Free Active Server Applications and Examples by Bill Gearhart
Online since Friday January 7, 2000

 home > ASP & VB Script Tutorials > Using GetObject()

enter a phrase to search:(advanced search)


 h o m e 

 w h a t 's  n e w 

 c o d e  l i b r a r y 

 d o w n l o a d s 

 C# & .NET  h o w  t o ' s 

 VBScript s c r i p t s 

 JScript s c r i p t s 

 f e e d b a c k 

 u s e r   f o r u m s 

 l i n k s 

 s e a r c h 

 t u t o r i a l s 

 s u p p o r t 


click an example below
Nothing new recently

Using the GetObject( ) function

GetObject( ) may very well be the ultimate function of VBScript and JScript, but the Microsoft documentation for the function is so weak that no matter how perceptive you are, you could never guess that it was capable of so much. GetObject( ) is the magic function. It can open almost anything and create an object reference from it. Here's a short list of what you can do with GetObject( ):
  • use a Java Class that has NOT been registered on the server
  • Access and change keys in the IIS Metabase
  • Access and change Windows settings
  • use a Windows Scripting Component that has NOT been registered on the server
  • use a component based on it's CLSID
  • queue a component
  • and on and on...
How can one function be that versatile (and that list above is just a little taste of GetObject( )'s power) and more importantly, how can you specify to GetObject( ) what to do with the object you are referencing?

It's all in the way that the argument of GetObject( ) is specified. Here's a detailed look at the GetObject( ) function:
GetObject([pathname] [, class])
There are two arguments, both of them are optional. Most of the time, you will only use one argument for GetObject( ) and your argument will contain a moniker at it's beginning. Before I lose you on that wierd word moniker, all a moniker is, is a name that represents an object and may include the complete path or address that identifies the location of the object. So a moniker for the GetObject( ) function is usually a one word descriptor representing the type of object being called so that the Scripting Engine can use the proper method to retrieve a working copy of the object that you can use in ASP to perform actions.

These monikers are what allows the GetObject( ) function to be so versatile. Now we need a list of monikers that GetObject( ) supports... This can be a daunting task because the documentation on this stuff is scarse and displaced everywhere and there's nothing stopping you from defining custom monikers that GetObject( ) can use. Here's the ones that I'm familiar with and that GetObject( ) should support on most Windows 2000 and NT machines.

GetObject( ) argument
(including moniker in bold)
what it does
"iis:<metabasepath>" Allows a programmer to view and alter key IIS functionality for any webserver physically connected to this machine.
"java:<classname>" Returns a reference to an unregistered java object found in the %system root%\java\trustlib folder using the Java Virtual Machine.
"script:<absolutepath>" Returns a reference to an unregistered Windows Scripting Component or other supported script type.
"clsid:<clsid>" Returns a reference to an object by it's class ID in the registry.
"WinMgmts:<string>" Allows access to base Windows OS functionality with WMI
"OBJREF:<base64encodedstring>" Returns access to a running object instance
"queue:<clsid/progid>" Used to activate a queued COM+ component via MSMQ.
"new:<clsid/progid>" Allows instancing of any COM component supporting the IClassFactory pointer (including queued components)


Okay that's a pretty good list. There's more but that's all I can think of right now and since you'll probably only use a couple of them, it's really not needed. I just wanted to put up a list because I spent so long trying to find one specific to the GetObject( ) function and came up empty.

So based on all these examples above, a moniker is just a descriptive name that tells GetObject( ) how the object should be referenced without revealing any clues as to how it might internally call and reference the object before an instance is returned to ASP. That way, we can use almost any COM object in ASP and at the same time, not be concerned with the logic that actually gets the object reference. In a nutshell, an ASP programmer will always be able to use the familiar
object.[property][method][collection] [= whatever]
object-oriented interface that their already familiar with, regardless of the type of COM object (based on the Moniker) being called with GetObject( ).

Realistically with ASP, you'll probably only use two of the monikers in the list above with GetObject( ):
"java:<classname>"
"script:<absolutepath>"
The other GetObject( ) monikers are usually used with Windows Scripting Host or in VBA although they can theoretically be used with GetObject( ) in ASP with either VBScript or JScript provided that the proper permissions are enabled, etc...

I'll try to write a little and give an example for each moniker I described above: (Only a few of these will work in ASP. The others require Windows Scripting Host or VBA...)

Java Moniker
Returns a reference to an unregistered java object found in the %system root%\java\trustlib folder using the Java Virtual Machine.

IIS Moniker
Allows a programmer to view and alter key IIS functionality for any webserver physically connected to this machine.

Script Moniker
Returns a reference to an unregistered Windows Scripting Component or other supported script type.

WinMgmts Moniker
Allows access to base Windows OS functionality with WMI

Other Monikers
Links to other monikers like: queue, new, OBJREF and clsid.