Exemple #1
0
        public static unsafe SharedMemory Create(string name, Guid content, List <SecurityIdentifier> allowedSids)
        {
            int errorCode = UnsafeNativeMethods.ERROR_SUCCESS;

            byte[] binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ);
            SafeFileMappingHandle fileMapping;

            UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES();
            fixed(byte *pinnedSecurityDescriptor = binarySecurityDescriptor)
            {
                securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor;
                fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), name);
                errorCode   = Marshal.GetLastWin32Error();
            }

            if (fileMapping.IsInvalid)
            {
                fileMapping.SetHandleAsInvalid();
                fileMapping.Close();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode));
            }

            SharedMemory         sharedMemory = new SharedMemory(fileMapping);
            SafeViewOfFileHandle view;

            // Ignore return value.
            GetView(fileMapping, true, out view);

            try
            {
                SharedMemoryContents *contents = (SharedMemoryContents *)view.DangerousGetHandle();
                contents->pipeGuid = content;
                Thread.MemoryBarrier();
                contents->isInitialized = true;
                return(sharedMemory);
            }
            finally
            {
                view.Close();
            }
        }
        public static unsafe SharedMemory Create(string name, Guid content, List <SecurityIdentifier> allowedSids)
        {
            SafeFileMappingHandle handle;
            SafeViewOfFileHandle  handle2;
            SharedMemory          memory2;
            int error = 0;

            byte[] buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648);
            UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES();
            fixed(byte *numRef = buffer)
            {
                securityAttributes.lpSecurityDescriptor = (IntPtr)numRef;
                handle = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), name);
                error  = Marshal.GetLastWin32Error();
            }

            if (handle.IsInvalid)
            {
                handle.SetHandleAsInvalid();
                handle.Close();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
            }
            SharedMemory memory = new SharedMemory(handle);

            GetView(handle, true, out handle2);
            try
            {
                SharedMemoryContents *contentsPtr = (SharedMemoryContents *)handle2.DangerousGetHandle();
                contentsPtr->pipeGuid = content;
                Thread.MemoryBarrier();
                contentsPtr->isInitialized = true;
                memory2 = memory;
            }
            finally
            {
                handle2.Close();
            }
            return(memory2);
        }