SetErrorCode() private method

private SetErrorCode ( int hr ) : void
hr int
return void
Example #1
0
        internal unsafe static int WaitForMultipleObjects(IntPtr* pHandles, int numHandles, bool waitAll, int millisecondsTimeout)
        {
            Contract.Assert(millisecondsTimeout >= -1);

            //
            // In the CLR, we use CoWaitForMultipleHandles to pump messages while waiting in an STA.  In that case, we cannot use WAIT_ALL.  
            // That's because the wait would only be satisfied if a message arrives while the handles are signalled.
            //
            if (waitAll)
            {
                if (numHandles == 1)
                    waitAll = false;
                else if (GetCurrentApartmentType() == ApartmentType.STA)
                    throw new NotSupportedException(SR.NotSupported_WaitAllSTAThread);
            }

            int result;
            if (ReentrantWaitsEnabled)
            {
                Contract.Assert(!waitAll);
                result = RuntimeImports.RhCompatibleReentrantWaitAny(false, millisecondsTimeout, numHandles, pHandles);
            }
            else
            {
                result = (int)Interop.mincore.WaitForMultipleObjectsEx((uint)numHandles, (IntPtr)pHandles, waitAll, (uint)millisecondsTimeout, false);
            }

            if (result == WAIT_FAILED)
            {
                uint error = Interop.mincore.GetLastError();
                switch (error)
                {
                    case (uint)Interop.Constants.ErrorInvalidParameter:
                        throw new ArgumentException();

                    case (uint)Interop.Constants.ErrorAccessDenied:
                        throw new UnauthorizedAccessException();

                    case (uint)Interop.Constants.ErrorNotEnoughMemory:
                        throw new OutOfMemoryException();

                    default:
                        Exception ex = new Exception();
                        ex.SetErrorCode((int)error);
                        throw ex;
                }
            }

            return result;
        }
Example #2
0
        // This will create a new guid.  Since we've now decided that constructors should 0-init,
        // we need a method that allows users to create a guid.
        public static Guid NewGuid()
        {
            // CoCreateGuid should never return Guid.Empty, since it attempts to maintain some
            // uniqueness guarantees.  It should also never return a known GUID, but it's unclear
            // how extensively it checks for known values.
            Contract.Ensures(Contract.Result <Guid>() != Guid.Empty);

            Guid g;
            int  hr = Interop.mincore.CoCreateGuid(out g);

            // We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
            // in the HR to ComException mappings into the core library just for this so we will try a generic exception if
            // we ever hit this condition.
            if (hr != 0)
            {
                Exception ex = new Exception();
                ex.SetErrorCode(hr);
                throw ex;
            }
            return(g);
        }