<commit_before>﻿// Copyright (c) to owners found in https://github.com/AArnott/pinvoke/blob/master/COPYRIGHT.md. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.

namespace PInvoke
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using static PInvoke.Kernel32;

    /// <content>
    /// Methods and nested types that are not strictly P/Invokes but provide
    /// a slightly higher level of functionality to ease calling into native code.
    /// </content>
    public static partial class Hid
    {
        public static Guid HidD_GetHidGuid()
        {
            Guid guid;
            HidD_GetHidGuid(out guid);
            return guid;
        }

        public static HiddAttributes HidD_GetAttributes(SafeObjectHandle hFile)
        {
            var result = HiddAttributes.Create();
            if (!HidD_GetAttributes(hFile, ref result))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return result;
        }

        public static SafePreparsedDataHandle HidD_GetPreparsedData(SafeObjectHandle hDevice)
        {
            SafePreparsedDataHandle preparsedDataHandle;
            if (!HidD_GetPreparsedData(hDevice, out preparsedDataHandle))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return preparsedDataHandle;
        }

        public static HidpCaps HidP_GetCaps(SafePreparsedDataHandle preparsedData)
        {
            var hidCaps = default(HidpCaps);
            var result = HidP_GetCaps(preparsedData, ref hidCaps);
            switch (result)
            {
                case NTStatus.HIDP_STATUS_SUCCESS:
                    return hidCaps;

                case NTStatus.HIDP_STATUS_INVALID_PREPARSED_DATA:
                    throw new ArgumentException("The specified preparsed data is invalid.", nameof(preparsedData));

                default:
                    result.ThrowOnError();
                    return default(HidpCaps);
            }
        }

        public static bool HidD_GetManufacturerString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetManufacturerString(hidDeviceObject, sb, sb.Capacity), out result);
        }

        public static bool HidD_GetProductString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetProductString(hidDeviceObject, sb, sb.Capacity), out result);
        }

        public static bool HidD_GetSerialNumberString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetSerialNumberString(hidDeviceObject, sb, sb.Capacity), out result);<commit_msg>Always throw in HidP_GetCaps if we don't have one of the two expected results<commit_after>﻿// Copyright (c) to owners found in https://github.com/AArnott/pinvoke/blob/master/COPYRIGHT.md. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.

namespace PInvoke
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using static PInvoke.Kernel32;

    /// <content>
    /// Methods and nested types that are not strictly P/Invokes but provide
    /// a slightly higher level of functionality to ease calling into native code.
    /// </content>
    public static partial class Hid
    {
        public static Guid HidD_GetHidGuid()
        {
            Guid guid;
            HidD_GetHidGuid(out guid);
            return guid;
        }

        public static HiddAttributes HidD_GetAttributes(SafeObjectHandle hFile)
        {
            var result = HiddAttributes.Create();
            if (!HidD_GetAttributes(hFile, ref result))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return result;
        }

        public static SafePreparsedDataHandle HidD_GetPreparsedData(SafeObjectHandle hDevice)
        {
            SafePreparsedDataHandle preparsedDataHandle;
            if (!HidD_GetPreparsedData(hDevice, out preparsedDataHandle))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return preparsedDataHandle;
        }

        public static HidpCaps HidP_GetCaps(SafePreparsedDataHandle preparsedData)
        {
            var hidCaps = default(HidpCaps);
            var result = HidP_GetCaps(preparsedData, ref hidCaps);
            switch (result)
            {
                case NTStatus.HIDP_STATUS_SUCCESS:
                    return hidCaps;

                case NTStatus.HIDP_STATUS_INVALID_PREPARSED_DATA:
                    throw new ArgumentException("The specified preparsed data is invalid.", nameof(preparsedData));

                default:
                    result.ThrowOnError();
                    throw new InvalidOperationException("HidP_GetCaps returned an unexpected success value");
            }
        }

        public static bool HidD_GetManufacturerString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetManufacturerString(hidDeviceObject, sb, sb.Capacity), out result);
        }

        public static bool HidD_GetProductString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetProductString(hidDeviceObject, sb, sb.Capacity), out result);
        }

        public static bool HidD_GetSerialNumberString(SafeObjectHandle hidDeviceObject, out string result)
        {
            return GrowStringBuffer(sb => HidD_GetSerialNumberString(hidDeviceObject, sb, sb.Capacity), out result);
{'fraction_non_alphanumeric': 0.06651997291807718, 'fraction_numerical': 0.002031144211238998, 'mean_word_length': 2.2827777777777776, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1855789', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1538, 'n_words': 390}