Copy Memory Block

1.8k views Asked by At
//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;
}

I want to copy just 4 bytes: (the "value1" integer variable into my memory block).

When I try to compile I get this error:

Error 1 error C2036: 'PVOID' : unknown size

1

There are 1 answers

6
ScottMcP-MVP On BEST ANSWER

To add 3 bytes to a pointer use (PBYTE), not (PVOID)