private void Button_OPEN_Click(object sender, EventArgs e) { for (var i = 0; i < CONNECTED_PRINTER_LIST.Count; i++) { if (CONNECTED_PRINTER_LIST[i].USB_SYMBOLIC_NAME.Equals(comboBox_Printer.Text)) { Selected_Printer = CONNECTED_PRINTER_LIST[i]; Selected_Printer.READ_TIMEOUT = 50; Selected_Printer.WRITE_TIMEOUT = 1000; if (Selected_Printer.OpenDevice()) { timer1.Enabled = true; button_Refresh.Enabled = false; button_Open.Enabled = false; comboBox_Printer.Enabled = false; button_closeport.Enabled = true; button_Send.Enabled = true; checkBox_printer.Enabled = false; checkBox_scanner.Enabled = false; //button_sendFile.Enabled = true; TextBox_fileName_TextChanged(this, EventArgs.Empty); } else { _logger.AddText("Port open failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } return; } } }
private void Enum_USB_device(ref List <CePrinter> Printer_USB_list) { IntPtr hDevInfoSet; SP_DEVINFO_DATA DeviceInfoData; var confRet = 0; //get all USB Symbolic links var Symb_Link = new List <string>(); Symb_Link = GetSymbolycLinkName(); //devs -> handle to device information set hDevInfoSet = SetupDiGetClassDevs(ref m_pGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); for (var i = 0;; i++) { //SetupDiEnumDeviceInterfaces enumerates the device interfaces that //are contained in a device information set var dia = new SP_DEVICE_INTERFACE_DATA(); dia.cbSize = Marshal.SizeOf(dia); if (!SetupDiEnumDeviceInterfaces(hDevInfoSet, IntPtr.Zero, ref m_pGuid, i, ref dia)) { break; } //SetupDiEnumDeviceInfo returns a SP_DEVINFO_DATA structure DeviceInfoData = new SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); if (!SetupDiEnumDeviceInfo(hDevInfoSet, i, ref DeviceInfoData)) { break; } uint len = 0; //CM_Get_Device_ID_Size returns size for CM_Get_Device_ID confRet = CM_Get_Device_ID_Size(ref len, DeviceInfoData.DevInst, 0); var t = new char[len]; //CM_Get_Device_ID returns crah[] with the registry key path of the device confRet = CM_Get_Device_ID(DeviceInfoData.DevInst, t, len, 0); if (confRet == CR_SUCCESS) { var str = ""; for (var f = 0; f < t.Length; f++) { str += t[f].ToString(); } var temp_dev = new CePrinter(); if (str.Contains("MI")) { var old_devInst = DeviceInfoData.DevInst; DeviceInfoData = new SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); if (!SetupDiEnumDeviceInfo(hDevInfoSet, i, ref DeviceInfoData)) { break; } //multi interface device //get parent device DeviceInfoData.DevInst = 0; confRet = CM_Get_Parent(out DeviceInfoData.DevInst, old_devInst, 0); confRet = CM_Get_Device_ID_Size(ref len, DeviceInfoData.DevInst, 0); t = new char[len]; confRet = CM_Get_Device_ID(DeviceInfoData.DevInst, t, len, 0); str = ""; for (var f = 0; f < t.Length; f++) { str += t[f].ToString(); } } //get USB ADDRESS NUMBER from str temp_dev.USB_ADDRESS_NUMBER = str[str.Length - 1].ToString(); //get PID from registry key path if (str.Contains("PID")) { for (var a = 0; a < str.Length - 6; a++) { try { if (str[a].ToString().ToUpper().Equals("P")) { if (str[a + 1].ToString().ToUpper().Equals("I")) { if (str[a + 2].ToString().ToUpper().Equals("D")) { temp_dev.PRINTER_PID = str[a + 4] + str[a + 5].ToString() + str[a + 6] + str[a + 7]; } } } } catch { } } } //assign USB symbolic link for (var q = 0; q < Symb_Link.Count; q++) { if (Symb_Link[q].ToUpper().Contains("PID_" + temp_dev.PRINTER_PID)) { //printer founded!!! temp_dev.USB_SYMBOLIC_NAME = Symb_Link[q]; Symb_Link[q] = ""; break; } } Printer_USB_list.Add(temp_dev); } } }