在两个线程之间共享QAxObject?

时间:2013-04-10 11:49:30

标签: c++ qt qthread qaxobject

让我们说一个在线程中运行的对象A.它有一个指向QAxObject实例和一个对象B的指针。对象B有一个指向QAxObject的指针。

对象A创建一个线程并在其中移动对象B.

#ifndef OBJECTA_H
#define OBJECTA_H

#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QAxObject>

#include "ObjectB.h"

class ObjectA : public QObject
{
    Q_OBJECT
public:
    ObjectA(QObject *parent = 0) : QObject(parent)
    {
        thread = new QThread();
        activeX = new QAxObject();
        objectB = new ObjectB(activeX);
        objectB->moveToThread(thread);

        ObjectA::connect(objectB, SIGNAL(someSignal()), this, SLOT(someSlot()));

        thread->start();
    }

protected slots:
    void someSlot();

private:
    QThread *thread;
    QAxObject *activeX;
    ObjectB *objectB;
};

#endif // OBJECTA_H


#ifndef OBJECTB_H
#define OBJECTB_H

#include <QtCore/QObject>
#include <QAxObject>

class ObjectB : public QObject
{
    Q_OBJECT
public:
    ObjectB(QAxObject *axObject, QObject *parent = 0) : QObject(parent)
    {
        activeX = axObject;
    }

signals:
    void someSignal();

private:
    QAxObject *activeX;
};

#endif // OBJECTB_H

对象B是否能够使用与对象A共享的QAxObject?

我觉得这是不可能的。目前,我收到此错误:QAxBase: Error calling IDispatch member NewProject: Unknown error

线程A和线程B都不能使用QAxObject。

关于此的任何信息?

1 个答案:

答案 0 :(得分:0)

以下是解释:Access a COM object from a 3rd party dll across threads

应该使用:

CoInitialize(0);
CoInitializeEx(NULL, COINIT_MULTITHREADED);