简体   繁体   English

C ++从共享内存读取

[英]C++ Read from shared memory

I want to read status information that an application provides via shared memory. 我想读取应用程序通过共享内存提供的状态信息。 I want to use C++ in order to read the content of that named shared memory and then call it with pinvoke from a C#-class. 我想使用C ++来读取已命名共享内存的内容,然后使用C#类中的pinvoke对其进行调用。

From the software I know that it has a certain file structure: A struct STATUS_DATA with an array of four structs of SYSTEM_CHARACTERISTICS . 从软件中,我知道它具有一定的文件结构:一个STATUS_DATA结构,具有SYSTEM_CHARACTERISTICS四个结构的数组。

I'm not (yet) familiar with C++, so I tried to follow msdn basically. 我(还)不熟悉C ++,因此我尝试基本遵循msdn。 To find the size of the file to be mapped, I added the sizes of the struct members as to be seen in the code below. 为了找到要映射的文件的大小,我添加了结构成员的大小,如下面的代码所示。 This results in a ACCESS DENIED, so I figured, that the result based on the structs is too high. 这导致访问被拒绝,因此我认为基于结构的结果过高。 When I use sizeof(STATUS_DATA) (I added the struct to my source), it still ends up in an ACCESS DENIED. 当我使用sizeof(STATUS_DATA) (将结构添加到源代码)时,它仍然以ACCESS DENIED结尾。 If I try something lower, like 1024 Bytes, only thing I can see in pbuf is a < , while debugging. 如果我尝试较低的值(例如1024字节),则在调试时只能在pbuf看到<

This is what I got so far: 这是我到目前为止所得到的:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib, "user32.lib")

using namespace std;


signed int BUF_SIZE = 4 * (10368 + 16 + 4 + 16 + 4 + 16 + 4 + 1 + 4); // sizeof(STATUS_DATA);
TCHAR szName[]=TEXT("ENGINE_STATUS");

int main()
{
   HANDLE hMapFile;
   unsigned char* pBuf;

   hMapFile = OpenFileMapping(
                   FILE_MAP_READ,    // read access
                   FALSE,                 // do not inherit the name
                   szName);               // name of mapping object 

   if (hMapFile == NULL) 
   { 
      _tprintf(TEXT("Could not open file mapping object (%d).\n"), 
             GetLastError());

      return 1;
   } 

   pBuf = (unsigned char*) MapViewOfFile(hMapFile, // handle to map object
               FILE_MAP_READ,  // read/write permission
               0,                    
               0,                    
               BUF_SIZE); // 1024);                  

   if (pBuf == NULL) 
   { 
      _tprintf(TEXT("Could not map view of file (%d).\n"), 
             GetLastError()); 

   CloseHandle(hMapFile);
      return 1;
   }

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

   return 0;
}

I also made sure that this Shared Mem "is there" by following that hint . 通过遵循该提示 ,我还确保此“共享内存”在“那里”。 Can somebody give me a hint, what I'm missing? 有人可以给我一个提示,我想念什么吗? Thanks! 谢谢!

The last parameter to MapViewOfFile (dwNumberOfBytesToMap) must be less than the maximum size specified when the mapping was created. MapViewOfFile的最后一个参数(dwNumberOfBytesToMap)必须小于创建映射时指定的最大大小。 Since we don't know what that size is, it seems fair to assume that BUF_SIZE is exceeding it and 1024 isn't. 由于我们不知道该大小是多少,因此可以假设BUF_SIZE超过了它而1024没有超过它。 Specifying 0 for this parameter is an easy way to map the entire file into a single view. 为该参数指定0是将整个文件映射到单个视图的简便方法。

Most (all?) C++ debuggers will assume that a pointer to char is a null-terminated string, so when you try and view the mapped data it will only display up until the first byte that is zero. 大多数(全部?)C ++调试器都假定指向char的指针是一个以Null结尾的字符串,因此当您尝试查看映射的数据时,它将一直显示到第一个字节为零为止。 Depending on what data is in the file mapping, this could well be the second byte, which explains why you aren't seeing much information. 根据文件映射中的数据,这很可能是第二个字节,这说明了为什么您看不到太多信息。 You would be better to cast the returned pointer to STATUS_DATA* and viewing the individual members. 您最好将返回的指针转换为STATUS_DATA *并查看各个成员。

In short: 简而言之:

  • Specify zero (0) for dwNumberOfBytesToMap 为dwNumberOfBytesToMap指定零(0)
  • Cast the returned pointer to STATUS_DATA* instead of unsigned char* 将返回的指针强制转换为STATUS_DATA *而不是未签名的char *

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

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