简体   繁体   English

PHP - 访问使用C ++创建的共享内存

[英]PHP - Access shared Memory created with 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. 因为几天我试图让以下事情发挥作用:我有一个很小的C ++程序,可以从串口连续读取一些数据。 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 ...
}

Now i would like to access this shared memory with PHP. 现在我想用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! 但我不知道我应该设置哪个键,模式,安全性和大小!

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. 现在,在你写一些东西之前:我使用“MapViewOfFileEx()”,因为我想设置一个固定的地址,以便PHP可以从固定的地址读取。 I also tried this with "0x00030000" both in C++ and PHP. 我也用C ++和PHP中的“0x00030000”尝试了这个。 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. C ++能够创建FileMapping,但PHP无法访问,提供错误消息:shmop_open():无法附加或创建共享内存段。 As $mode i set "a" for only read-permissions. 作为$ mode,我只为读取权限设置“a”。 As $security i set 0777 for all-access... As $size i set 10 byte. 因为$ security我为所有访问设置了0777 ...因为$ size我设置了10个字节。

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. 正如在PHP手册中所写,他们说我应该为$ security和$ size设置0,如果我试图连接到现有的共享内存,但这也无效。

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? 我猜C ++的BaseAdress和PHP中的$ key不一样,但我怎么能告诉PHP那么共享内存块在哪里呢? 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)? 如果这不可能使这个工作:是否有另一种方式将数据从C ++程序传输到PHP(在wamp-server上运行)?

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? PS:正如我最近在其他问题中读到的那样,似乎无法与共享内存进行通信......我之前从未使用过命名管道,但是如何使用命名管道实现我的问题? Or is there a better/faster way to enable communication between C++ and PHP? 或者是否有更好/更快的方式来实现C ++和PHP之间的通信?

I'd recommend you to use Redis to deal with the communications between C++ and PHP. 我建议你使用Redis来处理C ++和PHP之间的通信。 You can use his Pub /sub module, is very powerfull. 你可以使用他的Pub / sub模块,非常强大。 http://redis.io/topics/pubsub . http://redis.io/topics/pubsub Here you have a list of clients for redis: http://redis.io/clients 在这里,您有redis的客户列表: http: //redis.io/clients

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM