Re: Writing my 1st VBS "Script Component"

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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/
.



Relevant Pages

  • Re: active directory question
    ... Thank you for the time you took to review this script. ... Later you seem to use ADO to find the trustee. ... The only attribute you need retrieve is "member". ...
    (microsoft.public.scripting.vbscript)
  • Re: Script to populate Distribution list
    ... that list several diffrent zip codes for the same location is there a way to ... > ' Check if user already a member of the group. ... This would slow the script ... > methods require the AdsPath of the user. ...
    (microsoft.public.scripting.vbscript)
  • Re: Error using LDAP query
    ... I know you said this line is failing: ... CreateObject) failed to set oADSysInfo to a valid object. ... try running this simple script as a user (non-Domain ... >> member of at least 2 other groups, ...
    (microsoft.public.windows.server.scripting)
  • Re: Login Script group membership
    ... Would it be more managable to write this as a vbs instead of a batch file? ... script, as not all o/s's can run a .vbs file directly as a logon script. ... - you'd need to write a wrapper function to invoke ifmember and return the ... In the general case a user can be a member of any number of ...
    (microsoft.public.windows.server.scripting)
  • Re: I need to change the group membership using a logon script
    ... admins group in order to run the script. ... I believe users need to be members of the local Administrators group to run ... "Domain Admins" is made a member of the local Administrators group on the ... Administrators groups on the computers. ...
    (microsoft.public.scripting.vbscript)