MapViewOfFile与errorCode 6失效(无效句柄)

时间:2015-08-04 15:39:34

标签: winapi memory handle error-code

我试图将文件映射到内存并使用MapViewOfFile(),但它错误地使用错误代码6.我尝试了几乎任何事情,我也读到了大文件是问题,但问题也发生在1kb文件。

我的代码:

test(@Html.Raw(JsonConvert.Serialize(lol)));

输出如下:

HANDLE hFile = CreateFile(pFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    e = GetLastError();
    printf("CreateFile Errorcode %d\n", GetLastError());
    if (hFile == INVALID_HANDLE_VALUE)
    {
        printf("Error: could not create handle to file");
        printf("CreateFileMapping error code: %d", e)
        return 1;
    }
    printf("successfully created a Handle to try.txt");
    HANDLE pMap = CreateFileMapping(hFile, NULL, PAGE_EXECUTE_READWRITE,0 ,0 ,NULL);
    e = GetLastError();
    if (pMap == INVALID_HANDLE_VALUE)
    {
        printf("Error: Unable to CreateFileMapping\n");
        printf("CreateFileMapping error code: %d", e)
        return 1;
    }
    printf("CreateFileMapping successfull.")

    LPVOID lpBase MapViewOfFile(pMap, FILE_MAP_ACCESS| FILE_MAP_EXECUTE, 0, 0, 0);
    e = GetLastError();
    if (!lpBase)
    {
        printf("Error: could not map file to memory");
        printf("MapViewOfFile Errorcode %d\n", GetLastError());
        CloseHandle(hFile);
        UnmapViewOfFile(lpBase);
        printf("closed hFile handle and unmapped lpBase.")
        return 1;
    }

1 个答案:

答案 0 :(得分:1)

下面:

  

HANDLE hFile = CreateFile(pFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

GENERIC_READ替换为GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE

此外,没有FILE_MAP_ACCESS,但FILE_MAP_ALL_ACCESS

我尝试了这段代码,它成功地映射了测试文件,不确定你想要进一步使用它。另外,对于检查问题,您还可以使用:来自sysinternals的Procmon.exe - 它将报告文件操作期间发生的问题。