Windows

时间:2017-01-11 14:30:21

标签: windows shared-memory memory-mapped-files

有没有办法检查在Windows上映射到内存映射文件的视图数量?

类似于Linux上的shmctl(... ,IPC_STAT,...)

1 个答案:

答案 0 :(得分:2)

我有同样的需要访问共享视图的数量。所以我创建了这个问题:Accessing the number of shared memory mapped file views (Windows)

您可以找到适合您需求的解决方案。

根据Scath评论,我将在此处添加建议的解决方案,但优点应该转到eryksunRbMm。使用NtQueryObject调用可以访问HandleCount(虽然它可能不是100%可靠):

#include <stdio.h>
#include <windows.h>
#include <winternl.h>

typedef NTSTATUS (__stdcall *NtQueryObjectFuncPointer) (
            HANDLE                   Handle,
            OBJECT_INFORMATION_CLASS ObjectInformationClass,
            PVOID                    ObjectInformation,
            ULONG                    ObjectInformationLength,
            PULONG                   ReturnLength);

int main(void)
{
    _PUBLIC_OBJECT_BASIC_INFORMATION pobi;
    ULONG rLen;

    // Create the memory mapped file (in system pagefile) (better in global namespace
    // but needs SeCreateGlobalPrivilege privilege)
    HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT,
                  0, 1, "Local\\UniqueShareName");

    // Get the NtQUeryObject function pointer and then the handle basic information
    NtQueryObjectFuncPointer _NtQueryObject = (NtQueryObjectFuncPointer)GetProcAddress(
            GetModuleHandle("ntdll.dll"), "NtQueryObject");

    _NtQueryObject(hMap, ObjectBasicInformation, (PVOID)&pobi, (ULONG)sizeof(pobi), &rLen);

    // Check limit
    if (pobi.HandleCount > 4) {
        printf("Limit exceeded: %ld > 4\n", pobi.HandleCount);
        exit(1);
    }
    //...
    Sleep(30000);
}