我想讀取應用程序通過共享內存提供的狀態信息。我想使用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