【问题标题】:Number of mapped views to a shared memory on WindowsWindows 上共享内存的映射视图数
【发布时间】:2017-01-11 14:30:21
【问题描述】:

有没有办法检查有多少视图已映射到 Windows 上的内存映射文件?

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

【问题讨论】:

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


【解决方案1】:

我同样需要访问共享视图的数量。所以我提出了这个问题: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);
}

【讨论】:

  • @Scath 感谢您的澄清,我希望现在的答案是“正确的”
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 2012-09-08
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多