2013-08-22 59 views
1
//include headers. 
#include <windows.h> 
#include <iostream> 
#define BUF_SIZE 256 
TCHAR szName[]=TEXT("MyFileMappingObject"); 
LPCTSTR pBuf; 

//my data stracture. 
struct NetworkVariabel 
{ 
    int ServerTime; 
    int Value1; 
    int Value2; 
}; 

//main program. 
int main() 
{ 
    //map file handel. 
    HANDLE hMapFile; 
    hMapFile = CreateFileMapping(
     INVALID_HANDLE_VALUE, // use paging file 
     NULL,     // default security 
     PAGE_READWRITE,   // read/write access 
     0,      // maximum object size (high-order DWORD) 
     BUF_SIZE,    // maximum object size (low-order DWORD) 
     szName);     // name of mapping object 

    //if null. 
    if (hMapFile == NULL){ 
     return 0; 
    } 

    pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object 
     FILE_MAP_ALL_ACCESS, // read/write permission 
     0, 
     0, 
     BUF_SIZE); 

    //if null. 
    if (pBuf == NULL){ 
     CloseHandle(hMapFile); 
     return 0; 
    } 

    //my memory block data 
    NetworkVariabel BaseGlobalNetData; 
    BaseGlobalNetData.ServerTime=100; 
    BaseGlobalNetData.Value1=50; 
    BaseGlobalNetData.Value2=80; 

    CopyMemory((PVOID)pBuf, &BaseGlobalNetData,sizeof(BaseGlobalNetData) );//fill memory block this line work fine. 

    //my new memory block data 
    NetworkVariabel ReplaceNewGlobalNetData; 
    ReplaceNewGlobalNetData.Value1=988; 

    //now just want to replace 4byte(the Value1 variable data) into memory block so add to next 3 byte offset and 4byte Len because its integer ; 
    CopyMemory((PVOID)pBuf+3, &ReplaceNewGlobalNetData, sizeof(int) ); 

    //get a key. 
    getchar(); 

    //unmap. 
    UnmapViewOfFile(pBuf); 

    //close map. 
    CloseHandle(hMapFile); 

    return 0; 
} 

我想複製只有4個字節:(「value1」整數變量到我的內存塊)。複製內存塊

當我嘗試編譯我得到這個錯誤:

Error 1 error C2036: 'PVOID' : unknown size

+0

你有UNICODE指令集? –

回答

0

要增加3個字節的指針使用(PBYTE),未(PVOID)

+0

換句話說'(PVOID)(pBuf + 3)' –

+0

錯誤修正了,但是新的值沒有被替換爲那塊內存..新值必須是988 –

+0

這個值不是988它是-858993460 –