Click here to Skip to main content
15,889,909 members
Home / Discussions / C#
   

C#

 
GeneralRe: Breaking Up Classes Pin
inayathussaintoori27-Jan-11 14:04
inayathussaintoori27-Jan-11 14:04 
GeneralRe: Breaking Up Classes Pin
musefan28-Jan-11 5:58
musefan28-Jan-11 5:58 
GeneralRe: Breaking Up Classes Pin
Luc Pattyn28-Jan-11 6:14
sitebuilderLuc Pattyn28-Jan-11 6:14 
AnswerRe: Breaking Up Classes Pin
PIEBALDconsult27-Jan-11 14:09
mvePIEBALDconsult27-Jan-11 14:09 
AnswerRead this ebook from los techies about the SOLID principles Pin
i.j.russell28-Jan-11 1:36
i.j.russell28-Jan-11 1:36 
AnswerRe: Breaking Up Classes Pin
Alan Balkany28-Jan-11 3:07
Alan Balkany28-Jan-11 3:07 
AnswerRe: Breaking Up Classes Pin
fjdiewornncalwe28-Jan-11 4:12
professionalfjdiewornncalwe28-Jan-11 4:12 
QuestionUSB device detection upon program startup Pin
Blubbo27-Jan-11 9:51
Blubbo27-Jan-11 9:51 
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:
----

C#
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].Location = 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;
          }
      }
    }

AnswerRe: USB device detection upon program startup Pin
Luc Pattyn27-Jan-11 11:02
sitebuilderLuc Pattyn27-Jan-11 11:02 
GeneralRe: USB device detection upon program startup Pin
Blubbo27-Jan-11 16:46
Blubbo27-Jan-11 16:46 
AnswerRe: USB device detection upon program startup Pin
Luc Pattyn27-Jan-11 17:25
sitebuilderLuc Pattyn27-Jan-11 17:25 
Questionproblem with dynamic ips when using sockets. Pin
prasadbuddhika27-Jan-11 6:25
prasadbuddhika27-Jan-11 6:25 
AnswerRe: problem with dynamic ips when using sockets. Pin
Yusuf27-Jan-11 6:30
Yusuf27-Jan-11 6:30 
AnswerRe: problem with dynamic ips when using sockets. Pin
Richard MacCutchan27-Jan-11 7:15
mveRichard MacCutchan27-Jan-11 7:15 
GeneralRe: problem with dynamic ips when using sockets. Pin
prasadbuddhika27-Jan-11 19:23
prasadbuddhika27-Jan-11 19:23 
GeneralRe: problem with dynamic ips when using sockets. Pin
Richard MacCutchan27-Jan-11 22:45
mveRichard MacCutchan27-Jan-11 22:45 
AnswerRe: problem with dynamic ips when using sockets. Pin
jschell27-Jan-11 8:37
jschell27-Jan-11 8:37 
QuestionEquivalent way to achieve const or readonly return type from method Pin
bob1697227-Jan-11 5:09
bob1697227-Jan-11 5:09 
AnswerRe: Equivalent way to achieve const or readonly return type from method Pin
_Erik_27-Jan-11 5:33
_Erik_27-Jan-11 5:33 
AnswerRe: Equivalent way to achieve const or readonly return type from method Pin
Luc Pattyn27-Jan-11 5:36
sitebuilderLuc Pattyn27-Jan-11 5:36 
AnswerRe: Equivalent way to achieve const or readonly return type from method PinPopular
jschell27-Jan-11 8:45
jschell27-Jan-11 8:45 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
fjdiewornncalwe27-Jan-11 8:49
professionalfjdiewornncalwe27-Jan-11 8:49 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
bob1697227-Jan-11 8:56
bob1697227-Jan-11 8:56 
AnswerRe: Equivalent way to achieve const or readonly return type from method Pin
RobCroll27-Jan-11 12:06
RobCroll27-Jan-11 12:06 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
bob1697227-Jan-11 18:35
bob1697227-Jan-11 18:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.