This question got me to where I am now The name 'WM_DEVICECHANGE' does not exist in the current context
but I am having an issue detecting the proper messages when they happen, and I am not sure if the message codes I am using wrong or if I made a mistake elsewhere, but I just want to know how I can use the overridden method to detect ONLY USB attach or detach, or if maybe there is a more conclusive article somewhere to show me explicitly what each code within WM_DEVICECHANGE is?
private const int WM_DEVICECHANGE = 0x0219;
private const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //device was removed
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
switch (m.Msg) {
case WM_DEVICECHANGE:
if (m.WParam.ToInt64() == DBT_DEVICEARRIVAL)
MessageBox.Show("TEST");
if (m.WParam.ToInt64() == DBT_DEVICEREMOVECOMPLETE)
MessageBox.Show("DETACHED");
break;
}
}
my issue is if I try to follow the example in the question asked above, I get an error of "Operator '==' cannot be applied to operands of type 'System.IntPtr' and 'int'" but if I leave it as shown in my sample code then it doesn't trigger because the number in WParam never matches that in DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE