我想读取应用程序通过共享内存提供的状态信息。我想使用C++来读取那个命名共享内存的内容,然后用C#类中的pinvoke来调用它。C++从共享内存中读取
从软件我知道它有一定的文件结构:一个结构STATUS_DATA
与一个由SYSTEM_CHARACTERISTICS
四个结构阵列。
我还没有熟悉C++,所以我试图基本上遵循msdn。为了找到要映射的文件的大小,我添加了结构成员的大小,以便在下面的代码中看到。这导致访问被拒绝,所以我想,基于结构的结果太高。当我使用sizeof(STATUS_DATA)
(我将结构添加到我的源代码中)时,它仍然以ACCESS DENIED结尾。如果我尝试一些更低的内容,比如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;
}
我也按照该hint确信,这个共享纪念品“是存在的”。有人能给我一个提示,我错过了什么?谢谢!
就是这样,谢谢! – rdoubleui 2009-10-12 13:17:51