Windows 上共享内存的映射视图数 [英] Number of mapped views to a shared memory on Windows

查看:64
本文介绍了Windows 上共享内存的映射视图数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to check how many views have been mapped on to a memory mapped file on Windows?

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

Something like the equivalent of shmctl(... ,IPC_STAT,...) on Linux?

推荐答案

我同样需要访问共享视图的数量.所以我创建了这个问题:访问共享内存的数量映射文件视图 (Windows)

I had the same need to access the number of shared views. So I created this question: Accessing the number of shared memory mapped file views (Windows)

您可能会在那里找到适合您需求的解决方案.

You may find a solution that suits your needs there.

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

As per Scath comment, I am going to add here the proposed solution, although merit should go to eryksun and RbMm. Making use of NtQueryObject call one can access the HandleCount (although it may not be 100% reliable):

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

这篇关于Windows 上共享内存的映射视图数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆