2

尝试打开以前创建的文件映射时,此代码使我访问被拒绝错误。帮助,帮助,帮助。

  • 操作系统:WinXP SP3,无快速用户切换。
  • 以下代码使用 runas 在 2 个不同的用户帐户上运行。
  • 两个用户都具有管理员权限。

测试代码:

SECURITY_DESCRIPTOR sd;

if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
    printf("InitializeSecurityDescriptor failed %d\n", GetLastError());

if(!SetSecurityDescriptorDacl(&sd, true, 0, false))
    printf("SetSecurityDescriptorDacl failed %d\n", GetLastError());

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = false;

HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0x1000, "Global\\MyMap");
void* pMap = 0;
if(hMap) {
    pMap = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0x1000);
} else {
    printf("CreateFileMapping failed: %d\n", GetLastError());
    hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, "Global\\MyMap");
    if(hMap) {
        printf("OpenFileMapping sucess!\n");
    } else {
        printf("OpenFileMapping failed: %d\n", GetLastError());
    }
}
if(pMap) {
    printf("Sucess!\n");
    printf("DW: %X", *(DWORD*)pMap);
    *(DWORD*)pMap = 0xDEADBEEF;
} else {
    printf("MapViewOfFile failed: %d\n", GetLastError());
}

cin.ignore();

if(pMap)
    UnmapViewOfFile(pMap);

if(hMap)
    CloseHandle(hMap);
4

1 回答 1

1

您忘记将 SECURITY_ATTRIBUTES 结构传递给 CreateFileMapping ...

于 2012-06-01T11:42:11.700 回答