如何从 msscriptcontrol 公开变量?

问题描述:

我已经尝试了一段时间了..是否可以使用 msscriptcontrol 向主脚本公开变量和其他内容?

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

此控件的示例:

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

我喜欢做的是在 vb6 中制作一个 activeX com dll,以便在我的 vbscript 中包含其他脚本.我这样做的旧方法是:读取带有 fso 对象的文件并执行全局脚本文件的内容.现在我想把它包装成一个activeX dll.这里有一些伪 vbscript 代码来向您展示我在 dll 完成后要完成的工作:

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() 

安妮的想法?我试图在带有 msscriptcontrol 的 vb6 类中使用包含函数来执行此操作,但它不能执行executeGlobal"并将脚本暴露给主 vbscript ...

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

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

如果你想用任何 COM(ic) 语言编写组件并在任何 COM(ic) 语言中使用它们 - 即使没有注册 - 然后使用 Windows 脚本组件.

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.

更新:

来自您的评论

所以有时我将大脚本拆分为较小的 vbscript,放入将它们放入一个文件夹并制作一个主脚本来读取其中的所有内容该文件夹并执行脚本中的内容.在主文件中有是一个名为include"的子(参见我的问题中的示例),以便我可以包含文件,例如c++ 矿石的东西.问题是每次我这样做时,我都必须在主 vbscript 所以我想知道我是否可以在 vb6 中制作一个 activeX dll我只能这样做:createobject("blah.include").include文件路径"...

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

我假设您的现实问题是通过 VBScript 中的模块/库重用代码.这可以在没有 MS ScriptControl、vb6 和 dll 开销的情况下实现.

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) 使用类似的东西

(1) Use something like

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

如果您所有可重用的代码都在 BaseLib.vbs 中(并且如果您没有将代码分发到该文件夹​​中的许多较小的文件中只是为了从那里读取所有内容"的特权),那么您是完成.

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) 如果您有一些专门的库(数据库、XML、MS Office 自动化、Libre Office 自动化等),并且想根据您的 main.vbs 的任务从该集合中进行选择,要么 (a) 添加几行,如

(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()

或 (b) 将 Sub include(suitableparms) 放入 BaseLib.vbs 并像调用它一样

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"    _
                 )

当然,这样的 Sub 应该提供比

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 - 引用 &撇开名称错误 - 等效于 (a) 具有调用的额外开销.就像没用/臃肿一样

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

(参见此处思想)

因此,在 Sub include() 的设计中付出一些努力,该子include() 处理包含的文件中可能存在的语法错误,避免多次加载同一个模块,并提供额外的有效负载(搜索lib 文件夹列表,保证有序的卸载顺序,进行初始化/清理,...) - 或坚持 (a).

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) 如果您想混合语言并使用 COM 的功能,请忘记 ExecuteGlobal 并使用 .wsf 和 .wsc 文件.如果您对 XML 一无所知并且...没有使用 wsc 文件以及如何正确注册它们的经验",那么您将必须了解这些奇怪的野兽,最好通过研究文档.

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