Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
RobCroll27-Jan-11 18:55
RobCroll27-Jan-11 18:55 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
Bernhard Hiller28-Jan-11 1:24
Bernhard Hiller28-Jan-11 1:24 
GeneralRe: Equivalent way to achieve const or readonly return type from method Pin
bob1697228-Jan-11 3:08
bob1697228-Jan-11 3:08 
AnswerRe: Equivalent way to achieve const or readonly return type from method Pin
TheGreatAndPowerfulOz28-Jan-11 7:38
TheGreatAndPowerfulOz28-Jan-11 7:38 
QuestionAccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program [modified] Pin
Blubbo27-Jan-11 2:24
Blubbo27-Jan-11 2:24 
I want to either postmessage or sendmessage to tell the software to acquire the USB devices upon startup. I have been getting this "AccessviolationException was unhandled" error during debugging. (Attempting to read or write protected memory. This is often an indcation that other memory is corrupted.

The line where the error dialog box showed pointed to this line after "case HID_API.DBT_DEVICEARRIVAL":
int devType = Marshal.ReadInt32(m.LParam, 4);

Can anyone help me out? How do I fix this?

Code snippet below:

----

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.RegisterHidNotification(this.Handle);
       SendMessage(this.Handle.ToInt32(), HID_API.WM_DEVICECHANGE, HID_API.DBT_DEVICEARRIVAL, (IntPtr)HID_API.DBT_DEVTYP_DEVICEINTERFACE);
     }

      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.
               break;
            }

            case HID_API.WM_DEVICECHANGE:
            {
               switch (m.WParam.ToInt32())
                {
                   case HID_API.DBT_DEVICEARRIVAL:
                   {
                       //dc.FindUsbHid();
                      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:
             {
               int devType = Marshal.ReadInt32(m.LParam, 4);

               if (devType == HID_API.DBT_DEVTYP_DEVICEINTERFACE)
               {
                  DEV_BROADCAST_DEVICEINTERFACE1 devInterface = (DEV_BROADCAST_DEVICEINTERFACE1)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE1));
                  string deviceInterface = new string (devInterface.dbcc_name);

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

                  SECURITY_ATTRIBUTES securityAttrib = new SECURITY_ATTRIBUTES();
                  int usbHandle = HID_API.CreateFile(deviceInterface, 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 (deviceInterface.Contains("Vid_1238&Pid_5000"))
                    MessageBox.Show(string.Format("{0}: {1} {2}", "USB Box Found", "Handle =", usbHandle));
                  if (deviceInterface.Contains("Vid_0483&Pid_0003"))
                    MessageBox.Show(string.Format("{0}: {1} {2}", "Clear Box Found", "Handle =", usbHandle));
                }
                break;
             }
        }
    }



-- Modified Thursday, January 27, 2011 8:55 AM

AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
RaviRanjanKr27-Jan-11 2:42
professionalRaviRanjanKr27-Jan-11 2:42 
GeneralRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Blubbo27-Jan-11 2:53
Blubbo27-Jan-11 2:53 
AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Luc Pattyn27-Jan-11 3:00
sitebuilderLuc Pattyn27-Jan-11 3:00 
GeneralRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Blubbo27-Jan-11 2:58
Blubbo27-Jan-11 2:58 
AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Luc Pattyn27-Jan-11 3:08
sitebuilderLuc Pattyn27-Jan-11 3:08 
GeneralRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Blubbo27-Jan-11 3:24
Blubbo27-Jan-11 3:24 
AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Luc Pattyn27-Jan-11 3:39
sitebuilderLuc Pattyn27-Jan-11 3:39 
AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Alan N27-Jan-11 3:20
Alan N27-Jan-11 3:20 
GeneralRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Blubbo27-Jan-11 6:43
Blubbo27-Jan-11 6:43 
GeneralRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
Blubbo27-Jan-11 6:57
Blubbo27-Jan-11 6:57 
AnswerRe: AccessviolationException was unhandled when doing SendMessage to get DeviceArrival message upon startup of program Pin
_Erik_27-Jan-11 5:39
_Erik_27-Jan-11 5:39 
QuestionZoom in on a label without changing the font size [modified] Pin
Lee Reid27-Jan-11 0:57
Lee Reid27-Jan-11 0:57 
AnswerRe: Zoom in on a label without changing the font size Pin
Henry Minute27-Jan-11 4:09
Henry Minute27-Jan-11 4:09 
AnswerRe: Zoom in on a label without changing the font size Pin
dojohansen27-Jan-11 22:00
dojohansen27-Jan-11 22:00 
QuestionDownloading file (with out extension) from Ftp throwing error Pin
arun_pk27-Jan-11 0:24
arun_pk27-Jan-11 0:24 
AnswerRe: Downloading file (with out extension) from Ftp throwing error Pin
Pete O'Hanlon27-Jan-11 1:30
mvePete O'Hanlon27-Jan-11 1:30 
AnswerRe: Downloading file (with out extension) from Ftp throwing error Pin
Ravi Sant27-Jan-11 2:40
Ravi Sant27-Jan-11 2:40 

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.