how to trigger event when a com device is connected in c#
C#
/*
Headers to be used:-
using System;
using System.Runtime.InteropServices;
*/
public Form1()
{
InitializeComponent();
UsbNotification.RegisterUsbDeviceNotification(this.Handle);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == UsbNotification.WmDevicechange)
{
switch ((int)m.WParam)
{
case UsbNotification.DbtDeviceremovecomplete:
Usb_DeviceRemoved(); // this is where you do your magic
break;
case UsbNotification.DbtDevicearrival:
Usb_DeviceAdded(); // this is where you do your magic
break;
}
}
}
internal static class UsbNotification
{
public const int DbtDevicearrival = 0x8000; // system detected a new device
public const int DbtDeviceremovecomplete = 0x8004; // device is gone
public const int WmDevicechange = 0x0219; // device change event
private const int DbtDevtypDeviceinterface = 5;
private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices
private static IntPtr notificationHandle;
/// <summary>
/// Registers a window to receive notifications when USB devices are plugged or unplugged.
/// </summary>
/// <param name="windowHandle">Handle to the window receiving notifications.</param>
public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
{
DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
{
DeviceType = DbtDevtypDeviceinterface,
Reserved = 0,
ClassGuid = GuidDevinterfaceUSBDevice,
Name = 0
};
dbi.Size = Marshal.SizeOf(dbi);
IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
Marshal.StructureToPtr(dbi, buffer, true);
notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
}
/// <summary>
/// Unregisters the window for USB device notifications
/// </summary>
public static void UnregisterUsbDeviceNotification()
{
UnregisterDeviceNotification(notificationHandle);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);
[DllImport("user32.dll")]
private static extern bool UnregisterDeviceNotification(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct DevBroadcastDeviceinterface
{
internal int Size;
internal int DeviceType;
internal int Reserved;
internal Guid ClassGuid;
internal short Name;
}
}
Also in C#:
- Title
- c# throw new exception
- Category
- C#
- Title
- c# random string
- Category
- C#
- Title
- c# summary tag
- Category
- C#
- Title
- c# string remove special characters
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- unity remove gameobject
- Category
- C#
- Title
- uncaught syntaxerror invalid or unexpected token unity webgl
- Category
- C#
- Title
- c# dictionary add
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- c# bool list count true
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- c# inline if
- Category
- C#
- Title
- how to hide a panel ui unity
- Category
- C#
- Title
- isInstanceOf nunit
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- c# getter setter
- Category
- C#
- Title
- c# MD5.Create returning nul
- Category
- C#
- Title
- asking for user input integer c#
- Category
- C#
- Title
- get day month year from date c#
- Category
- C#
- Title
- c# public static string
- Category
- C#
- Title
- c# switct case
- Category
- C#
- Title
- unity check tag
- Category
- C#
- Title
- c# window instantly close
- Category
- C#
- Title
- unity rigid body variable
- Category
- C#
- Title
- c# mixed multidimensional array
- Category
- C#
- Title
- unity 2d enemy field of view
- Category
- C#
- Title
- c# how to initialize an array
- Category
- C#
- Title
- open file in explorer c#
- Category
- C#
- Title
- Instantiate c#
- Category
- C#
- Title
- f string C#
- Category
- C#
- Title
- unity c# public all codes
- Category
- C#
- Title
- computer
- Category
- C#
- Title
- application variable in asp.net core
- Category
- C#
- Title
- perlin noise unity
- Category
- C#
- Title
- c# authorize attribute
- Category
- C#
- Title
- collision detector unity c# 2d
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- bubble sort c#
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- asp.net core mvc not triggering client side validation
- Category
- C#
- Title
- bitmap to byte array c#
- Category
- C#
- Title
- what is type unity
- Category
- C#
- Title
- c# pull request
- Category
- C#
- Title
- ihttpactionresult to object c#
- Category
- C#
- Title
- how to a equall to b in c#
- Category
- C#
- Title
- how to name GameObject in c#
- Category
- C#
- Title
- newtonsoft create dynamic object
- Category
- C#
- Title
- non null array length
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- webutility.urlencode space
- Category
- C#
- Title
- remove all array elements c#
- Category
- C#
- Title
- how to make an array in csharp
- Category
- C#
- Title
- find negative version of integer in c#
- Category
- C#
- Title
- while loop c#
- Category
- C#
- Title
- assign datasource to dropdownlist in c#
- Category
- C#
- Title
- how get data from json in c#
- Category
- C#
- Title
- unity accessing 2d pointlight from c# script
- Category
- C#
- Title
- external font family uwp c#
- Category
- C#
- Title
- take screenshot in c#
- Category
- C#
- Title
- mvc c# w3schools
- Category
- C#
- Title
- how to parse a string to an integer c#
- Category
- C#
- Title
- C# Unknown column 'FundAllocation' in 'field list
- Category
- C#
- Title
- displayname c#
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- how to get joypad axis input unity
- Category
- C#
- Title
- see if two string arrays are equal c#
- Category
- C#
- Title
- read configuration workerservice
- Category
- C#
- Title
- block wapalyzer from detecting codeigniter
- Category
- C#
- Title
- 2D follow ia unity 2D with agrorange
- Category
- C#
- Title
- c# reverse array
- Category
- C#
- Title
- c# capitalize first letter
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- c# exit program
- Category
- C#
- Title
- c# tab character
- Category
- C#
- Title
- unity how to rotate something to point to something else
- Category
- C#
- Title
- unity set sprite transparency
- Category
- C#
- Title
- c# remove time in datetime
- Category
- C#
- Title
- c# append to file
- Category
- C#
- Title
- c# param exception
- Category
- C#
- Title
- unity resources load
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- declare string array c# without size
- Category
- C#
- Title
- c# get current directory xamarin
- Category
- C#
- Title
- decimal to string whole number c#
- Category
- C#
- Title
- create char array c#
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- escape double quotes c#
- Category
- C#
- Title
- unity line renderer position count
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- c# check if string is in array
- Category
- C#
- Title
- CS0103 C# The name 'Request.Url.Scheme' does not exist in the current context
- Category
- C#
- Title
- add mime type for woff in web.config
- Category
- C#
- Title
- c# sql duplicate key exception
- Category
- C#
- Title
- reference to gameobject in different scene unity
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- httpclient soap request c#
- Category
- C#
- Title
- c# foreach arra
- Category
- C#
- Title
- C# get object property name
- Category
- C#
- Title
- c# interview questions
- Category
- C#
- Title
- unity get all children
- Category
- C#