Accessing the number of shared memory mapped file views (Windows)

核能气质少年 提交于 2019-12-05 07:24:46

as noted eryksun, most reliable way for do this - use CreateNamedPipe function. we can use nMaxInstances parameter - The maximum number of instances that can be created for this pipe. in next way. every instances of the application on start try create pipe instance. if this is ok - we can run, otherwise the limit is reached.

code can be next:

BOOL IsLimitReached(ULONG MaxCount)
{
    SECURITY_DESCRIPTOR sd;
    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);

    SECURITY_ATTRIBUTES sa = { sizeof(sa), &sd, FALSE };

    HANDLE hPipe = CreateNamedPipe(L"\\\\.\\pipe\\<some pipe>", 
        PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE, MaxCount, 0, 0, 0, &sa);

    if (hPipe == INVALID_HANDLE_VALUE)
    {
        ULONG dwError = GetLastError();

        if (dwError != ERROR_PIPE_BUSY)
        {
            // handle error 
        }
        return TRUE;
    }

    return FALSE;
}

and use, say for N instances

    if (!IsLimitReached(N))
    {
        MessageBoxW(0, L"running..",0,0);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!