Ejemplo n.º 1
0
        private NamedEvent(string name)
        {
            using (SecurityDescriptorSafePtr securityDescriptor = Security.CreateDefaultSecurityDescriptor())
            {
                var securityAttr = new Native.SECURITY_ATTRIBUTES
                {
                    Length             = (uint)Marshal.SizeOf <Native.SECURITY_ATTRIBUTES>(),
                    InheritHandle      = false,
                    SecurityDescriptor = securityDescriptor.DangerousGetHandle(),
                };

                eventHandle = Native.CreateEvent(
                    ref securityAttr,
                    manualReset: false,
                    initialState: false,
                    name);

                if (eventHandle.IsInvalid)
                {
                    throw new IOException(
                              $"Failed to create a NamedEvent {name}",
                              innerException: new Win32Exception(Marshal.GetLastWin32Error()));
                }

                Security.VerifyHandleOwner(eventHandle);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates or opens a shared memory view.
        /// </summary>
        /// <param name="sharedMemoryMapName"></param>
        /// <param name="sharedMemorySize"></param>
        /// <returns></returns>
        public static new SharedMemoryMapView CreateOrOpen(string sharedMemoryMapName, ulong sharedMemorySize)
        {
            SharedMemorySafeHandle sharedMemoryHandle;

            using (SecurityDescriptorSafePtr securityDescriptor = Security.CreateDefaultSecurityDescriptor())
            {
                var securityAttr = new Native.SECURITY_ATTRIBUTES
                {
                    Length             = (uint)Marshal.SizeOf <Native.SECURITY_ATTRIBUTES>(),
                    InheritHandle      = false,
                    SecurityDescriptor = securityDescriptor.DangerousGetHandle(),
                };

                Utils.SplitULong(sharedMemorySize, out uint sharedMemorySizeHigh, out uint sharedMemorySizeLow);

                sharedMemoryHandle = Native.CreateFileMapping(
                    Native.InvalidPointer,
                    ref securityAttr,
                    Native.FileMapProtection.PageReadWrite,
                    sharedMemorySizeHigh,
                    sharedMemorySizeLow,
                    sharedMemoryMapName);
            }

            if (sharedMemoryHandle.IsInvalid)
            {
                throw new FileNotFoundException(
                          $"Failed to CreateFileMapping {sharedMemoryMapName}",
                          innerException: new Win32Exception(Marshal.GetLastWin32Error()));
            }

            Security.VerifyHandleOwner(sharedMemoryHandle);

            return(new SharedMemoryMapView(sharedMemoryHandle, sharedMemorySize));
        }