וי חנ
וי חנ

Reputation: 79

Write into mapped file

I am using the windows.h library and I used 2 processes, one should open a file and the second process should change the first byte to "Z". I am getting a run time error when trying to write into the file' how can I do it properly?

hFile = CreateFileA(
        pFileName, // file name
        GENERIC_WRITE, // access type
        0, // other processes can't share
        NULL, // security
        OPEN_EXISTING, // open only if file exists
        FILE_ATTRIBUTE_NORMAL,
        NULL);


    // create file mapping object
    HANDLE hMapFile;
    hMapFile = CreateFileMapping(
        hFile, // file handle
        NULL, // default security
        PAGE_READWRITE, // read/write access
        0, // maximum object size (high-order DWORD)
        0, // maximum object size (low-order DWORD)
        // 0 means map the whole file
        L"myFile"); // name of mapping object, in case we
        // want to share it
        // read the file, one page at a time

and here is the second proccess:

hFile = OpenFileMapping(FILE_MAP_WRITE, TRUE, L"myFile");

    LPVOID lpMapAddress = MapViewOfFile(hFile,            // handle to
                                                      // mapping object
        FILE_MAP_ALL_ACCESS, // read/write
        NULL,                   // high-order 32
                             // bits of file
                             // offset
        NULL,      // low-order 32
                             // bits of file
                             // offset
        NULL);                // number of bytes
    sprintf((char*)lpMapAddress, "Z");
    UnmapViewOfFile(lpMapAddress);
    CloseHandle(hFile);
    CloseHandle(hFile);

Upvotes: 1

Views: 1902

Answers (1)

Rita Han
Rita Han

Reputation: 9700

I am getting a run time error when trying to write into the file' how can I do it properly?

Error checking can help you find out what exact the error is and where is it happened. Make sure the value returned from last function is valid before use it.

The following is an example working for me. You can have a try:

Create file mapping in one process.

HANDLE hFile = CreateFileA(
    "test.txt", // file name
    GENERIC_READ | GENERIC_WRITE, // access type
    0, // other processes can't share
    NULL, // security
    OPEN_EXISTING, // open only if file exists
    FILE_ATTRIBUTE_NORMAL,
    NULL);
if (hFile == INVALID_HANDLE_VALUE)
    printf("CreateFileA error: %d \n", GetLastError());


// create file mapping object
HANDLE hMapFile;
hMapFile = CreateFileMapping(
    hFile, // file handle
    NULL, // default security
    PAGE_READWRITE, // read/write access
    0, // maximum object size (high-order DWORD)
    0, // maximum object size (low-order DWORD)
    // 0 means map the whole file
    L"myFile"); // name of mapping object, in case we
    // want to share it
    // read the file, one page at a time
if (hMapFile == NULL)
    printf("CreateFileMapping error: %d \n", GetLastError());

getchar();

Open the file mapping in another.

HANDLE hFile = OpenFileMapping(FILE_MAP_WRITE, TRUE, L"myFile");
if (hFile == NULL)
    printf("OpenFileMapping error: %d \n", GetLastError());

LPVOID lpMapAddress = MapViewOfFile(hFile,            // handle to
                                                  // mapping object
    FILE_MAP_ALL_ACCESS, // read/write
    NULL,                   // high-order 32
                         // bits of file
                         // offset
    NULL,      // low-order 32
                         // bits of file
                         // offset
    NULL);                // number of bytes
if (lpMapAddress == NULL)
    printf("MapViewOfFile error: %d \n", GetLastError());

char newData[] = "Z";

snprintf((char*)lpMapAddress, sizeof(newData), newData);
UnmapViewOfFile(lpMapAddress);
CloseHandle(hFile);

Upvotes: 3

Related Questions