• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# SP_DEVICE_INTERFACE_DATA类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中SP_DEVICE_INTERFACE_DATA的典型用法代码示例。如果您正苦于以下问题:C# SP_DEVICE_INTERFACE_DATA类的具体用法?C# SP_DEVICE_INTERFACE_DATA怎么用?C# SP_DEVICE_INTERFACE_DATA使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



SP_DEVICE_INTERFACE_DATA类属于命名空间,在下文中一共展示了SP_DEVICE_INTERFACE_DATA类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: SetupDiEnumDeviceInterfaces

 private static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr hDevInfo,
     ref SP_DEVINFO_DATA devInfo,
     ref Guid interfaceClassGuid,
     UInt32 memberIndex,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
     );
开发者ID:pengxiangqi1991,项目名称:Camera-Control,代码行数:7,代码来源:SetupApi.cs


示例2: SetupDiEnumDeviceInterfaces

 internal static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr DeviceInfoSet,
     IntPtr DeviceInfoData,
     ref System.Guid InterfaceClassGuid,
     Int32 MemberIndex,
     ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData
     );
开发者ID:hwchiu0810,项目名称:micro,代码行数:7,代码来源:setupapiDll.cs


示例3: SetupDiGetDeviceInterfaceDetail

 static extern Boolean SetupDiGetDeviceInterfaceDetail(
    IntPtr hDevInfo,
    ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
    ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
    UInt32 deviceInterfaceDetailDataSize,
    out UInt32 requiredSize,
    ref SP_DEVINFO_DATA deviceInfoData
 );
开发者ID:jedijosh920,项目名称:KeyboardAudio,代码行数:8,代码来源:KeyboardWriter.cs


示例4: SetupDiGetDeviceInterfaceDetail

        public static unsafe string SetupDiGetDeviceInterfaceDetail(
            SafeDeviceInfoSetHandle deviceInfoSet,
            SP_DEVICE_INTERFACE_DATA interfaceData,
            SP_DEVINFO_DATA* deviceInfoData)
        {
            int requiredSize;

            // First call to get the size to allocate
            SetupDiGetDeviceInterfaceDetail(
                deviceInfoSet,
                ref interfaceData,
                null,
                0,
                &requiredSize,
                deviceInfoData);

            // As we passed an empty buffer we know that the function will fail, not need to check the result.
            var lastError = GetLastError();
            if (lastError != Win32ErrorCode.ERROR_INSUFFICIENT_BUFFER)
            {
                throw new Win32Exception(lastError);
            }

            fixed (byte* pBuffer = new byte[requiredSize])
            {
                var pDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA*)pBuffer;
                pDetail->cbSize = SP_DEVICE_INTERFACE_DETAIL_DATA.ReportableStructSize;

                // Second call to get the value
                var success = SetupDiGetDeviceInterfaceDetail(
                    deviceInfoSet,
                    ref interfaceData,
                    pDetail,
                    requiredSize,
                    null,
                    null);

                if (!success)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    return null;
                }

                return SP_DEVICE_INTERFACE_DETAIL_DATA.GetDevicePath(pDetail);
            }
        }
开发者ID:jmelosegui,项目名称:pinvoke,代码行数:46,代码来源:SetupApi.Helpers.cs


示例5: GetDevicePath

        public string GetDevicePath(Guid classGuid)
        {
            IntPtr hDevInfo = IntPtr.Zero;
            try
            {
                hDevInfo = SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, DiGetClassFlags.DIGCF_DEVICEINTERFACE | DiGetClassFlags.DIGCF_PRESENT);

                if (hDevInfo.ToInt64() <= 0)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
                dia.cbSize = (uint)Marshal.SizeOf(dia);

                SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
                devInfo.cbSize = (UInt32)Marshal.SizeOf(devInfo);

                UInt32 i = 0;

                // start the enumeration
                if (!SetupDiEnumDeviceInterfaces(hDevInfo, null, ref classGuid, i, dia))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
                didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)

                UInt32 requiredSize = 0;

                if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, dia, ref didd, 256, out requiredSize, devInfo))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                return didd.DevicePath;

            }
            finally
            {
                SetupDiDestroyDeviceInfoList(hDevInfo);
            }
        }
开发者ID:bwillard,项目名称:home-integration-platform,代码行数:44,代码来源:DeviceFinder.cs


示例6: DevicePresent

        protected static bool DevicePresent(Guid g)
        {
            // Used to capture how many bytes are returned by system calls
            UInt32 theBytesReturned = 0;

            // SetupAPI32.DLL Data Structures
            SP_DEVINFO_DATA theDevInfoData = new SP_DEVINFO_DATA();
            theDevInfoData.cbSize = Marshal.SizeOf(theDevInfoData);
            IntPtr theDevInfo = SetupDiGetClassDevs(ref g, IntPtr.Zero, IntPtr.Zero, (int)(DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE));
            SP_DEVICE_INTERFACE_DATA theInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            theInterfaceData.cbSize = Marshal.SizeOf(theInterfaceData);

            // Check for the device
            if (!SetupDiEnumDeviceInterfaces(theDevInfo, IntPtr.Zero, ref g, 0, ref theInterfaceData) && GetLastError() == ERROR_NO_MORE_ITEMS)
                return false;

            // Get the device's file path
            SetupDiGetDeviceInterfaceDetail(theDevInfo, ref theInterfaceData, IntPtr.Zero, 0, ref theBytesReturned, IntPtr.Zero);

            return !(theBytesReturned <= 0);
        }
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:21,代码来源:GenericDevice.cs


示例7: SetupDiEnumDeviceInterfaces

 public static extern bool SetupDiEnumDeviceInterfaces(DeviceInfoHandle DeviceInfoSet, int DeviceInfoData, ref System.Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
开发者ID:GibeomGu,项目名称:flow,代码行数:1,代码来源:DeviceManagementDeclarations.cs


示例8: SetupDiGetDeviceInterfaceDetail

 static extern unsafe int SetupDiGetDeviceInterfaceDetail(
     int DeviceInfoSet,
     ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData,
     ref PSP_DEVICE_INTERFACE_DETAIL_DATA myPSP_DEVICE_INTERFACE_DETAIL_DATA,
     int detailSize,
     ref int requiredSize,
     int* bPtr);
开发者ID:Datenschredder,项目名称:MediaPortal-1,代码行数:7,代码来源:FutabaMDM166A.cs


示例9: CT_SetupDiEnumDeviceInterfaces

 //---#+************************************************************************
 //---NOTATION:
 //-  int CT_SetupDiEnumDeviceInterfaces(int memberIndex)
 //-
 //--- DESCRIPTION:
 //--  
 //                                                             Autor:      F.L.
 //-*************************************************************************+#*
 public unsafe int CT_SetupDiEnumDeviceInterfaces(int memberIndex)
 {
     mySP_DEVICE_INTERFACE_DATA = new SP_DEVICE_INTERFACE_DATA();
     mySP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(mySP_DEVICE_INTERFACE_DATA);
     int result = SetupDiEnumDeviceInterfaces(
         hDevInfo,
         0,
         ref  MYguid,
         memberIndex,
         ref mySP_DEVICE_INTERFACE_DATA);
     return result;
 }
开发者ID:Datenschredder,项目名称:MediaPortal-1,代码行数:20,代码来源:FutabaMDM166A.cs


示例10: SetupDiSetClassInstallParams

 protected static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet,
     ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_PROPCHANGE_PARAMS ClassInstallParams,
     int ClassInstallParamsSize);
开发者ID:CheesyKek,项目名称:ScpToolkit,代码行数:3,代码来源:ScpDevice.Interop.cs


示例11: SetupDiEnumDeviceInterfaces

 static extern unsafe int SetupDiEnumDeviceInterfaces(
     int DeviceInfoSet,
     int DeviceInfoData,
     ref  GUID lpHidGuid,
     int MemberIndex,
     ref  SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData);
开发者ID:Datenschredder,项目名称:MediaPortal-1,代码行数:6,代码来源:FutabaMDM166A.cs


示例12: findHidDevices

        /// <summary>
        /// Find all devices with the HID GUID attached to the system
        /// </summary>
        /// <remarks>This method searches for all devices that have the correct HID GUID and
        /// returns an array of matching device paths</remarks>
        private bool findHidDevices(ref String[] listOfDevicePathNames, ref int numberOfDevicesFound)
        {
            Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Method called");
            // Detach the device if it's currently attached
            if (isDeviceAttached) detachUsbDevice();

            // Initialise the internal variables required for performing the search
            Int32 bufferSize = 0;
            IntPtr detailDataBuffer = IntPtr.Zero;
            Boolean deviceFound;
            IntPtr deviceInfoSet = new System.IntPtr();
            Boolean lastDevice = false;
            Int32 listIndex = 0;
            SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            Boolean success;

            // Get the required GUID
            System.Guid systemHidGuid = new Guid();
            HidD_GetHidGuid(ref systemHidGuid);
            Debug.WriteLine(string.Format("usbGenericHidCommunication:findHidDevices() -> Fetched GUID for HID devices ({0})", systemHidGuid.ToString()));

            try
                {
                // Here we populate a list of plugged-in devices matching our class GUID (DIGCF_PRESENT specifies that the list
                // should only contain devices which are plugged in)
                Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Using SetupDiGetClassDevs to get all devices with the correct GUID");
                deviceInfoSet = SetupDiGetClassDevs(ref systemHidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

                // Reset the deviceFound flag and the memberIndex counter
                deviceFound = false;
                listIndex = 0;

                deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);

                // Look through the retrieved list of class GUIDs looking for a match on our interface GUID
                do
                    {
                    //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Enumerating devices");
                    success = SetupDiEnumDeviceInterfaces
                        (deviceInfoSet,
                        IntPtr.Zero,
                        ref systemHidGuid,
                        listIndex,
                        ref deviceInterfaceData);

                    if (!success)
                        {
                        //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> No more devices left - giving up");
                        lastDevice = true;
                        }
                    else
                        {
                        // The target device has been found, now we need to retrieve the device path so we can open
                        // the read and write handles required for USB communication

                        // First call is just to get the required buffer size for the real request
                        success = SetupDiGetDeviceInterfaceDetail
                            (deviceInfoSet,
                            ref deviceInterfaceData,
                            IntPtr.Zero,
                            0,
                            ref bufferSize,
                            IntPtr.Zero);

                        // Allocate some memory for the buffer
                        detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
                        Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

                        // Second call gets the detailed data buffer
                        //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Getting details of the device");
                        success = SetupDiGetDeviceInterfaceDetail
                            (deviceInfoSet,
                            ref deviceInterfaceData,
                            detailDataBuffer,
                            bufferSize,
                            ref bufferSize,
                            IntPtr.Zero);

                        // Skip over cbsize (4 bytes) to get the address of the devicePathName.
                        IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);

                        // Get the String containing the devicePathName.
                        listOfDevicePathNames[listIndex] = Marshal.PtrToStringAuto(pDevicePathName);

                        //Debug.WriteLine(string.Format("usbGenericHidCommunication:findHidDevices() -> Found matching device (memberIndex {0})", memberIndex));
                        deviceFound = true;
                        }
                    listIndex = listIndex + 1;
                    }
                while (!((lastDevice == true)));
                }
            catch (Exception)
                {
                // Something went badly wrong... output some debug and return false to indicated device discovery failure
                Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> EXCEPTION: Something went south whilst trying to get devices with matching GUIDs - giving up!");
//.........这里部分代码省略.........
开发者ID:hwchiu0810,项目名称:micro,代码行数:101,代码来源:deviceDiscovery.cs


示例13: GetDevicePath

        //Metodo de ayuda para encontrar la ruta al dispositivo que se busca, esta funcion es llamada desde el metodo "EncuentraDevHID"
        private string GetDevicePath(IntPtr Infoset, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
        {
            uint nRequiredSize = 0;
            //Para obtener la ruta del dispositivo se hace un proceso de dos pasos, llamar a SetupDiGetDeviceInterfaceDetail la primer vez para obtener el espacio a guardar en cSize
            //Llamar por segunda vez a la misma funcion para obtener la ruta del dispositivo.
            if (!SetupDiGetDeviceInterfaceDetail(Infoset, ref DeviceInterfaceData, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))//Primera llamada
            {
                SP_DEVICE_INTERFACE_DETAIL_DATA dDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA();

                if (IntPtr.Size == 8) // for 64 bit operating systems
                {
                    dDetail.cbSize = 8;
                }
                else
                {
                    dDetail.cbSize = 5; // for 32 bit systems
                }

                if (SetupDiGetDeviceInterfaceDetail(Infoset, ref DeviceInterfaceData, ref dDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero)) //Segunda llamada
                {
                    return dDetail.DevicePath;//Listo se encontro la ruta de algun dispositivo, falta ahora ver si coinciden VIP y PID
                }
                string error = Marshal.GetLastWin32Error().ToString();
            }
            return null; //No se encontro ningun otro dispositivo en la lista :(
        }
开发者ID:Leucemidus,项目名称:AccessB-Debug,代码行数:27,代码来源:AccessB_winusbapi_back.cs


示例14: SetupDiEnumDeviceInterfaces

 public static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDeviceInfoSet, IntPtr DeviceInfoData, ref System.Guid InterfaceClassGuid, int MemberIndex, out SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
开发者ID:redshiftrobotics,项目名称:telemetry_Archive,代码行数:1,代码来源:WIN32.cs


示例15: SetupDiChangeState

 protected static extern bool SetupDiChangeState(IntPtr DeviceInfoSet,
     ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
开发者ID:CheesyKek,项目名称:ScpToolkit,代码行数:2,代码来源:ScpDevice.Interop.cs


示例16: SetupDiEnumDeviceInterfaces

 public static extern int SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
开发者ID:claydonkey,项目名称:PicKitS,代码行数:1,代码来源:GenericHID.cs


示例17: SetupDiEnumDeviceInterfaces

 internal static extern bool SetupDiEnumDeviceInterfaces(
     IntPtr deviceInfoSet,
     SP_DEVINFO_DATA deviceInfoData,
     ref Guid interfaceClassGuid,
     int memberIndex,
     SP_DEVICE_INTERFACE_DATA deviceInterfaceData
     );
开发者ID:kencr,项目名称:HIDSimpleFramework,代码行数:7,代码来源:USBDevice.cs


示例18: FindDeviceFromGuid

        //--------------------------------------------------------------------------
        // Discovery
        //--------------------------------------------------------------------------

        public static List<KnownNXT> FindDeviceFromGuid(Guid guidDeviceInstance)
        // Find all connected instances of this kind of USB device
            {
            IntPtr hDeviceInfoSet = INVALID_HANDLE_VALUE;
            try
                {
                hDeviceInfoSet = SetupDiGetClassDevs(ref guidDeviceInstance, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                if (INVALID_HANDLE_VALUE==hDeviceInfoSet)
                    ThrowWin32Error();

                SP_DEVICE_INTERFACE_DATA did = new SP_DEVICE_INTERFACE_DATA();
                did.Initialize();

                List<KnownNXT> result = new List<KnownNXT>();

                for (int iMember=0 ;; iMember++)
                    {
                    // Get did of the next interface
                    bool fSuccess = SetupDiEnumDeviceInterfaces
                        (hDeviceInfoSet,
                        IntPtr.Zero,
                        ref guidDeviceInstance,
                        iMember,
                        out did);

                    if (!fSuccess)
                        {
                        break;  // Done! no more 
                        }
                    else
                        {
                        // A device is present. Get details
                        SP_DEVICE_INTERFACE_DETAIL_DATA detail = new SP_DEVICE_INTERFACE_DETAIL_DATA();
                        detail.Initialize();

                        int cbRequired;
                        ThrowIfFail(SetupDiGetDeviceInterfaceDetail
                            (hDeviceInfoSet,
                            ref did,
                            ref detail,
                            Marshal.SizeOf(detail),
                            out cbRequired,
                            IntPtr.Zero));

                        result.Add(new KnownNXT(KnownNXT.CONNECTIONTYPE.USB, detail.DevicePath));
                        }
                    }

                return result;
                }
            finally
                {
                if (hDeviceInfoSet != IntPtr.Zero && hDeviceInfoSet != INVALID_HANDLE_VALUE)
                    {
                    SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
                    }
                }
            }
开发者ID:redshiftrobotics,项目名称:telemetry_Archive,代码行数:62,代码来源:Connection.cs


示例19: DeviceNameMatch

        /* NKH
        ///  <summary>
        ///  Compares two device path names. Used to find out if the device name
        ///  of a recently attached or removed device matches the name of a
        ///  device the application is communicating with.
        ///  </summary>
        ///
        ///  <param name="m"> a WM_DEVICECHANGE message. A call to RegisterDeviceNotification
        ///  causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.. </param>
        ///  <param name="mydevicePathName"> a device pathname returned by
        ///  SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param>
        ///
        ///  <returns>
        ///  True if the names match, False if not.
        ///  </returns>
        ///
        internal Boolean DeviceNameMatch(Message m, String mydevicePathName)
        {
            Int32 stringSize;

            try
            {
                DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1();
                DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR();

                // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure.

                Marshal.PtrToStructure(m.LParam, devBroadcastHeader);

                if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE))
                {
                    // The dbch_devicetype parameter indicates that the event applies to a device interface.
                    // So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure,
                    // which begins with a DEV_BROADCAST_HDR.

                    // Obtain the number of characters in dbch_name by subtracting the 32 bytes
                    // in the strucutre that are not part of dbch_name and dividing by 2 because there are
                    // 2 bytes per character.

                    stringSize = System.Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2);

                    // The dbcc_name parameter of devBroadcastDeviceInterface contains the device name.
                    // Trim dbcc_name to match the size of the String.

                    devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1];

                    // Marshal data from the unmanaged block pointed to by m.LParam
                    // to the managed object devBroadcastDeviceInterface.

                    Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface);

                    // Store the device name in a String.

                    String DeviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize);

                    // Compare the name of the newly attached device with the name of the device
                    // the application is accessing (mydevicePathName).
                    // Set ignorecase True.

                    if ((String.Compare(DeviceNameString, mydevicePathName, true) == 0))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return false;
        }
         */
        ///  <summary>
        ///  Use SetupDi API functions to retrieve the device path name of an
        ///  attached device that belongs to a device interface class.
        ///  </summary>
        ///  
        ///  <param name="myGuid"> an interface class GUID. </param>
        ///  <param name="devicePathName"> a pointer to the device path name 
        ///  of an attached device. </param>
        ///  
        ///  <returns>
        ///   True if a device is found, False if not. 
        ///  </returns>
        internal Boolean FindDeviceFromGuid(System.Guid myGuid, ref String devicePathName)
        {
            Int32 bufferSize = 0;
            IntPtr detailDataBuffer = IntPtr.Zero;
            Boolean deviceFound;
            IntPtr deviceInfoSet = new System.IntPtr();
            Boolean lastDevice = false;
            Int32 memberIndex = 0;
            SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            Boolean success;

//.........这里部分代码省略.........
开发者ID:smo-key,项目名称:NXTLib,代码行数:101,代码来源:DeviceManagement.cs


示例20: FindDevice

        ///  <summary>
        ///  Use SetupDi API functions to retrieve the device path name of an attached device that belongs to a device interface class.
        ///  </summary>
        ///  
        ///  <param name="deviceGuid"> an interface class GUID. </param>
        ///  <param name="devicePathNames"> a pointer to an array of device path names of attached deviced. </param>
        ///  
        ///  <returns>
        ///   True if a devices are found, False if not. 
        ///  </returns>
        internal Boolean FindDevice(Guid deviceGuid, ref String[] devicePathNames)
        {
            IntPtr deviceInfoSet = IntPtr.Zero;
            Int32 memberIndex = 0;
            SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            Int32 bufferSize = 0;
            IntPtr detailDataBuffer = IntPtr.Zero;
            IntPtr pDevicePathName = IntPtr.Zero;
            Boolean devicesFound = false, moreDevices, success;

            try
            {
                // SetupDiGetClassDevs returns a pointer to an array of structures containing informations about all devices
                // in the device interface class specified by the Guid.
                deviceInfoSet = SetupDiGetClassDevs(ref deviceGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

                // Save the size of the structure.
                MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData);

                do
                {
                    // SetupDiEnumDeviceInterfaces takes the previous return SP_DEVINFO_DATA and looks up their members.
                    // We receive a SP_DEVICE_INTERFACE_DATA here.
                    moreDevices = SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref deviceGuid, memberIndex, ref MyDeviceInterfaceData);

                    if (moreDevices)
                    {
                        // SetupDiGetDeviceInterfaceDetail returns a structure with the device path name and its size as a SP_DEVICE_INTERFACE_DETAIL_DATA.
                        // Calling this function we don't know the size of the structure and the function won't return the structure unless we call it with the correct size.
                        // That's why we need to call the function twice.

                        // The first call  won't return the struture but an error. Still we can retrieve the size of the structure.
                        success = SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref MyDeviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero);

                        // Allocate the memory for SP_DEVICE_INTERFACE_DETAIL_DATA of the size of bufferSize.
                        detailDataBuffer = Marshal.AllocHGlobal(bufferSize);

                        // Copy the bufferSize into the start of the structure, size depends on 32 or 64-Bit OS.
                        Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

                        // The second call is now correct and a pointer to the structure is returned within detailDataBuffer.
                        success = SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref MyDeviceInterfaceData, detailDataBuffer, bufferSize, ref bufferSize, IntPtr.Zero);

                        // The first four bytes are the buffersize.
                        pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);

                        // Now make space for the next device and get the string with the device path.
                        Array.Resize(ref devicePathNames, devicePathNames.Length + 1);
                        devicePathNames[devicePathNames.Length - 1] = Marshal.PtrToStringAuto(pDevicePathName);

                        devicesFound = true;
                    }

                    memberIndex++;
                } while (moreDevices == true);

                return devicesFound;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (detailDataBuffer != IntPtr.Zero)
                    Marshal.FreeHGlobal(detailDataBuffer);

                if (deviceInfoSet != IntPtr.Zero)
                    SetupDiDestroyDeviceInfoList(deviceInfoSet);
            }
        }
开发者ID:mgcarmueja,项目名称:MPTCE,代码行数:82,代码来源:UsbDevice.cs



注:本文中的SP_DEVICE_INTERFACE_DATA类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# SP_DEVINFO_DATA类代码示例发布时间:2022-05-24
下一篇:
C# SPWebApplication类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap