How to move one QAxObject from one thread to another
-
I have a TestConnector class that creates com-object in one thread and moves it to another thread after initialization (asynchronous initializator)):
testconnector.h
@#ifndef TESTCONNECTOR_H
#define TESTCONNECTOR_H#include <ActiveQt/qaxobject.h>
#include <ActiveQt/qaxbase.h>
#include <QThreadPool>
#include <QObject>class TestConnector : public QObject, public QRunnable
{
Q_OBJECTpublic:
TestConnector(QThread *recieveThread);
void run();
void setRecieveThread(QThread *t);signals:
void testConnectionFailed();
void testConnected(QAxObject *ax);protected:
QThread *recieveThread;
};#endif // TESTCONNECTOR_H@
testconnector.cpp
@#include "testconnector.h"
#include <QDebug>
#include <shlobj.h>TestConnector::TestConnector(QThread *recieveThread) :
QObject(0)
{
setRecieveThread(recieveThread);
}void TestConnector::setRecieveThread(QThread *t)
{
this->recieveThread = t;
}void TestConnector::run()
{
qDebug("TestConnector: Initializing COM library");
HRESULT h_result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
switch(h_result)
{
case S_OK:
qDebug("TestConnector: The COM library was initialized successfully on this thread");
break;
case S_FALSE:
qWarning("TestConnector: The COM library is already initialized on this thread");
break;
case RPC_E_CHANGED_MODE:
qWarning() << "TestConnector: A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA)."
<< " This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred";
break;
}qDebug("TestConnector: Connecting to Ax"); QAxObject *ax = new QAxObject("Word.Application", 0); if(ax->isNull()) { qCritical("TestConnector: Ax connection failed"); delete ax; ax = 0; emit testConnectionFailed(); } else { qDebug("TestConnector: Ax connected successfully"); ax->moveToThread(recieveThread); emit testConnected(ax); }
}@
after this manipulations I have QAxObject in recieveThread thread, but com-instance how I understand still in thread of TestConnector class. And the subject of question is how to move QAxObject with it child com-instance into another thread?
-
That will beb tricks
moving COM pointers between threads normally means calling CoMarshalInthredaInterFaceInStream and also an unmarshal. How it works with QAxObject, I don't know. I suggest look at the code of QAxObject and check, whether they do some special things in moveToThread -
I'm watching now the qaxobject.h, and can't find reimplemented moveToThread:
@class QAxObject : public QObject, public QAxBase
{
friend class QAxEventSink;
public:
const QMetaObject metaObject() const;
void qt_metacast(const char*);
int qt_metacall(QMetaObject::Call, int, void );
QObject qObject() const { return (QObject)this; }
const char *className() const;QAxObject(QObject *parent = 0); QAxObject(const QString &c, QObject *parent = 0); QAxObject(IUnknown *iface, QObject *parent = 0); ~QAxObject(); bool doVerb(const QString &verb);
protected:
void connectNotify(const char *signal);private:
const QMetaObject *parentMetaObject() const;
static QMetaObject staticMetaObject;
};@I think it is the theme for bugreport.
-
but it is implemented and that confuse