1

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
Tom
  • 221
  • 1
  • 4
  • 16

3 Answers3

2

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.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Ekkehard.Horner, i don't know a thing about XML and i don't have experience with wsc files and how to register them correctly on a 64 bit laptop. could you please help me? I just want to make a small "include(filepath)" function for vbscript so that i can include other scripts. Thanks in advance! I will edit my question with the script i want to put in the component. – Tom Nov 15 '14 at 22:48
1

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.

Community
  • 1
  • 1
  • Thanks for the quick reply :) but i'm trying to expose code to the script itself, not to the ScriptControl. i want to write a script inside the scriptcontrol that executeglobal's some code from a file to the main script.. its hard to explain what i mean, sorry for that^^ – Tom Nov 15 '14 at 21:58
  • main vbscript: new ScriptControl object -> code that reads file and executes the content in that file to the main vbscript. – Tom Nov 15 '14 at 22:02
  • answer to: @noodlesstillalive – Tom Nov 15 '14 at 22:08
  • You add a VB6 class to the script control's list of host objects it can access. `ScriptControl.AddObject(WhatTheScriptControlCallsIt, VB6_ClassOrOtherObject, True)` –  Nov 15 '14 at 22:23
  • noodlesstillalive, ok, i'm going to try this, but i'm not sure how to do this exactly. i reply when i'm done :) – Tom Nov 15 '14 at 22:53
  • noodlesstillalive, it works but i don't see how this can be used inside a vbscript to include an other script? the reason i wanted to put the include sub inside a dll is that it is smaller and i can load it each time i need it. (see include sub in edit of main question) – Tom Nov 15 '14 at 23:14
  • i'm going to post a new question because "how to expose variables from msscriptcontrol?" is answered... I asked the wrong question, thanks for the replies everyone :) – Tom Nov 15 '14 at 23:42
0

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.

wqw
  • 11,771
  • 1
  • 33
  • 41