【问题标题】:Boost Message queue Not receiving across two processes提升消息队列未跨两个进程接收
【发布时间】:2020-08-29 16:43:47
【问题描述】:

我编写了一个小型测试应用程序来使用 Boos Message_Queue 在两个进程之间发送数据。一切正常,我能够打印我发送的数据。

我将测试代码移到我的主项目中,现在主项目没有从 Receives 中唤醒。主项目作为系统运行,所以我追踪了一个错误,发现 message_queue 出于某种原因没有检查相同的内存位置。我定义了 BOOST_INTERPROCESS_SHARED_DIR_PATH,然后主项目能够打开测试应用程序创建的队列。但是当测试应用程序发送时,主项目从未从它的接收中醒来。主项目应该作为系统运行,测试应用程序作为用户运行。但我想既然它正在共享内存位置,它应该可以正常工作吗?

如果我再次在测试应用中打开队列,它会立即唤醒并接收所有消息。我是否遗漏了什么或者这是对 BOOST message_queue 的限制?

测试应用的代码:

MessageQueue::MessageQueue(int, boost::interprocess::permissions perm) :
    mq(boost::interprocess::create_only, "TestChannel", 100, sizeof(QueueData), perm)
{
}

MessageQueue::MessageQueue(bool) :
    mq(boost::interprocess::open_only, "TestChannel")
{
}


MessageQueue::~MessageQueue()
{
    int num = mq.get_num_msg();
    wprintf(_T("sent: %d\n"), num);
     boost::interprocess::message_queue::remove("TestChannel");
}

void MessageQueue::SetCommand(int i)
{
    QueueData qd;
    qd.fakeInfo = i; 
    qd.exit = false;
    CoCreateGuid(&qd.msgGuid);
    mq.send(&qd, sizeof(qd), 0);
    OLECHAR* uidOleStr;
    if (StringFromCLSID(qd.msgGuid, &uidOleStr) != S_OK)
        throw std::runtime_error("Unknown error occurred when trying to convert a GUID to string!");
    // Copy the ole str into a CString and then free it immediately, so we don't have to worry about it.
    CString guidString(uidOleStr);
    CoTaskMemFree(uidOleStr);
    wprintf(_T("sent: %d, %s\n"), qd.fakeInfo, guidString);
}

void MessageQueue::WaitForCommand()
{
    while(true)
    {
        QueueData qd;
        size_t size, pri;
        mq.receive(&qd, sizeof(qd), size, pri);        
        if (qd.fakeInfo == 2)
            sendExit();

        OLECHAR* uidOleStr;
        if (StringFromCLSID(qd.msgGuid, &uidOleStr) != S_OK)
            throw std::runtime_error("Unknown error occurred when trying to convert a GUID to string!");
        // Copy the ole str into a CString and then free it immediately, so we don't have to worry about it.
        CString guidString(uidOleStr);
        CoTaskMemFree(uidOleStr);
        wprintf(_T("Recieved: %d, %s\n"), qd.fakeInfo, guidString);
        if (qd.exit)
            break;
    }
}

void MessageQueue::sendExit()
{
    QueueData qd;
    qd.exit = true;
    mq.send(&qd, sizeof(qd), 0);
    wprintf(_T("Sent Exit"));
}

.h 文件:

#pragma once
#define BOOST_INTERPROCESS_SHARED_DIR_PATH "C:\\Program Files (x86)\\Users"
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/permissions.hpp>


class QueueData
{
public:
    int fakeInfo;
    GUID msgGuid;
    bool exit;
};


class MessageQueue
{
public:
    MessageQueue(int, boost::interprocess::permissions perm);
    MessageQueue(bool);
    ~MessageQueue();

    boost::interprocess::message_queue mq;

    void SetCommand(int);
    void WaitForCommand();
    void sendExit();
};

测试应用运行代码:(我一直在使用断点)

void waiter()
{
    MessageQueue mq(true);

    mq.WaitForCommand();
}

void sender()
{
    boost::interprocess::permissions perm;
    perm.set_unrestricted();
    try
    {
        boost::interprocess::message_queue::remove("TestChannel");
        MessageQueue mq(2, perm);

        mq.SetCommand(1);
        mq.SetCommand(1);
        mq.SetCommand(2);
    }
    catch (boost::interprocess::interprocess_exception e)
    {
    }
}

int main() {

    waiter();

    sender();

}

来自主项目的代码:(为了测试我确实使用了上面代码的等待,但仍然没有)

void MemoryChannel::WaitForCmd( const std::function< void ( MemoryChannelCmd cmd, const char *pData, TCHAR *tempPath, GUID msgGuid ) > func )
{
    QueueData mcObject;
    size_t size, pri;
    while (true)
    {
        pMCD->dataQueue.timed_receive(&mcObject, sizeof(mcObject), size, pri, boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(30000));
        size_t num = pMCD->dataQueue.get_num_msg();

        //func(MemoryChannelCmd::MEMORY_CHANNEL_RUN_SQL_SELECT, "", _T(""), mcObject.msgGuid);
    }
}

似乎不是代码问题,因为它在测试应用中有效,但在主项目中甚至共享代码都无效。

我很茫然。

【问题讨论】:

标签: c++ windows boost boost-interprocess


【解决方案1】:

对于进程间通信,必须首先启动更高权限的进程。只有这样低权限的进程才能连接。

在您的示例系统进程中,应该启动队列,测试应用程序连接,然后它们可以通信。这就是重新启动测试应用程序时它起作用的原因。

这样设计是为了防止低权限用户未经许可访问高权限用户内存。

【讨论】:

  • 我已切换到拥有该频道的主项目,但现在测试应用程序正在阻止发送。发送永远不会完成。
  • 尝试以管理员身份运行测试应用。
  • 没有变化。我确实注意到了这一点。互斥锁不匹配,主项目说它为空。 imgur.com/a/VVzTJ3y
  • 进一步更新:如果我定义 BOOST_INTERPROCESS_SHARED_DIR_PATH 我看不到正在创建互斥锁。如果我没有定义它,我会看到它正在创建,但文件位于不同的位置
猜你喜欢
  • 2011-11-04
  • 1970-01-01
  • 2020-11-27
  • 2013-06-07
  • 2013-10-17
  • 2018-05-12
  • 2021-04-07
  • 2012-01-19
  • 1970-01-01
相关资源
最近更新 更多