1
votes

I'm trying this for a while now.. is it possible to expose variables and other things to the main script with msscriptcontrol?

example of this control:

set a = createobject("msscriptcontrol.scriptcontrol") 
a.language = "vbscript" 
a.executestatement "ab = 12" 
msgbox a.eval("ab") 

what i like to do is to make an activeX com dll in vb6 for including other scripts in my vbscript. the old way i did that was: read a file with an fso object and executeglobal the content of the script file. now i want to wrap that into an activeX dll. here some pseudo-vbscript-code to show you what i'm trying to accomplish when the dll is finished:

set include = createObject("scripting.includeFile") 
include.file "c:\test.vbs" 
call sub_in_test_vbs() 

anny ideas? I was trying to do this with an include function inside a vb6 class with msscriptcontrol but it can't do "executeGlobal" and expose the script to the main vbscript...

[EDIT for: Ekkehard.Horner]

Sub Include(File)
ExecuteGlobal(CreateObject("SCRIPTING.FILESYSTEMOBJECT").OPENTEXTFILE("FILENAME & ".VBS", 1).READALL & vbNewLine)
End Sub
3

3 Answers

2
votes

If you want to write COMponents in any COM(ic) language and use them in any COM(ic) language - even without registration - then use Windows Script Components.

Update:

From your comment

so sometimes i split the large script into smaller vbscript's, put them into a folder and make a main script that reads everything in that folder and executes what's in the scripts. in the main file there is a sub called "include" (see example in my question) so that i can include files like in e.g. c++ ore something. the problem is that every time i do this i have to write that same "include" sub in the main vbscript so i wondered if i can make an activeX dll in vb6 so that i just can do this: createobject("blah.include").include "filepath"...

I assume that your real world problem is code re-use via modules/libraries in VBScript. That can be achieved without the overhead of MS ScriptControl, vb6, and dlls.

(1) Use something like

Dim gsLibDir : gsLibDir = "M:\lib\kurs0705\"
Dim goFS     : Set goFS = CreateObject( "Scripting.FileSystemObject" )
ExecuteGlobal goFS.OpenTextFile( gsLibDir & "BaseLib.vbs" ).ReadAll()

If all your re-usable code is in BaseLib.vbs (and it would be if you didn't distribute the code into many smaller files in that folder just for the privilege to 'read everything' from there), you are done.

(2) If you have a few specialized libs (Database, XML, MS Office automation, Libre Office automation, ...) and want to select from that set according to the task of your main.vbs, either (a) add a few lines like

ExecuteGlobal goFS.OpenTextFile( gsLibDir & "XmlLib.vbs" ).ReadAll()

or (b) put a Sub include(suitableparms) into BaseLib.vbs and call it like

includeLibs Array(                 _
           "§LibDir§ReLib.vbs"     _
         , "§LibDir§TxtManLib.vbs" _
         , "§LibDir§ADOConst.vbs"  _
         , "§LibDir§ADOLib.vbs"    _
         , "§LibDir§WMILib.vbs"    _
         , "§LibDir§DNLib.vbs"     _
         , "§LibDir§XPLLib.vbs"    _
                 )

Of course, such a Sub should provide more functionality than

Sub Include(File)
  ExecuteGlobal(CreateObject("SCRIPTING.FILESYSTEMOBJECT").OPENTEXTFILE("FILENAME & ".VBS", 1).READALL & vbNewLine)
End Sub

which - quote & name errors aside - is equivalent to (a) with the additional overhead of a call. Just as useless/bloated is

sub includeFile (fSpec)
    dim fileSys, file, fileData
    set fileSys = createObject ("Scripting.FileSystemObject")
    set file = fileSys.openTextFile (fSpec)
    fileData = file.readAll ()
    file.close
    executeGlobal fileData
    set file = nothing
    set fileSys = nothing
end sub

(cf. here, further food for thought)

So put some effort in the design of a Sub include() that deals with possible syntax errors in the files included, avoids loading the same module more than once, and provides extra payload (search a list of lib folders, garantee an ordered sequence of unloading, doing initialization/clean up, ...) - or stick with (a).

(3) If you want to mix languages and use the features of COM, forget ExecuteGlobal and use .wsf and .wsc files. If you 'don't know a thing about XML and ... don't have experience with wsc files and how to register them correctly', then you'll have to learn about these strange beasts, preferably by studying the docs.

1
votes

The script control can access your program's stuff, specifically objects.

It's help file should be in system32 folder with Reference, some basic conceptual info, and some small samples.


Makes run-time functionality available to a scripting engine.

Syntax

ScriptControl.AddObject(name, object[, addMembers])

The AddObject method has these parts:

Part Description

name Required. Name by which the added object is to be known in ScriptControl code.

object Required. Name of the object exposed at run time.

addMembers Optional. Boolean value. True if members of object are globally accessible; False if they are not.

Remarks

Use the AddObject method to make run-time functionality available to a scripting engine. The AddObject method enables a ScriptControl user to provide a set of name/object pairs to the scripting code. The scripting engines may expose the name in any way. In both VBScript and JScript, each name appears as a globally accessible name.

0
votes

This works as expected and shows "12"

Option Explicit

Private m_ab            As Variant

Property Let ab(Value As Variant)
    m_ab = Value
End Property

Property Set ab(Value As Variant)
    Set m_ab = Value
End Property

Private Sub Form_Load()
    With CreateObject("MSScriptControl.ScriptControl")
        .Language = "VBScript"
        .AddObject "__global__", Me, True
        .ExecuteStatement "ab = 12"
    End With
    MsgBox m_ab
End Sub

Note that this is actual code from VB6 IDE, not something written in stackoverflow's text area.