首页 > 代码库 > Script Control 组件Win7 X64平台运行问题及示例

Script Control 组件Win7 X64平台运行问题及示例

本文要点:

  1. MSSCRIPT.OCX组件只有x86版本,所以要在x64系统下运行,请使用云盘中的x86版本WScript安装程序。
  2. 将以下示例代码存为js文件,将其拖动到x86版本的WScript.exe或CScript.exe程序上即可以运行。
  3. 示例 Microsoft Script Control 1.0 的Error和Timeout两个事件的使用。
  4. 示例 Microsoft Script Control 1.0 的Run方法,Modules模块集合的使用等等。
  5. 另外 Microsoft Script Control 1.0 的中英文参考文档已经转为CHM格式,参考云盘的 MSSCRIPT.chm 等文件。

发现盗贴现象好严重,估计这文章一发表就注定被转抄的命运了,打个标记:By @Jimbowhy @源地址:

http://blog.csdn.net/winsenjiansbomber/article/details/41376999

同时,笔者绝对有理由相信,在文章的添加 mywife.cc 这样的字眼会大大增加本文的浏览量!哈哈:) 也算是对脚本应用的一个普及推广啦!


/* MSSCRIPT.OCX Microsoft Script Control 1.0 is ready for x86 system
 * it may site in c:\Windows\SysWOW64\msscript.ocx
 * and must run x86 version WScript.exe to execute this script
 */
//var sc = new ActiveXObject("MSScriptControl.ScriptControl");
var sc = WScript.CreateObject("MSScriptControl.ScriptControl","ScriptControl_");
sc.Language = "VBScript";
sc.AllowUI = true;
sc.Timeout = 3000;

/* This may occurs 80004005 before Language setted:
 * The operation could not be completed 
 * because the script engine has not been initialized to a valid language. */
sc.AddObject("WScript", WScript);

//sc.addCode('On Error Resume Next'); // add code to Global Module!
//sc.AddCode('x=1/0'); // Execute right now and causes Error event

// Delay sub about 5s in my 1.6Ghz 4 Cores CPU, so it will cause 3s Timeout event!
var vbs = 'Sub Main(msg) : For i = 1 to 5000000 Step 1 : x=x+i : Next : MsgBox msg&vbCrLf&"Delay Done:"&i : End Sub';
//sc.Eval(vbs); // Error, just expression to Eval
sc.AddCode(vbs);
sc.Run("Main","Hi, Main");


//var vbs = 'Function Main(msg) : MsgBox(msg) : End Function'; // Why not ByVal msg As String?
var vbs = 'Sub Main(msg) : MsgBox("Modules:"+msg) : End Sub'; // just Public not Private member
var m = sc.Modules.Add("Module1");
m.AddCode(vbs);
m.Run("Main","Hi, Main");



sc.Language = "JScript";
sc.AddObject("WScript", WScript); // reset when Language changed
sc.AddCode('WScript.Echo("Hi,JS");');

/* Events for Script Control
 * Timeout: Occurs when the time specified in the Timeout property has elapsed, 
 * and a user has selected End from the resulting dialog box. */
function ScriptControl_Timeout(){
    WScript.Echo("Event: ScriptControl Timeout "+sc.Timeout);
}

/* Error: An Error event can occur during event sinking or during a direct call 
 * to the scripting engine when calling into an object returned by the CodeObject 
 * property. The error argument is the number of the run-time error that occurred. */
function ScriptControl_Error(){
    var msg = "Event: ScriptControl Error " +
        "\nLine:" + sc.Error.Line +
        "\nColumn:" + sc.Error.Column +
        "\nNumber:" + sc.Error.Number +
        "\nDescription:" + sc.Error.Description;
    sc.Error.Clear();
    WScript.Echo(msg);
}



关于Windows Scripting Host这个很实用的古董编程技术,Jimbowhy 收集了大量收藏级的资料,请到云盤免费下载:


http://pan.baidu.com/s/1i3Br5OD


包括:

Advanced VBSript for Microsoft Windows Administrators.pdf
Advanced Windows Script Host Developers Guide (2003) .pdf
IVIEWERS.DLL
Microsoft VBScript Step by Step.pdf
Microsoft Windows脚本技术.CHM
OLEVIEW.EXE
scd56chs.exe JScript & VBScript 官方参考手册 5.6中文版
script56.chm JScript & VBScript 官方参考手册 5.6 e文版
scripten.5.7.0.16535.win2k.exe  JScript & VBScript 安装程序
scripten.5.7.0.16535.win2k3 .exe
scripthost20dev.chm  Microsoft Windows Script Host 2.0 Developer’s Guide / Günter Born.<
script_repository10.chm System Administration Scripting Guide Script RepositoryVersion 1.0, May 2002
VBScript Programmer‘s Reference, 3rd Edition.pdf
VBScript程序员参考手册.pdf
VBS_常用脚本.docx
sct10en.exe    Microsoft Script Control 1.0
MSSCRIPT.chm  Microsoft Script Control 1.0 英文手册的CHM格式
MSSCRIPT-CHS.chm  Microsoft Script Control 1.0 中文手册的CHM格式





Script Control 组件Win7 X64平台运行问题及示例