示例#1
0
        private IntPtr OpenDeviceHandle(Guid pGuid, BasicParm._PIPE_TYPE pipe_type, uint ui_deviceNum, bool buseAsyncIo)
        {
            IntPtr hDev;
            //g_bUseAsyncIo = buseAsyncIo;
            IntPtr hdinfo = GeneralAPI.SetupDiGetClassDevs(ref pGuid, 0, IntPtr.Zero, GeneralAPI.DIGCF_DEVICEINTERFACE | GeneralAPI.DIGCF_PRESENT);
            if (hdinfo.ToInt32() == GeneralAPI.INVALID_HANDLE_VALUE)
            {
                return IntPtr.Zero;
                //throw new Exception("Invalid Handle!");
            }

            GeneralAPI.SP_DEVINFO_DATA deviceInfoData = new GeneralAPI.SP_DEVINFO_DATA();
            //deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);//28=16+4+4+4
            //deviceInfoData.devInst = 0;
            //deviceInfoData.classGuid = System.Guid.Empty;
            //deviceInfoData.reserved = 0;

            GeneralAPI.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new GeneralAPI.SP_DEVICE_INTERFACE_DATA();
            deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);
            deviceInterfaceData.flags = 0;
            deviceInterfaceData.interfaceclassGuid = System.Guid.Empty;
            deviceInterfaceData.reserved = 0;

            // see if the device with the corresponding DeviceNumber is present
            //if(!GeneralAPI.SetupDiEnumDeviceInfo(hdinfo, 0, deviceInfoData))
            if (!GeneralAPI.SetupDiEnumDeviceInterfaces(hdinfo, IntPtr.Zero, ref pGuid, ui_deviceNum, ref deviceInterfaceData))
            {
                GeneralAPI.SetupDiDestroyDeviceInfoList(hdinfo);
                return IntPtr.Zero;
            }

            int dwDeviceSize = 0;
            bool result = GeneralAPI.SetupDiGetDeviceInterfaceDetail(hdinfo, ref deviceInterfaceData, IntPtr.Zero, 0, ref dwDeviceSize, deviceInfoData);
            dwDeviceSize += 32;
            IntPtr detailDataBuffer = Marshal.AllocHGlobal(dwDeviceSize);
            if (detailDataBuffer == IntPtr.Zero)
            {
                GeneralAPI.SetupDiDestroyDeviceInfoList(hdinfo);
                return IntPtr.Zero;
            }

            GeneralAPI.SP_INTERFACE_DEVICE_DETAIL_DATA detailData = new GeneralAPI.SP_INTERFACE_DEVICE_DETAIL_DATA();
            detailData.cbSize = Marshal.SizeOf(typeof(GeneralAPI.SP_INTERFACE_DEVICE_DETAIL_DATA));//GeneralAPI.SP_INTERFACE_DEVICE_DETAIL_DATA);

            //Marshal.WriteInt32(detailDataBuffer, 4);
            Marshal.StructureToPtr(detailData, detailDataBuffer, false);

            //int temp = 0;
            result = GeneralAPI.SetupDiGetDeviceInterfaceDetail(hdinfo, ref deviceInterfaceData, detailDataBuffer, dwDeviceSize, ref dwDeviceSize, deviceInfoData);

            if (!result)
            {
                GeneralAPI.SetupDiDestroyDeviceInfoList(hdinfo);
                Marshal.FreeHGlobal(detailDataBuffer);
                //MessageBox.Show("Get Device Interface Detail failed!");
                Console.WriteLine("Get Device Interface Detail failed!");
                return IntPtr.Zero;
            }

            IntPtr pdevicePathName = (IntPtr)((int)detailDataBuffer + 4);
            string devicePahtName = Marshal.PtrToStringAuto(pdevicePathName);
            Marshal.FreeHGlobal(detailDataBuffer);
            GeneralAPI.SetupDiDestroyDeviceInfoList(hdinfo);

            //string test = "\\\\?\\usb#vid_064b&pid_1212#6&2ed19cf&0&3#{eb8322c5-8b49-4feb-ae6e-c99b2b232045}";

            if (BasicParm._PIPE_TYPE.WRITE_PIPE == pipe_type)
            {
                if (buseAsyncIo)
                {
                    g_hWriteEvent = GeneralAPI.CreateEvent(IntPtr.Zero, true, true, null);
                    if (!GeneralAPI.ResetEvent(g_hWriteEvent))
                    {
                        //MessageBox.Show("Reset write Event failed!!");
                        Console.WriteLine("Reset write Event failed!!");
                    }
                }
                devicePahtName += "\\PIPE_0x01";

            }
            else if (BasicParm._PIPE_TYPE.READ_PIPE == pipe_type)
            {
                if (buseAsyncIo)
                {
                    g_hReadEvent = GeneralAPI.CreateEvent(IntPtr.Zero, true, true, null);
                    if (!GeneralAPI.ResetEvent(g_hReadEvent))
                    {
                        //MessageBox.Show("Reset Event failed!!!");
                        Console.WriteLine("Reset Event failed!!!");
                    }
                }
                devicePahtName += "\\PIPE_0x00";
            }
            else
            {
                return IntPtr.Zero;
            }

            // determine the attributes
            uint dwAttribs;
            if (buseAsyncIo)
                dwAttribs = BasicParm.FILE_FLAG_OVERLAPPED;
            else
                dwAttribs = BasicParm.FILE_ATTRIBUTE_NORMAL;
            hDev = GeneralAPI.CreateFile(
                        devicePahtName,
                        (uint)(BasicParm.GENERIC_READ | BasicParm.GENERIC_WRITE),
                        (uint)(BasicParm.FILE_SHARE_READ | BasicParm.FILE_SHARE_WRITE),
                        BasicParm.NULL,
                        BasicParm.OPEN_EXISTING,
                        dwAttribs,
                        BasicParm.NULL);
            return hDev;
        }
示例#2
0
        internal int QueryNumDevices(ref Guid guid)
        {
            int nDevices = 0;
            IntPtr hDevInfo = GeneralAPI.SetupDiGetClassDevs(ref guid,
                0, IntPtr.Zero, GeneralAPI.DIGCF_DEVICEINTERFACE | GeneralAPI.DIGCF_PRESENT);
            //IntPtr hDevInfo = GeneralAPI.SetupDiGetClassDevs(ref pguid,
            //   0, IntPtr.Zero, GeneralAPI.DIGCF_PRESENT);

            if (hDevInfo.ToInt32() == GeneralAPI.INVALID_HANDLE_VALUE)
            {
                throw new Exception("Invalid Handle!");
            }

            GeneralAPI.SP_DEVINFO_DATA deviceInfoData = new GeneralAPI.SP_DEVINFO_DATA();
            //deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);//28=16+4+4+4
            //deviceInfoData.devInst = 0;
            //deviceInfoData.classGuid = System.Guid.Empty;  //System.Guid.Empty;
            //deviceInfoData.reserved = 0;

            GeneralAPI.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new GeneralAPI.SP_DEVICE_INTERFACE_DATA();
            deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);
            deviceInterfaceData.flags = 0;
            deviceInterfaceData.interfaceclassGuid = System.Guid.Empty;
            deviceInterfaceData.reserved = 0;

            //GeneralAPI.HidD_GetHidGuid(ref guid);

            for (nDevices = 0; ; nDevices++)
            {
                //if(!GeneralAPI.SetupDiEnumDeviceInfo(hDevInfo, (uint)nDevices,deviceInfoData))
                if (!GeneralAPI.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, (UInt32)nDevices, ref deviceInterfaceData))
                {
                    int errorNo = Marshal.GetLastWin32Error();
                    if (errorNo == GeneralAPI.WinError.ERROR_NO_MORE_ITEMS)
                    {
                        break;
                    }
                    else
                    {
                        GeneralAPI.SetupDiDestroyDeviceInfoList(hDevInfo);
                        return -1;
                    }
                }
            }

            GeneralAPI.SetupDiDestroyDeviceInfoList(hDevInfo);

            //g_lTotalDevices = nDevices;
            return nDevices;
        }