Пример #1
0
        /// <summary>
        /// Gets the formats that are currently available on the clipboard.
        /// </summary>
        public unsafe static uint[] GetAvailableClipboardFormats()
        {
            uint count;

            Span <uint>        initialBuffer = stackalloc uint[5];
            ValueBuffer <uint> buffer        = new ValueBuffer <uint>(initialBuffer);

            while (!Imports.GetUpdatedClipboardFormats(ref MemoryMarshal.GetReference(buffer.Span), (uint)buffer.Length, out count))
            {
                Error.ThrowIfLastErrorNot(WindowsError.ERROR_INSUFFICIENT_BUFFER);
                buffer.EnsureCapacity((int)count);
            }

            return(buffer.Span.Slice(0, (int)count).ToArray());
        }
Пример #2
0
        private unsafe void CheckAlignment <T>(int alignment) where T : unmanaged
        {
            ValueBuffer <T> t = new ValueBuffer <T>();

            try
            {
                t.EnsureCapacity(42);
                void *p       = Unsafe.AsPointer(ref t.Span[0]);
                ulong address = (ulong)p;
                int   sizeOfT = Unsafe.SizeOf <T>();
                int   offset  = (int)(address % (ulong)alignment);
                offset.Should().Be(0);
            }
            finally
            {
                t.Dispose();
            }
        }
Пример #3
0
        public unsafe string GetTestValueBuffer()
        {
            Span <char>        stack  = stackalloc char[128];
            ValueBuffer <char> buffer = new ValueBuffer <char>(stack);

            fixed(char *n = "USERNAME")
            fixed(char *c = &buffer[0])
            {
                uint returnValue;

                while ((returnValue = Imports.GetEnvironmentVariableW(n, c, (uint)buffer.Length)) > buffer.Length)
                {
                    buffer.EnsureCapacity((int)returnValue);
                }

                if (returnValue == 0)
                {
                    Error.ThrowIfLastErrorNot(WindowsError.ERROR_ENVVAR_NOT_FOUND);
                    return(string.Empty);
                }
                return(new string(buffer.Span.Slice(0, (int)returnValue)));
            }
        }