/// <summary> /// Fires the barcode scanned event. /// </summary> /// <param name="deviceInfo">information about the device that generated /// the barcode</param> private void FireBarcodeScanned(BarcodeScannerDeviceInfo deviceInfo) { string barcode; EventHandler handler; barcode = this.keystrokeBuffer.ToString(); handler = this.BarcodeScanned; this.keystrokeBuffer = new StringBuilder(); if (handler != null) { handler(this, new BarcodeScannedEventArgs(barcode, deviceInfo)); } }
/// <summary> /// Initializes a new instance of the BarcodeScannedEventArgs class. /// </summary> /// <param name="barcode">the barcode that was scanned</param> /// <param name="deviceInfo">information about the device that sent the /// barcode</param> /// <exception cref="ArgumentNullException">if the barcode or deviceInfo are /// null</exception> public BarcodeScannedEventArgs(string barcode, BarcodeScannerDeviceInfo deviceInfo) { if (barcode == null) { throw new ArgumentNullException("barcode"); } if (deviceInfo == null) { throw new ArgumentNullException("deviceInfo"); } this.Barcode = barcode; this.DeviceInfo = deviceInfo; }
/// <summary> /// Enumerates devices provided by GetRawInputDeviceList. We'll only listen /// to these devices. /// </summary> /// <exception cref="ConfigurationErrorsException">if an error occurs /// during configuration</exception> private void InitializeBarcodeScannerDeviceHandles() { BarcodeScannerListenerConfigurationSection config; BarcodeScannerListenerConfigurationElementCollection hardwareIdsConfig; List <string> hardwareIds; uint numDevices; uint size; config = BarcodeScannerListenerConfigurationSection.GetConfiguration(); hardwareIdsConfig = config.HardwareIds; hardwareIds = new List <string>(); numDevices = 0; size = (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST)); foreach (BarcodeScannerListenerConfigurationElement hardwareId in hardwareIdsConfig) { hardwareIds.Add(hardwareId.Id); } // First, we get the number of raw input devices in the list by passing // in IntPtr.Zero. Then we allocate sufficient memory and retrieve the // entire list. if (NativeMethods.GetRawInputDeviceList(IntPtr.Zero, ref numDevices, size) == 0) { IntPtr rawInputDeviceList; rawInputDeviceList = Marshal.AllocHGlobal((int)(size * numDevices)); if (NativeMethods.GetRawInputDeviceList( rawInputDeviceList, ref numDevices, size) != uint.MaxValue) { // Next, we iterate through the list, discarding undesired items // and retrieving further information on the barcode scanner devices for (int i = 0; i < numDevices; ++i) { uint pcbSize; NativeMethods.RAWINPUTDEVICELIST rid; pcbSize = 0; rid = (NativeMethods.RAWINPUTDEVICELIST)Marshal.PtrToStructure( new IntPtr((rawInputDeviceList.ToInt32() + (size * i))), typeof(NativeMethods.RAWINPUTDEVICELIST)); if (NativeMethods.GetRawInputDeviceInfo( rid.hDevice, NativeMethods.RawInputDeviceInfoCommand.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize) >= 0) { if (pcbSize > 0) { string deviceName; string friendlyName; BarcodeScannerDeviceInfo info; IntPtr data; data = Marshal.AllocHGlobal((int)pcbSize); if (NativeMethods.GetRawInputDeviceInfo( rid.hDevice, NativeMethods.RawInputDeviceInfoCommand.RIDI_DEVICENAME, data, ref pcbSize) >= 0) { deviceName = (string)Marshal.PtrToStringAnsi(data); if ((from hardwareId in hardwareIds where deviceName.Contains(hardwareId) select hardwareId).Count() > 0) { friendlyName = GetDeviceFriendlyName(deviceName); info = new BarcodeScannerDeviceInfo( deviceName, GetBarcodeScannerDeviceType(rid.dwType), rid.hDevice, friendlyName); this.devices.Add(rid.hDevice, info); } } Marshal.FreeHGlobal(data); } } } } Marshal.FreeHGlobal(rawInputDeviceList); } }