原文链接:How to use VBScript in C++(Using Qt)
本文主要解释C++和VBScript脚本混编过程。存在多种使用简单代码以完成程序功能的情况。一个简单的例子的是在Windows下使用DDE通讯协议。想使用简单C++代码以完成该协议几乎让人崩溃。但是使用VBScript脚本是一件相当简单的事情。这里并不会讨论如何完成DDE通讯协议。使用Google搜索“如何使用VBScript链接DDE”并且结合本篇文章的学习。过程如下:
1.在qmake项目文件中添加axcontainer模块
QT+=axcontainer
2.包含ActiveQt头文
#include <ActiveQt>
3.在MainWindow中声明两个指针变量
QAxScriptManager *scrpt_mgr;
QAxScript *main_scrpt;
4.使用QAxScriptManager初始化QAxScript指针变量。本例中,VBScript保存在Qt资源文件中,但是也可以将脚本文件放在磁盘任何地方
main_scrpt = scrpt_mgr->load(“:/VBScript.vbs”, “MyScript”);
5.使用QAxScript类调用VB脚本中函数
main_scrpt->call(“SaySomething(QString)”, “Hello!”);
源码下载链接: Download
===================================================================================================================================
XXNet项目在Windows下使用VBScript脚本完成运行工作,遇到没有生成桌面快捷方式的情况,start.vbs脚本如下:
Function CurrentPath()
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
CurrentPath = objFSO.GetParentFolderName(objFile)
End Function
Function CurrentVersion()
strCurrentPath = CurrentPath()
strVersionFile = strCurrentPath & "/code/version.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(strVersionFile)) Then
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(strVersionFile,1)
CurrentVersion = objFileToRead.ReadLine()
version_path = strCurrentPath & "/code/" & CurrentVersion & "/launcher/start.py"
If( Not fso.FileExists(version_path) ) Then
CurrentVersion = "default"
End If
objFileToRead.Close
Set objFileToRead = Nothing
Else
CurrentVersion = "default"
End If
End Function
Function isConsole()
Set objArgs = Wscript.Arguments
'WScript.Echo objArgs.Count
'WScript.Echo objArgs(0)
isConsole = 0
If objArgs.Count > 0 Then
if objArgs(0) = "console" Then
isConsole = 1
End If
End If
End Function
strCurrentPath = CurrentPath()
strVersion = CurrentVersion()
Dim strArgs
quo = """"
If isConsole() Then
python_cmd = "python.exe"
Else
python_cmd = "pythonw.exe"
End If
strExecutable = quo & strCurrentPath & "\code\" & strVersion & "\python27\1.0\" & python_cmd & quo
strArgs = strExecutable & " " & quo & strCurrentPath & "\code\" & strVersion & "\launcher\start.py" & quo
'WScript.Echo strArgs
Set oShell = CreateObject ("Wscript.Shell")
oShell.Run strArgs, isConsole(), false
分析:
1.CurrentPath函数是返回当前目录路径
2.CurrentVersion是位于code目录下中version.txt中的版本信息
3.isConsole没有弄明白是啥,好像是使用控制台还是什么的运行python脚本
4.oShell通过获取的一系列参数运行xxnet
总之,弄了一下午并没有实质性的成果,并不可以在win平台下的cmd中直接敲指令就运行,但是发现想要运行VBScript,QAxScriptManager、QAxScript需要和QWidget一起使用。
继续折腾呢?