المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : USB device detection upon program startup



C# Programming
01-28-2011, 09:31 AM
I have recently posted about the AccessMemoryViolation issue on my WndProc when the usb device is being turned on (or hooked up). This part is solved and runs good.

Now the other thing is that I wonder if there is a way to send the message to WndProc during the startup (WM_CREATE) with the following Parameters:

Message: WM_DEVICECHANGE
WParam: DBT_DEVICEARRIVAL
LParam: DBT_DEVTYP_DEVICEINTERFACE

Code snippet:
----

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

using HID_API_Library;

namespace USB_Box_Application
{
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(int hWnd, int msg, int wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);

int[,] deviceInfo = new int[1,2];
Device_Controller dc = new Device_Controller();
DeviceControl[] deviceControl = new DeviceControl[4];
IntPtr[] hwndProcess = new IntPtr[4];

public Point[] PanelLoc = new Point[4];

public Form1()
{
InitializeComponent();
dc.ParentHandle = this.Handle;
//dc.FindUsbHid();
dc.RegisterHidNotification(this.Handle);
DisplayPanels();
this.label2.Text = this.Handle.ToInt32().ToString();
//PostMessage(this.Handle.ToInt32(), HID_API.WM_DEVICECHANGE, HID_API.DBT_DEVICEARRIVAL, (IntPtr)HID_API.DBT_DEVTYP_DEVICEINTERFACE);
SendMessage(this.Handle.ToInt32(), HID_API.WM_DEVICECHANGE, HID_API.DBT_DEVICEARRIVAL, (IntPtr)HID_API.DBT_DEVTYP_DEVICEINTERFACE);
}

private void DisplayPanels()
{
PanelLoc[0] = new Point(0, 0);
PanelLoc[1] = new Point(205, 0);
PanelLoc[2] = new Point(0, 105);
PanelLoc[3] = new Point(205, 105);

for (int i = 0; i < 4; i++)
{
if (deviceControl[i] == null)
deviceControl[i] = new DeviceControl();

deviceControl[i].******** = PanelLoc[i];

try
{
deviceControl[i].DeviceHandle = dc.cbInfos[i].usbHandle;
deviceControl[i].DeviceBoxId = dc.cbInfos[i].boxID;
deviceControl[i].DeviceProductId = dc.cbInfos[i].productId;
deviceControl[i].DeviceVendorId = dc.cbInfos[i].vendorId;
deviceControl[i].DeviceConnected = true;
}
catch
{
deviceControl[i].DeviceHandle = -1;
deviceControl[i].DeviceBoxId = -1;
deviceControl[i].DeviceProductId = -1;
deviceControl[i].DeviceVendorId = -1;
deviceControl[i].DeviceConnected = false;
}
}

this.Controls.AddRange(deviceControl);

}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0001: // Windows being created (similiar to form.load event)
{
dc.RegisterHidNotification(this.Handle); // registering usb device notifications to parent handle.
SendMessage(this.Handle.ToInt32(), HID_API.WM_DEVICECHANGE, HID_API.DBT_DEVICEARRIVAL, m.LParam);
break;
}

case HID_API.WM_DEVICECHANGE:
{
switch (m.WParam.ToInt32())
{
case HID_API.DBT_DEVICEARRIVAL:
{
OnDeviceChange(m);
break;
}
case HID_API.DBT_DEVICEREMOVECOMPLETE:
{
RemoveDevice(ref m);
break;
}
}
break;
}
}
base.WndProc(ref m);
}

void OnDeviceChange(Message m)
{
switch (m.WParam.ToInt32())
{
case HID_API.DBT_DEVICEARRIVAL:
{
DEV_BROADCAST_HDR hdr = (DEV_BROADCAST_HDR)m.GetLParam(typeof(DEV_BROADCAST_HDR));
if (hdr.dbch_devicetype == HID_API.DBT_DEVTYP_DEVICEINTERFACE)
{
DEV_BROADCAST_DEVICEINTERFACE1 dbd = (DEV_BROADCAST_DEVICEINTERFACE1)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE1));
string usbDevice = new string(dbd.dbcc_name);

if (usbDevice.Contains("Vid_1238&Pid_5000"))
label2.Text = "USB Box Found!";
if (usbDevice.Contains("Vid_0483&Pid_0003"))
label2.Text = "Clear Box Found!";

SECURITY_ATTRIBUTES securityAttrib = new SECURITY_ATTRIBUTES();

int usbHandle = HID_API.CreateFile(usbDevice, HID_API.GENERIC_READ,
HID_API.FILE_SHARE_READ | HID_API.FILE_SHARE_WRITE,
ref securityAttrib,
HID_API.OPEN_EXISTING,
HID_API.FILE_FLAG_OVERLAPPED, 0);

if (usbDevice.Contains("Vid_1238&Pid_5000"))
MessageBox.Show(string.Format("{0}: {1} {2}", "USB Box Found", "Handle =", usbHandle));
if (usbDevice.Contains("Vid_0483&Pid_0003"))
MessageBox.Show(string.Format("{0}: {1} {2}", "Clear Box Found", "Handle =", usbHandle));
}
break;
}
}
}