Click here to Skip to main content
13,148,573 members (66,036 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have an environment where there are two different COM objects which implement IActiveScript interface and they get IDispatch as reference to the site where the scripts are placed.

Now, I want to refer to a function of one script from inside another Script(both Scripts are inside the Scripting Engine but loaded in different sites), is it possible ? How will it work and How to implement it?

Posted 8-Jul-12 19:30pm
Updated 9-Jul-12 4:20am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you want to create VBScript COM Object, you have read about Active Script Components. You will have to create an XML document with special format and extension and register it with regsvr32.
Techiz 9-Jul-12 10:22am
Thanks for the Reply, I actually have studied that and implemented it. I have updated the question Kindly help me in that. Thanks
armagedescu 9-Jul-12 11:19am
Suppose, you have ApplicationX which is using (hosting) two scripts, VBScript1 and VBScript2.
So far everything is good. ApplicationX has the script dispatch do script1 and script dispatch to script2, let's call them Script1IDispatch and Script2IDispatch.
VBScript1 can not call functions from VBScript2. But VBScript1 can call functions from ApplicationX and VBScript2 also can call functions from ApplicationX. And viceversa, ApplicationX can call functions from VBScript1 and from VBScript2.
Any VBScript object is just an IDispatch. So, you need to initialize a script object inside the VBScript2. Supposing you have the variable SomeVBScript2Variable, which is meant to refer VBScript1. All you have to remember, is that any objects are just IDispatch. In this case, you initialize a VBScript2 variable to Script1IDispatch.
In pseudocode it would look like this:
Script2IDispatch->SomeVBScript2Variable = Script1IDispatch.
In real code this means to call GetIDsOfNames for "SomeVBScript2Variable" and to call Invoke with a pointer to Script1IDispatch.
After that you can call from VBScript2 SomeVBScript2Variable.SomeScript2Method...
armagedescu 9-Jul-12 11:23am
You also can do it the way round. From VBScript2 call some ApplicationX function meant to get the Script1IDispatch. All you have to know, is that an IDispatch from VBScript point of view is an object, with functions, methods, properties, nothing more.
Techiz 10-Jul-12 1:25am
For the Approach, you have discussed is kind of implementing Automation Wrappers over Scripting Sites. Is there any way that we can put Global Functions in VB-Scripting Engine. So that It can be accessible from any Scripting Site without introducing any Automation wrappers over scripting sites.
armagedescu 10-Jul-12 2:00am
As I already told in the first reply, you have to write script components. They are not wrappers, they are components. Read about script components. This is the only way to extend the scripts. For instance VBScript regular expression is plain ActiveX, and you may use it outside the script anywhere you like.
They are XML files, with .wsc extensions. Inside that files you put your code. After that register extension with regsvr32 and it will be accessible from any application, script, C++, VBA, JScript, C#. You instantiate them as COM objects, with CreateObject inside VBS, CoCreateInstance in C++, new ActiveXObject in jscript and so on. You can write ActiveX components components in any language you like.
Techiz 10-Jul-12 2:15am
Ok I will read about them in detail and will get back to you. Thanks
Techiz 11-Jul-12 4:47am
I have read about script components, and it is all related to SSIS and other data related task, I dont see the how can I use this in my scenario. Can you kindly suggest me any article to get more details.

I also want to know that if, if they are different exe's and they submit there own script, they have there own IDispatch, so If I refer one script function in another script. How will the Engine resolve this expression to get to other script function and what work is required to achieve this.
armagedescu 11-Jul-12 17:06pm
Here is a very good and detailed help file from Microsoft
Download it and use. If you can not read it, then you should right click the file, open the properties and press unblock.
In the contents Tab you will see script components. There is everything you need.
Techiz 30-Jul-12 5:45am
Thank you for guiding me earlier and giving me references. Now I am very much familiar with WSC and have implemented and used. I have a few questions regarding WSC. Firstly I would like to ask what kind of threading model Microsoft have give for script components, are they thread safe? As wsc gets registered with the windows (or scripting runtime) hence they are accessible throughout the windows environment, I want to apply security and access rights to this component. Kindly guide me though this and help me. Thanks
armagedescu 30-Jul-12 6:38am
Threading model is Apartment. For more info you may check the registration information with OleView, coming with VisualC++, or look in the registry under key
armagedescu 30-Jul-12 6:52am
In fact as you may see, in the InProcServer32 is registered a dll, which acts instead of your script component. This dll uses your script, registered in ScripletURL.
Techiz 1-Aug-12 0:48am
how can I apply privacy or a restriction setting so that the component is not available through out windows environment?
armagedescu 1-Aug-12 7:30am
Strictly responce is no. This is because in fact VB function CreateObject calls COM API functions CLSIDFromProgID and CoCreateInstance. CoCreateInstance inside calls CoGetClassObject and IClassFactory::CreateInstance. None of these functions provides information about the caller.
But you may try to investigate if there is any COM registration information which can limit the usage of COM object. In fact there is a strictly limiting possibility if you use Reg Free COM (also named "side by side"). Anyway, if you limit the usage of component, the active script itself is not restricted, and will expose any functionality to external programs.
Techiz 7-Aug-12 3:08am
Suppose there are two WSC's, Script1.wsc and Script2.wsc. In Script1.wsc there is a method Method1 written in VBS. Now if I want to refer or use that function in Script2.wsc, do I need to include some reference of Scritp1.wsc in the Script2.wsc file or I need to CreateObject and then call it or there is a
better way?
There is only Microsoft WSC Wizard, is there any api available to generate .esc files myself with some method calls.

armagedescu 7-Aug-12 15:16pm
So, yes, you have to create an instance of exposed object. You can not expose a function from an a class of objects. It must be instanciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170924.2 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100