Пытаюсь инициализировать COM-объект в другом потоке, Для этого использую класс TestConnector
#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_OBJECT
public:
TestConnector(QThread *recieveThread);
void run();
void setRecieveThread(QThread *t);
signals:
void testConnectionFailed();
void testConnected(QAxObject *ax);
protected:
QThread *recieveThread;
};
#endif // TESTCONNECTOR_H
#include "testconnector.h"
#include <QDebug>
TestConnector::TestConnector(QThread *recieveThread) :
QObject(0)
{
setRecieveThread(recieveThread);
}
void TestConnector::setRecieveThread(QThread *t)
{
this->recieveThread = t;
}
void TestConnector::run()
{
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);
}
}
TestConnector *tc = new TestConnector(this->thread());
connect(tc, SIGNAL(testConnected(QAxObject*)), this, SLOT(testConnected(QAxObject*)));
connect(tc, SIGNAL(testConnectionFailed()), this, SLOT(testConnectionFailed()));
QThreadPool::globalInstance()->start(tc);
(Info) TestConnector: Connecting to Ax
(Warning) QAxBase::setControl: requested control Word.Application could not be instantiated
(Error) TestConnector: Ax connection failed
Если не ошибаюсь, в другом потоке нужно инициализировать COM.
Смотри на CoInitializeEx(...)
#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_OBJECT
public:
TestConnector(QThread *recieveThread);
void run();
void setRecieveThread(QThread *t);
signals:
void testConnectionFailed();
void testConnected(QAxObject *ax);
protected:
QThread *recieveThread;
};
#endif // TESTCONNECTOR_H
#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);
}
}
(Warning) QAxBase: Error calling IDispatch member <Название любой вызываемой функции>: Unknown error
(Warning) QAxBase::dynamicCallHelper: Object does not support automation
Вобщем судя по обсуждению на официальном форуме QAxObject при перемещениии в другой поток (moveToThread) не пакует дочерний ком-объект
Если кому-то интересна дальнейшая судьба вопроса:
http://bugreports.qt.nokia.com/browse/QTBUG-18521
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)