private void Form1_Load(object sender, EventArgs e) { Guid hidGuid = Guid.Empty; Hid.HidD_GetHidGuid(ref hidGuid); IntPtr deviceNotificationHandle; deviceNotificationHandle = IntPtr.Zero; RegisterForDeviceNotifications("", this.Handle, hidGuid, ref deviceNotificationHandle); }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_DEVICECHANGE) { if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL)) { //// Look for a colorimeter if one isn't already attached if (colorimeter == null) { Guid colorimeterGuid = Guid.Empty; Hid.HidD_GetHidGuid(ref colorimeterGuid); String[] devicePathNames = MyDeviceManagement.FindDevicePathsFromGuid(colorimeterGuid, NUMBER_OF_DEVICE_PATHNAMES); var devicePath = Hid.FindDevicePath(Colorimeter.COLORIMETER_PRODUCT_ID, Colorimeter.COLORIMETER_VENDOR_ID, devicePathNames); var readHandle = FileIO.CreateFile(devicePath, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0); //we have found a viable colorimeter with a working path and handle if (!devicePath.Equals(null) && !readHandle.IsInvalid) { //colorimeterDetected = true; colorimeter = new Colorimeter(readHandle, devicePath); // Start checkColorimeterDeviceStateBackgroundWorker // When the Background Worker completes, a different backgroundworker // is called to get the firmware and test file versions var request = new ColorimeterRequest() { ColorimeterRequestActionType = ColorimeterActionType.FirmwareVersion | ColorimeterActionType.TestFileVersion | ColorimeterActionType.DeviceState }; if (!colorimeterConnectedBackgroundWorker.IsBusy) { colorimeterConnectedBackgroundWorker.RunWorkerAsync(request); } } } //// Find out if it's the device we're communicating with. if (MyDeviceManagement.DeviceNameMatch(m, colorimeter.colorimeterPathName)) { listBox1.Items.Add("Colorimeter attached."); } } // If WParam contains DBT_DEVICEREMOVAL, a device has been removed. We only care if the removed device is a colorimeter we are already talking to else if ((m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE) && colorimeter != null) { if (MyDeviceManagement.DeviceNameMatch(m, colorimeter.colorimeterPathName)) { listBox1.Items.Add("Colorimeter removed."); //colorimeterDetected = false; colorimeter = null; } } listBox1.SelectedIndex = listBox1.Items.Count - 1; } }