Beispiel #1
0
        public unsafe static IntPtr OpenDevice(string devicePath, bool sync)
        {
            uint fileAttributes = UsbNativeApi.FILE_ATTRIBUTE_NORMAL;

            if (!sync)
            {
                fileAttributes |= UsbNativeApi.FILE_FLAG_OVERLAPPED;
            }

            return((IntPtr)UsbNativeApi.CreateFile(devicePath,
                                                   UsbNativeApi.GENERIC_WRITE | UsbNativeApi.GENERIC_READ,
                                                   UsbNativeApi.FILE_SHARE_WRITE | UsbNativeApi.FILE_SHARE_READ,
                                                   0, // default security
                                                   UsbNativeApi.OPEN_EXISTING,
                                                   fileAttributes,
                                                   0));
        }
Beispiel #2
0
        public unsafe static List <string> GetDevices(string identification, string classGuid)
        {
            string devicePath      = string.Empty;
            int    deviceNumber    = 0;
            int    hardwareDevInfo = 0;

            UsbNativeType.GUID guid;
            List <string>      devices = new List <string>();

            if (classGuid != null)
            {
                guid = ParseClassGuid(classGuid);
            }
            else
            {
                // falback to human interface devices GUID class
                guid = new UsbNativeType.GUID();
                UsbNativeApi.HidD_GetHidGuid(ref guid);
            }


            hardwareDevInfo = UsbNativeApi.SetupDiGetClassDevs(
                ref guid,
                null,
                null,
                UsbNativeApi.DIGCF_INTERFACEDEVICE | UsbNativeApi.DIGCF_PRESENT);

            // iterate through the available GUID interface devices
            while (true)
            {
                devicePath = GetDevicePath(guid, hardwareDevInfo, deviceNumber);
                if ((devicePath == null) || devicePath.Equals(String.Empty))
                {
                    UsbNativeApi.SetupDiDestroyDeviceInfoList(hardwareDevInfo);
                    return(devices);
                }
                // get 2Virt devices interfaces only
                if ((identification == null) || (devicePath.ToUpper().IndexOf(identification) > 0))
                {
                    devices.Add(devicePath);
                }
                deviceNumber++;
            }
        }
Beispiel #3
0
        public unsafe static IoStatus DeviceIoControl(IntPtr handle, IOCTLcommand command)
        {
            GCHandle hInput  = new GCHandle();
            GCHandle hOutput = new GCHandle();
            IoStatus status  = new IoStatus();

            status.buffer = new byte[command.outputMaxSize];
            status.size   = 0;
            status.error  = USBError.SUCCESS;

            try
            {
                // pin the buffers into place
                hInput  = GCHandle.Alloc(command.inputBuffer, GCHandleType.Pinned);
                hOutput = GCHandle.Alloc(status.buffer, GCHandleType.Pinned);

                if (UsbNativeApi.DeviceIoControl(
                        handle,
                        command.ioctlNo,
                        hInput.AddrOfPinnedObject(), (command.inputBuffer == null) ? 0 : (uint)command.inputBuffer.Length,
                        hOutput.AddrOfPinnedObject(), (uint)command.outputMaxSize,
                        ref status.size,
                        (IntPtr)0) != 0)
                {
                    status.error = USBError.WIN_API_SPECIFIC;
                }
            }
            finally
            {
                if (hInput.IsAllocated)
                {
                    hInput.Free();
                }
                if (hOutput.IsAllocated)
                {
                    hOutput.Free();
                }
            }

            return(status);
        }
Beispiel #4
0
        private unsafe static string GetDevicePath(UsbNativeType.GUID guid, int hardwareDevInfo, int device_number)
        {
            int result = 0;

            UsbNativeType.SP_DEVICE_INTERFACE_DATA interfaceData = new UsbNativeType.SP_DEVICE_INTERFACE_DATA();
            interfaceData.cbSize = Marshal.SizeOf(interfaceData);

            // get the device_number device interface
            result = UsbNativeApi.SetupDiEnumDeviceInterfaces(
                hardwareDevInfo,
                0,
                ref guid,
                device_number,
                ref interfaceData);

            // get the required size for the device interface system path
            int devicePathSize = 0;

            result = UsbNativeApi.SetupDiGetDeviceInterfaceDetail(
                hardwareDevInfo,    // IN HDEVINFO  DeviceInfoSet,
                ref interfaceData,  // IN PSP_DEVICE_INTERFACE_DATA  DeviceInterfaceData,
                null,               // DeviceInterfaceDetailData,  OPTIONAL
                0,                  // IN DWORD  DeviceInterfaceDetailDataSize,
                ref devicePathSize, // OUT PDWORD  RequiredSize,  OPTIONAL
                null);              //

            // get the actual device interface details
            UsbNativeType.PSP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetail = new UsbNativeType.PSP_DEVICE_INTERFACE_DETAIL_DATA();
            interfaceDetail.cbSize = 5; // sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA) == 5 in C
            result = UsbNativeApi.SetupDiGetDeviceInterfaceDetail(
                hardwareDevInfo,        // IN HDEVINFO  DeviceInfoSet,
                ref interfaceData,      // IN PSP_DEVICE_INTERFACE_DATA  DeviceInterfaceData,
                ref interfaceDetail,    // DeviceInterfaceDetailData,  OPTIONAL
                devicePathSize,         // IN DWORD  DeviceInterfaceDetailDataSize,
                ref devicePathSize,     // OUT PDWORD  RequiredSize,  OPTIONAL
                null);                  //

            return(interfaceDetail.DevicePath);
        }
Beispiel #5
0
 public unsafe static void CloseDevice(IntPtr handle)
 {
     UsbNativeApi.CloseHandle((int)handle);
 }