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”整数变量到我的内存块中)。

当我尝试编译时,出现此错误:

错误 1 ​​错误 C2036:“PVOID”:未知大小

4

1 回答 1

0

要向指针添加 3 个字节,请使用 (PBYTE),而不是 (PVOID)

于 2013-08-22T13:57:13.270 回答