PDA

View Full Version : QAxScriptManager not returning a QAxScript* for VBScript



jgirlich
23rd May 2013, 18:18
Hi,

I'm trying to call a VBScript from within Qt and set up a small test application:


#include <QAxScriptManager>
#include <QDebug>

int main(int argc, char *argv[])
{
QAxScriptManager m_manager;
QAxScript *m_helloWorld = m_manager.load("C:\\test.vbs", "HelloWorld");
if (m_helloWorld)
m_helloWorld->call("helloWorld");
else
qDebug() << "didn't load";
}


The VBScript is at the right place on C:\test.vbs and looks like this:


Function helloWorld()
msgbox("Hello World")
End Function


Now when I compile and run this I don't get a QAxScript back from the QAxScriptManager. Stepping through the code I learned that the file is read and a QAxScript object created, but in the load function on it its isValid() function returns false because the internal member 'engine' is not set and therefore later the object is discarded and a NULL pointer returned. But I couldn't find where 'engine' would ever be set.

Can you please help me understand what I am doing wrong?
Or give me some example code?

Cheers
Jan

Edited: typo

ChrisW67
24th May 2013, 01:18
QAxScriptManager::registerEngine() looks promising (a guess, not from experience). I don't know exactly what you would send it, but I would try:


QAxScriptManager::registerEngine("vbs", "VBScript");

where "VBScript" is the ProgId of the VBSCript in process server.

jgirlich
24th May 2013, 10:26
Nope, that didn't do the trick. I changed my code to look like this and scriptEngineLoaded is set to true, but the result is still the same. Also from the Qt code I read I believe the VBScript engine is set automatically internally when a script ending in vbs is loaded.


int main(int argc, char *argv[])
{
bool scriptEngineLoaded = QAxScriptManager::registerEngine("VBScript", "vbs");
QAxScriptManager m_manager;
QAxScript *m_helloWorld = m_manager.load("C:\\test.vbs", "HelloWorld");
if (m_helloWorld)
m_helloWorld->call("helloWorld");
else
qDebug() << "didn't load";
}

Added after 47 minutes:

Ah, I found some very uselful documentation on http://crpppc19.epfl.ch/doc/qt4-doc-html/html/activeqt-container.html and after reading through the testcon example http://crpppc19.epfl.ch/doc/qt4-doc-html/html/activeqt-testcon.html (find the code in your qt installation directory under tools/activeqt/testcon) I got it working.


#include <QAxScriptManager>
#include <QDebug>
#include <QWidget>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

// Some QObject is crucial. If the script manager is not initialized with a QObject* then call() will return "Unknown Error"
QWidget *widget = new QWidget();
QAxScriptManager *m_manager = new QAxScriptManager(widget);

QAxScript *m_helloWorld = m_manager->load("C:\\test.vbs", "HelloWorld");
if (m_helloWorld)
m_helloWorld->call("helloWorld()"); // don't forget () for a function without params.
else
qDebug() << "didn't load";

// cleanup code missing
return a.exec();
}

Edited: removed doubled part of post

jgirlich
7th June 2013, 11:11
To specify: Since the test function in the vbs file used MsgBox a QWidget was needed. If you don't use any GUI elements in your vbs code you can skip the QWidget and it works fine as well.