C++ 写入映射文件

C++ 写入映射文件,c++,winapi,process,C++,Winapi,Process,我使用的是windows.h库,我使用了两个进程,一个进程应该打开一个文件,第二个进程应该将第一个字节更改为“Z”。 我在尝试写入文件“如何正确执行”时遇到运行时错误 hFile = CreateFileA( pFileName, // file name GENERIC_WRITE, // access type 0, // other processes can't share NULL, // security

我使用的是windows.h库,我使用了两个进程,一个进程应该打开一个文件,第二个进程应该将第一个字节更改为“Z”。 我在尝试写入文件“如何正确执行”时遇到运行时错误

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
这是第二个过程:

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);

我在尝试写入文件“how”时遇到运行时错误 我能做好吗

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
错误检查可以帮助您找出错误的确切原因以及发生在哪里。使用前请确保上一个函数返回的值有效。

下面是一个对我有用的例子。您可以尝试一下:

在一个进程中创建文件映射

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();
在另一个文件夹中打开文件映射

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);

您仅以写访问权限打开文件,但尝试创建具有读写页面保护的节(即文件映射)。这将在访问被拒绝的情况下失败(5),但您不会在任何地方检查失败。此外,请确定是要使用显式链接还是旧式TCHAR系统。调用
CreateFileA
,它显式使用多字节字符串API(即ANSI),人们希望
pFileName
是一个字节字符串。然后使用
CreateFileMapping
宏,而不是显式调用
CreateFileMappingW
(宽字符API),我们希望定义了
UNICODE
,因为传递给它的是一个宽字符串
L“myFile
。毫无疑问,为什么要使用Windows API来实现STL似乎可行的功能?也许我错过了一些关于你实际上想要完成的事情。@ DRA:C++中的标准库中没有任何东西可以进行进程间的交流。真正的教训就是学习错误检查。在你做到这一点之前,你根本不知道什么是失败的,为什么失败。