其他应用程序/进程上的XLib XSendEvent

时间:2017-05-29 08:02:34

标签: c++ ipc xlib

我有两个库:IDA和XIDA XIDA处理事件循环,每个应用程序/进程都有一个事件循环。 这些库通过回调函数“连接”。

这适用于单个应用/流程中的事件......

现在我注意到我可以从一个应用程序向另一个应用程序发送一个事件。它需要的是另一个应用程序的窗口句柄。所以我编写了一个DBus消息,我可以收到一个......

这也有效,但看起来XSendEvent()的接收者只能使用函数及其参数,没有成员变量!

在hpp:

        Window mSection;
        Window Section(){
          return 0; // This is working
        }
        Window Section_A(){
          return this->mSection; // This raises a memory access error
        }
        int CheckEvents( IDA_A *vIDA_A, XEvent vEvent);
        static int Section_EventCallback(void* this_ptr, XEvent vEvent){
        IDA_A* nIDA_A = NULL;
        int nRet = 1;
            try{
                nIDA_A = static_cast < IDA_A* > (this_ptr);
            } catch (...) { return 0; }

            cout << "TEST " << nIDA_A->Section() << endl; // This is working, also with any other function

            cout << "TEST " << nIDA_A->mSection << endl; // This raises a memory access error, also with any other member variable

            cout << "TEST " << nIDA_A->Section_A() << endl; // This also raises a memory access error

            nRet =  nIDA_A->CheckEvents(nIDA_A, vEvent);
            nIDA_A = NULL;
            return nRet;
        }

在cpp:

        int IDA_A::CheckEvents(IDA_A *vIDA_A, XEvent vEvent) {
        Window nWindow = vEvent.xany.window;
        cout << "TEST " <<  " -- " << nWindow  << endl; // This is working
        cout << "TEST " <<  " -- " << this->mSection  << endl; // This raises a memory access error
        return 1;
        }

它还需要使用成员变量吗?

问候
Earlybite

------------------------编辑:--------------------- -------

这是XIDA中XSendEvents()到达的部分(从事件循环内部调用之后):

    int XIDA_A::Send_To_Callback(XEvent vEvent){
        for(int i = 0; i < (int) this->mVecEvCallback.size(); i++){
            if( *this->mVecEvCallback[i].pWindow == vEvent.xany.window ){
                if( vEvent.type == ClientMessage ) {
                    cout << "THIS XIDA CLIENT " << this->mIdent << endl;
                }
                s_EventCallback nEvCallback;
                nEvCallback = this->mVecEvCallback[i];
                nEvCallback.Callback (nEvCallback.this_Ptr, vEvent);
                return 1;
            }
        }
        return 0;
    }

到目前为止,它正在运作,也是成员变量。但在IDA中再也不会出现回调了 但这是一样的过程!
例如,与DBus一起工作......
我真的很想知道原因!

1 个答案:

答案 0 :(得分:0)

解决。
我发现了错误......

这个

    cIDAMessage nMsg;
    nMsg.Msg_Type = IDAMsg_EventCallback;
    nMsg.Lng_Send[0] = 0;
    nMsg.Para_Void[0] = (void*) &this->mSection;
    nMsg.Para_ThisPtr = (void*) this;
    nMsg.Para_CallbackPtr = (void*) &this->Section_EventCallback;
    this->XIDA_Callback(this->mXIDA_A_Ptr, &nMsg);

是init的一部分。

我忘了设置

nMsg.Para_ThisPtr = (void*) this;

但我还是不明白......

static int Section_EventCallback(void* this_ptr, XEvent vEvent){
IDA_A* nIDA_A = NULL;
int nRet = 1;
    try{
        nIDA_A = static_cast < IDA_A* > (this_ptr);
    } catch (...) { return 0; }
    nRet =  nIDA_A->CheckEvents(nIDA_A, vEvent);
    nIDA_A = NULL;
    return nRet;
}

这里来了/指针为NULL 为什么nIDA_A不是NULL,或者为什么没有出现错误,因为static_cast不能使用NULL进行转换?
为什么函数工作,但不是成员变量?

你知道我的意思吗?