0

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

Community
  • 1
  • 1
  • You know what! you solved an issue i was facing from 1AM to 8AM with your very simple code, i can't find a way to thank you also stack community. thanks god :) – Amr SubZero Aug 11 '15 at 06:52

1 Answers1

0

ToInt64() will just return the address(or handle) as an integer. I believe you are wanting to retrieve the value stored there, in which case you would do

if(Marshal.ReadInt64(m.wParam) == DBT_DEVICEARRIVAL)
//and so on

Marshal is in the System.Runtime.InteropServices namespace.

Also make sure that is where the value is supposed to be pulled from, i.e. could be the high word or low word or something and not all 64/32 bits.

user1132959
  • 978
  • 8
  • 16
  • I tried your solution and then I tried checking all the fields within m for a number that matched what DBT_DEVICEARRIVAL was and NONE of them matched after a USB was inserted and the WM_DEVICECHANGE event was hit. Is it possible I have defined DBT_DEVICEARRIVAL with the wrong codes? If so then how did they work properly for anyone else? –  Aug 06 '13 at 18:59
  • @JBeck You have the right code. Try ReadInt32() instead of ReadInt64() – user1132959 Aug 06 '13 at 19:03
  • Still nothing using if(Marshal.ReadInt32(m.wParam) == DBT_DEVICEARRIVAL) it is never getting a number the same as DBT_DEVICEARRIVAL. Is the defined value for it really supposed to be 0x8000? –  Aug 06 '13 at 19:11
  • @JBeck Did you register your app to receive notifications? http://msdn.microsoft.com/en-us/library/windows/desktop/aa363431.aspx – user1132959 Aug 06 '13 at 19:28
  • Oh shoot! No I do not think I did that. How would I go about doing that in C#? –  Aug 06 '13 at 19:31
  • @JBeck Take a look here: http://www.pinvoke.net/default.aspx/user32.registerdevicenotification Basically you will call it on loading your app. You may have to find a tutorial if this doesn't have enough info. – user1132959 Aug 06 '13 at 20:15