#include <windows.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hFile = CreateFile("one.dat", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        cout << "CreateFile is fail" << endl;
        return 0;
    }
    DWORD dwFileSize = GetFileSize(hFile, NULL);

    HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0,
        dwFileSize + 5 * sizeof(char), "wsTest");

    LPVOID lpView = NULL;
    if (hFileMap)
    {
        lpView = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
    }

    char *lpContent = (char*)lpView;
    strcpy(lpContent + dwFileSize / sizeof(char), "hello");

    UnmapViewOfFile(lpView);
    CloseHandle(hFileMap);
    CloseHandle(hFile);

    cin.get();
    return 0;
}

2011-08-10

发表评论

电子邮件地址不会被公开。 必填项已用*标注

鄂ICP备17003086号. Copyright © 2016. All Rights Reserved.