How to use VBScript in C++ (Using Qt)

Posted on Posted in Qt Tutorials, Tutorials

In this article I’ll explain the process of combining C++ and VBScript codes together. There are many situations in which you find yourself in desperate need of some simple code that can handle what your program wants to do. An example that comes to my mind is using DDE Protocol in Windows. It can be heartbreaking to try and write a quick DDE handling program using C++. Well on the other hand it is extremely easy using VBScript. But I am not going to talk about DDE I this post. Search Google for “how to connect to DDE using VBScript” and combine that with what you learn from this post. So here it is:

  • 1. Add axcontainer to your used modules by adding the line below to your qmake project (.PRO file)

QT += axcontainer

  • 2. Add ActiveQt to your include files

#include <ActiveQt>

  • 3. Create two variables in MainWindow for a QAxScriptManager and QAxScript

QAxScriptManager *scrpt_mgr;
QAxScript *main_scrpt;

  • 4. Initialize your QAxScript using QAxScriptManager. In this example it is assumed that the VBScript file is embedded as a resource but you can also use any VBScript file saved on disk.

main_scrpt = scrpt_mgr->load(“:/VBScript.vbs”, “MyScript”);

  • 5. Call VBScript functions using the QAxScript class

main_scrpt->call(“SaySomething(QString)”, “Hello!”);

Download the example project below for a simple, working demonstration of embedding and using a VBScript code in a Qt application.

Download QtVBScriptExample project by clicking the link below:
Any questions, put a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *