Beispiel #1
0
        private static APIs.DeviceInterfaceDetailData GetDeviceInfo(IntPtr hDevInfoSet, Guid deviceGUID, out string deviceID, int devicePortIndex)
        {
            // Get a Device Interface Data structure.
            // --------------------------------------

            APIs.DeviceInterfaceData interfaceData = new APIs.DeviceInterfaceData();
            APIs.CRErrorCodes        crResult;
            IntPtr startingDevice;

            APIs.DevinfoData infoData = new APIs.DevinfoData();
            APIs.DeviceInterfaceDetailData detailData = new APIs.DeviceInterfaceDetailData();

            bool   result = true;
            IntPtr ptrInstanceBuf;
            uint   requiredSize = 0;

            interfaceData.Init();
            infoData.Size   = Marshal.SizeOf(typeof(APIs.DevinfoData));
            detailData.Size = Marshal.SizeOf(typeof(APIs.DeviceInterfaceDetailData));

            result = APIs.SetupDiEnumDeviceInterfaces(
                hDevInfoSet,
                0,
                ref deviceGUID,
                (uint)devicePortIndex,
                ref interfaceData);

            if (!result)
            {
                if (Marshal.GetLastWin32Error() == APIs.ERROR_NO_MORE_ITEMS)
                {
                    deviceID = string.Empty;
                    return(new APIs.DeviceInterfaceDetailData());
                }
                else
                {
                    throw new ApplicationException("[CCore::GetDeviceInfo] Error when retriving device info");
                }
            }


            //Get a DevInfoDetailData and DeviceInfoData
            // ------------------------------------------

            // First call to get the required size.
            result = APIs.SetupDiGetDeviceInterfaceDetail(
                hDevInfoSet,
                ref interfaceData,
                IntPtr.Zero,
                0,
                ref requiredSize,
                ref infoData);

            // Create the buffer.
            detailData.Size = 5;

            // Call with the correct buffer
            result = APIs.SetupDiGetDeviceInterfaceDetail(
                hDevInfoSet,
                ref interfaceData,
                ref detailData, // ref devDetailBuffer,
                requiredSize,
                ref requiredSize,
                ref infoData);

            if (!result)
            {
                System.Windows.Forms.MessageBox.Show(Marshal.GetLastWin32Error().ToString());
                throw new ApplicationException("[CCore::GetDeviceInfo] Can not get SetupDiGetDeviceInterfaceDetail");
            }

            startingDevice = infoData.DevInst;

            //Get current device data
            ptrInstanceBuf = Marshal.AllocHGlobal(1024);
            crResult       = (APIs.CRErrorCodes)APIs.CM_Get_Device_ID(startingDevice, ptrInstanceBuf, 1024, 0);

            if (crResult != APIs.CRErrorCodes.CR_SUCCESS)
            {
                throw new ApplicationException("[CCore::GetDeviceInfo] Error calling CM_Get_Device_ID: " + crResult);
            }

            // We have the pnp string of the parent device.
            deviceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

            Marshal.FreeHGlobal(ptrInstanceBuf);
            return(detailData);
        }
        private static APIs.DeviceInterfaceDetailData GetDeviceInfo(IntPtr hDevInfoSet, Guid deviceGUID, out string deviceID, int devicePortIndex)
        {
            // Get a Device Interface Data structure.
              // --------------------------------------

              APIs.DeviceInterfaceData interfaceData = new APIs.DeviceInterfaceData();
              APIs.CRErrorCodes crResult;
              IntPtr startingDevice;
              APIs.DevinfoData infoData = new APIs.DevinfoData();
              APIs.DeviceInterfaceDetailData detailData = new APIs.DeviceInterfaceDetailData();

              bool result = true;
              IntPtr ptrInstanceBuf;
              uint requiredSize = 0;

              interfaceData.Init();
              infoData.Size = Marshal.SizeOf(typeof(APIs.DevinfoData));
              detailData.Size = Marshal.SizeOf(typeof(APIs.DeviceInterfaceDetailData));

              result = APIs.SetupDiEnumDeviceInterfaces(
              hDevInfoSet,
              0,
              ref deviceGUID,
              (uint)devicePortIndex,
              ref interfaceData);

              if (!result)
              {
            if (Marshal.GetLastWin32Error() == APIs.ERROR_NO_MORE_ITEMS)
            {
              deviceID = string.Empty;
              return new APIs.DeviceInterfaceDetailData();
            }
            else
            {
              throw new ApplicationException("[CCore::GetDeviceInfo] Error when retriving device info");
            }
              }

              //Get a DevInfoDetailData and DeviceInfoData
              // ------------------------------------------

              // First call to get the required size.
              result = APIs.SetupDiGetDeviceInterfaceDetail(
              hDevInfoSet,
              ref interfaceData,
              IntPtr.Zero,
              0,
              ref requiredSize,
              ref infoData);

              // Create the buffer.
              detailData.Size = 5;

              // Call with the correct buffer
              result = APIs.SetupDiGetDeviceInterfaceDetail(
            hDevInfoSet,
            ref interfaceData,
            ref detailData, // ref devDetailBuffer,
            requiredSize,
            ref requiredSize,
            ref infoData);

              if (!result)
              {
            System.Windows.Forms.MessageBox.Show(Marshal.GetLastWin32Error().ToString());
            throw new ApplicationException("[CCore::GetDeviceInfo] Can not get SetupDiGetDeviceInterfaceDetail");
              }

              startingDevice = infoData.DevInst;

              //Get current device data
              ptrInstanceBuf = Marshal.AllocHGlobal(1024);
              crResult = (APIs.CRErrorCodes)APIs.CM_Get_Device_ID(startingDevice, ptrInstanceBuf, 1024, 0);

              if (crResult != APIs.CRErrorCodes.CR_SUCCESS)
            throw new ApplicationException("[CCore::GetDeviceInfo] Error calling CM_Get_Device_ID: " + crResult);

              // We have the pnp string of the parent device.
              deviceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

              Marshal.FreeHGlobal(ptrInstanceBuf);
              return detailData;
        }