Re: Writing my 1st VBS "Script Component"
- From: "Justin Piper" <jpiper@xxxxxxxxx>
- Date: Fri, 20 Jul 2007 14:07:52 -0500
On Fri, 20 Jul 2007 12:10:00 -0500, Jim Rodgers <JimRodgers@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
"Justin Piper" wrote:
Be careful when mixing VBScript classes and script components, since
returning an instance of a VBScript class from a member of a script
component does not increase the reference count on the component.
Thanks Justin,
As a newbie to Script Components (wsc files), I would
eventually have tried to return an object like that. In fact,
I may be even more likely since you brought this to my
attention.
Correct me if I am wrong, but if I continue to follow my
own programming style -- whereby I always use objects
in order (create / open / ...use... / close / set = Nothing)
then there will be no problems. I developed this habit
from using the ADODB objects where pooling would be
affected, especially in VBScript / ASP / ADODB.
I wouldn't advise it. Set = Nothing is not the only reason a script component's reference count might decrease. Returning from a function could cause it, if the component were assigned to a variable local to that function. Stack unwinding following an error could, as well.
It would be much safer, and not significantly more involved, to define a second component in the same package and use CreateComponent to return it.
test.wsc:
<?xml version="1.0"?>
<package>
<component id="a">
<?component error="true" debug="true"?>
<public><method name="F"/></public>
<script language="VBScript">
Function F : Set F = CreateComponent("b") : End Function
</script>
</component>
<component id="b">
<?component error="true" debug="true"?>
<public><method name="Test"/></public>
<script language="VBScript">
Function Test : Test = "Test" : End Function
</script>
</component>
</package>
test.vbs:
Option Explicit
Function ScriptDir()
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f: Set f = fso.GetFile(WScript.ScriptFullName)
Set ScriptDir = f.ParentFolder
End Function
Dim a: Set a = GetObject("script:" & ScriptDir.Path & "\test.wsc#a")
Dim b: Set b = a.F
WScript.Echo b.Test
Set a = Nothing
WScript.Echo b.Test
I guess this means I've been using objects (even in VB
and VBA) as if they are late-bound? Is that what you are
implying?? So, if I were to create objects in an early-
bound code environment, then I could do this:
Late binding refers to when member names are resolved, not to when the objects are created and destroyed. It means that the members are resolved as the program runs, rather than before.
Dim <myObj1> As <objType1>
Dim <myObj2> As <objType2>
Set <myObj2> = New <objType2>
Set <myObj1> = <objType2>.<rtnsObjType1>
Set <myObj2> = Nothing
'
debug.Print <myObj1>.<rtnsString> ' Obj1 is still valid???
...Pardon the pseudocode.
It never occurred to me that would be legal. Is this
what you are saying by implication?
This usage is legal, it just doesn't work in the case of VBScript classes.
ANOTHER question for you Justin, you wrote in your
example...
Dim c: Set c = GetObject("script:" & ScriptDir.Path & "\comp.wsc")
Again, I did not you could do that. I'm still reading up on
Script Components. I "think" I saw four other methods
in the "loiterature."
Dim c: Set c = GetObject("MyObjectID")
Dim c: Set c = GetObject("MyObjectID.WSC")
Dim c: Set c = GetObject("Scriptlet.MyObjectID")
Dim c: Set c = GetObject("Component.MyObjectID")
Obviously, all these methods would require having
registered the wsc file [and the typelib with it]. To the
best of your knowledge, would any of these four other
methods work or not work SPECIFICALLY in
ASP Classic on IIS 5/6?
Offhand I know of three ways to instanciate a script component.
1) Using CreateObject() to create an instance using the progid or classid of a registered component, which I gather you are already somewhat familiar with.
2) Using GetObject and the "script:" URI scheme to create an instance using the name of the WSC file. There's a good overview of this technique on Eric Phelps's website.
http://www.ericphelps.com/scripting/samples/wsc/_readme.txt
3) Using CreateComponent to create an instance of another component in the same package using its component ID. This is described in Microsoft's script component documentation.
Also, would the way you demonstrated also work even
if the component were NOT registered?
Yes, it is specificly intended to allow you to create script components that are not registered.
How do I package up these things and be assured that
IntelliSense™ will ALWAYS work [in MS Editors]?
I'm not positive, since I rarely an editor that has IntelliSense support, but you shouldn't have to do anything aside from explicitly including a <parameter> element for each argument your methods take.
--
Justin Piper
Bizco Technologies
http://www.bizco.com/
.
- Follow-Ups:
- Re: Writing my 1st VBS "Script Component"
- From: Jim Rodgers
- Re: Writing my 1st VBS "Script Component"
- References:
- Re: Writing my 1st VBS "Script Component"
- From: mr_unreliable
- Re: Writing my 1st VBS "Script Component"
- From: Justin Piper
- Re: Writing my 1st VBS "Script Component"
- From: Jim Rodgers
- Re: Writing my 1st VBS "Script Component"
- Prev by Date: Re: update today's date in a folder name, daily, via a script?
- Next by Date: Re: interesting...
- Previous by thread: Re: Writing my 1st VBS "Script Component"
- Next by thread: Re: Writing my 1st VBS "Script Component"
- Index(es):
Relevant Pages
|