public unsafe Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity)
 {
     SafeWaitHandle handle;
     if (initialCount < 0)
     {
         throw new ArgumentOutOfRangeException("initialCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired"));
     }
     if (maximumCount < 1)
     {
         throw new ArgumentOutOfRangeException("maximumCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired"));
     }
     if (initialCount > maximumCount)
     {
         throw new ArgumentException(SR.GetString("Argument_SemaphoreInitialMaximum"));
     }
     if ((name != null) && (MAX_PATH < name.Length))
     {
         throw new ArgumentException(SR.GetString("Argument_WaitHandleNameTooLong"));
     }
     if (semaphoreSecurity != null)
     {
         Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES structure = null;
         structure = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES {
             nLength = Marshal.SizeOf(structure)
         };
         fixed (byte* numRef = semaphoreSecurity.GetSecurityDescriptorBinaryForm())
         {
             structure.lpSecurityDescriptor = new SafeLocalMemHandle((IntPtr) numRef, false);
             handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(structure, initialCount, maximumCount, name);
         }
     }
     else
     {
         handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name);
     }
     int num = Marshal.GetLastWin32Error();
     if (handle.IsInvalid)
     {
         if (((name != null) && (name.Length != 0)) && (6 == num))
         {
             throw new WaitHandleCannotBeOpenedException(SR.GetString("WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name }));
         }
         InternalResources.WinIOError();
     }
     createdNew = num != 0xb7;
     base.SafeWaitHandle = handle;
 }
Пример #2
0
        public unsafe Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity)
#endif
        {
            if (initialCount < 0)
            {
                throw new ArgumentOutOfRangeException("initialCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
            }

            if (maximumCount < 1)
            {
                throw new ArgumentOutOfRangeException("maximumCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
            }

            if (initialCount > maximumCount)
            {
                throw new ArgumentException(SR.GetString(SR.Argument_SemaphoreInitialMaximum));
            }
            
            if(null != name && MAX_PATH < name.Length)
            {
                throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
            }
            SafeWaitHandle   myHandle;
#if !FEATURE_PAL && !FEATURE_NETCORE
            // For ACL's, get the security descriptor from the SemaphoreSecurity.
            if (semaphoreSecurity != null) {
                NativeMethods.SECURITY_ATTRIBUTES secAttrs = null;
                secAttrs = new NativeMethods.SECURITY_ATTRIBUTES();
                secAttrs.nLength = (int)Marshal.SizeOf(secAttrs);
                byte[] sd = semaphoreSecurity.GetSecurityDescriptorBinaryForm();
                fixed(byte* pSecDescriptor = sd) {                
                    secAttrs.lpSecurityDescriptor = new SafeLocalMemHandle((IntPtr) pSecDescriptor, false);
                    myHandle = SafeNativeMethods.CreateSemaphore(secAttrs, initialCount, maximumCount, name);
                }
            }
            else {
#endif
                myHandle = SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name);
#if !FEATURE_PAL && !FEATURE_NETCORE
            }
#endif
            int errorCode = Marshal.GetLastWin32Error();
            if (myHandle.IsInvalid)
            {
                if(null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
                    throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle,name));
                InternalResources.WinIOError();
            }
            createdNew = errorCode != NativeMethods.ERROR_ALREADY_EXISTS;
            this.SafeWaitHandle = myHandle;
        }