Пример #1
0
        public override bool BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            var bytes = buffer.GetBytes();

            return(bytes.Any(b => b != 0));
        }
        public override Guid BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            var bytes = buffer.GetBytes(0, 16);

            return(new Guid(bytes));
        }
        public override TStruct BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            // Construct a new TStruct from the data in the buffer.
            var value = Marshal.PtrToStructure(buffer.Data, typeof(TStruct));

            return((TStruct)value);
        }
Пример #4
0
        public override TInteger BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            var integerSize = Marshal.SizeOf(typeof(TInteger));
            var bytes       = buffer.GetBytes(0, integerSize);

            return(_fromBytes(bytes));
        }
Пример #5
0
        public override GenericSecurityDescriptor BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            var bytes = buffer.GetBytes();

            var securityDescriptor = new RawSecurityDescriptor(bytes, 0);

            return(securityDescriptor);
        }
        public override Api.Buffer ValueToBuffer(TStruct value)
        {
            // Allocate a new buffer of the right size to hold the value.
            var valueSize = Marshal.SizeOf(typeof(TStruct));
            var buffer    = new Api.Buffer(valueSize);

            // Copy the struct's contents into the buffer. The existing value doesn't need to be freed because the
            // buffer hasn't held anything yet.
            Marshal.StructureToPtr(value, buffer.Data, false);

            return(buffer);
        }
Пример #7
0
        public static string GetErrorMessage(int errorCode)
        {
            const int formatMessageFromSystem = 0x1000;

            using (var buffer = new Api.Buffer(4096))
            {
                int messageLen = FormatMessage(formatMessageFromSystem, IntPtr.Zero, errorCode, 0, buffer.Data, buffer.Length, IntPtr.Zero);
                if (messageLen == 0)
                {
                    return(string.Format("Unknown error code [0].", errorCode));
                }

                var messageBytes = buffer.GetBytes(0, messageLen * 2);
                return(Encoding.Unicode.GetString(messageBytes));
            }
        }
        public override string BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            var bytes = buffer.GetBytes();

            // Figure out how much data there is, ignoring any null terminator.
            var byteLength = bytes.Length;

            if (byteLength > 2 && bytes[byteLength - 1] == 0 && bytes[byteLength - 2] == 0)
            {
                byteLength -= 2;
            }

            return(Encoding.Unicode.GetString(bytes, 0, byteLength));
        }
        public override string[] BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            // All strings to be returned.
            var result = new List <string>();

            // Variables used during the string conversions.
            var data   = buffer.GetBytes();
            var offset = 0;
            var length = 0;

            // Don't go past the end of the data.
            while (offset < data.Length)
            {
                // Find out how many bytes are used for the string.
                for (length = 0; length + 2 <= data.Length; length += 2)
                {
                    if (data[offset + length] == 0 && data[offset + length + 1] == 0)
                    {
                        break;
                    }
                }

                // The list is terminate by a single empty string.
                if (length == 0)
                {
                    break;
                }

                // Get the string and add it to the result.
                var item = Encoding.Unicode.GetString(data, offset, length);
                result.Add(item);

                // Update the offset to point to the next string.
                offset += length + 2;
            }

            // Return the converted strings.
            return(result.ToArray());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="propertyKey"></param>
        /// <param name="propertyDataType"></param>
        /// <param name="propertyData"></param>
        /// <returns></returns>
        public static bool GetDeviceProperty(DeviceInfo device, DevicePropertyKey propertyKey,
                                             out DevicePropertyType propertyDataType, out Api.Buffer propertyData)
        {
            var deviceInfoList    = device.InfoSet.InfoSet;
            var deviceInfoData    = device.InfoData;
            var devicePropertyKey = propertyKey.PropertyKey;

            propertyData = new Api.Buffer();
            int requiredSize;

            // Keep trying until we've got success or an unrecoverable error.
            while (true)
            {
                var success = GetDeviceProperty(deviceInfoList, ref deviceInfoData, ref devicePropertyKey,
                                                out propertyDataType, propertyData.Data, propertyData.Length, out requiredSize, 0);

                // If the data was read successfully, truncate the buffer to match the length read.
                if (success)
                {
                    propertyData.Truncate(requiredSize);
                    return(true);
                }

                // If the last error was for anything except the buffer being too small, cleanly get rid of the buffer
                // before returning failure.
                var lastError = ErrorHelpers.GetLastError();
                if (lastError != ErrorCode.InsufficientBuffer)
                {
                    propertyData.Dispose();
                    propertyData = null;

                    return(false);
                }

                // Resize the buffer to the required length before trying again.
                propertyData.Resize(requiredSize);
            }
        }
Пример #11
0
 /// <summary>
 /// Converts a value from an unmanaged format to its supported form.
 /// </summary>
 /// <param name="buffer">A <see cref="Api.Buffer"/> containing the unmanaged form of the value.</param>
 /// <returns>The supported form of the data in the buffer.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffer"/> is <c>null</c>.</exception>
 public abstract object BufferToObject(Api.Buffer buffer);
Пример #12
0
        public override byte[] BufferToValue(Api.Buffer buffer)
        {
            buffer.ThrowIfNull("buffer");

            return(buffer.GetBytes());
        }