PHP-访问使用C ++创建的共享内存 [英] PHP - Access shared Memory created with C++

查看:198
本文介绍了PHP-访问使用C ++创建的共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天以来,我一直在努力使以下事情起作用: 我有一个微型C ++程序,可以从串行端口连续读取一些数据. 此数据像这样存储在共享内存中:

Since a few days i'm trying to get the following thing to work: I have a tiny C++ program which reads some data continuously from a serial port. This data is stored in shared memory like this:

HANDLE hMapFile;
hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,
    NULL,
    PAGE_READWRITE,
    0,
    10,
    NULL);

LPCTSTR pBuf;
pBuf = (LPTSTR) MapViewOfFileEx(
    hMapFile,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    10,
    NULL);

while(true)
{
    //... some code ...
    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    //... some code ...
}

现在,我想使用PHP访问此共享内存.所以我尝试执行以下操作:

Now i would like to access this shared memory with PHP. so i tried to do the following:

$shm_id = shmop_open($key, $mode, $security, $size);
$read = shmop_read($shm_id, 0, 10);
//... some code ...

但是我不知道应该设置哪个键,模式,安全性和大小!

But i don't know which key, mode, security and size i should set!

现在,在您写点东西之前: 我使用"MapViewOfFileEx()",因为我想设置一个固定地址,以便PHP可以从固定地址读取. 我也在C ++和PHP中使用"0x00030000"进行了尝试. C ++能够创建FileMapping,但是PHP无法访问并给出错误消息:shmop_open():无法附加或创建共享内存段. 作为$ mode,我为只读权限设置了"a". 由于$ security我将0777设置为all-access ... 由于$ size,我设置了10个字节.

Now, before you gonna write something: I use "MapViewOfFileEx()" because i would like to set a fixed address so PHP can read from a fixed address. I also tried this with "0x00030000" both in C++ and PHP. C++ was able to create the FileMapping, but PHP can't access giving the error-message: shmop_open(): unable to attach or create shared memory segment. As $mode i set "a" for only read-permissions. As $security i set 0777 for all-access... As $size i set 10 byte.

如PHP手册中所述,他们说如果我尝试附加到现有共享内存上,则应将$ security和$ size设置为0,但这也不起作用.

As written in the PHP-manual they say i should set 0 for $security AND $size, if i'm trying to attach to a existing shared-memory, but this is also not working.

我怎样才能完成这个概念?我猜想C ++的BaseAdress与PHP中的$ key不同,但是 我怎样才能告诉PHP共享内存块在哪里? 如果这不可能使它正常工作:是否会有另一种方法将数据从C ++程序传输到PHP(在wamp-server上运行)?

How can i get this concept done? I guess that the BaseAdress of C++ is not the same like the $key in PHP, but how can i tell PHP then where the shared-memory block is? If this is impossible to get this working: Would there be another way to transmit data from a C++ program to PHP (running on wamp-server)?

PS:正如我最近在其他问题中读到的那样,似乎无法与共享内存进行通信……我以前从未使用过命名管道,但是如何使用命名管道来解决我的问题呢?还是有更好/更快的方法来启用C ++和PHP之间的通信?

PS: as i recently read in other questions, it seems to be impossible to communicate with shared-memory... I never worked with named-pipes before, but how would my problem be realized using named-pipes? Or is there a better/faster way to enable communication between C++ and PHP?

推荐答案

我建议您使用Redis处理C ++和PHP之间的通信. 您可以使用他的Pub/sub模块,功能非常强大. http://redis.io/topics/pubsub . 在这里,您有redis的客户端列表: http://redis.io/clients

I'd recommend you to use Redis to deal with the communications between C++ and PHP. You can use his Pub /sub module, is very powerfull. http://redis.io/topics/pubsub. Here you have a list of clients for redis: http://redis.io/clients

这篇关于PHP-访问使用C ++创建的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆