C++从共享内存中读取

发布于 2024-08-07 16:25:29 字数 1872 浏览 2 评论 0原文

我想读取应用程序通过共享内存提供的状态信息。我想使用 C++ 来读取该命名共享内存的内容,然后从 C# 类中使用 pinvoke 调用它。

从软件中我知道它具有特定的文件结构:一个结构体STATUS_DATA,其中包含四个SYSTEM_CHARACTERISTICS结构体的数组。

我还不熟悉 C++,所以我基本上尝试遵循 msdn。为了找到要映射的文件的大小,我添加了结构成员的大小,如下面的代码所示。这会导致访问被拒绝,所以我认为基于结构的结果太高了。当我使用 sizeof(STATUS_DATA) (我将结构添加到我的源代码中)时,它仍然以“访问被拒绝”结束。如果我尝试更低的值,例如 1024 字节,则在调试时我在 pbuf 中只能看到 <

这就是我到目前为止得到的:

#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 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.

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.

I'm not (yet) familiar with C++, so I tried to follow msdn basically. 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. If I try something lower, like 1024 Bytes, only thing I can see in pbuf is a <, while debugging.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-08-14 16:25:29

MapViewOfFile (dwNumberOfBytesToMap) 的最后一个参数必须小于创建映射时指定的最大大小。由于我们不知道该大小是多少,因此可以合理地假设 BUF_SIZE 超过了它,而 1024 没有超过。为此参数指定 0 是将整个文件映射到单个视图的简单方法。

大多数(全部?)C++ 调试器会假设指向 char 的指针是一个以 null 结尾的字符串,因此当您尝试查看映射数据时,它只会显示到第一个字节为零为止。根据文件映射中的数据,这很可能是第二个字节,这解释了为什么您看不到太多信息。您最好将返回的指针强制转换为 STATUS_DATA* 并查看各个成员。

简而言之:

  • 为 dwNumberOfBytesToMap 指定零 (0)
  • 将返回的指针转换为 STATUS_DATA* 而不是 unsigned char*

The last parameter to MapViewOfFile (dwNumberOfBytesToMap) must be less than the maximum size specified when the mapping was created. Since we don't know what that size is, it seems fair to assume that BUF_SIZE is exceeding it and 1024 isn't. Specifying 0 for this parameter is an easy way to map the entire file into a single view.

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. 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.

In short:

  • Specify zero (0) for dwNumberOfBytesToMap
  • Cast the returned pointer to STATUS_DATA* instead of unsigned char*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文