The name 'WM_DEVICECHANGE' does not exist in the current context

南楼画角 提交于 2019-12-02 01:30:34

问题


I am trying to detect a usb arrival event . I tried to override wndproc() for getting my messages. But I am facing an error by windows messages.

The error is :

The name 'WM_DEVICECHANGE' does not exist in the current context

The name 'DBT_DEVICEARRIVAL' does not exist in the current context

Also this is the code I am tried.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;         
using System.IO;
using Microsoft.Win32.SafeHandles; 

namespace USBCheckerApp
{
    public partial class Form1 : Form
    {
        bool bDeviceFound = false;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!bDeviceFound)
            {
                button1.Enabled = false;
            }


        }
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_DEVICECHANGE:
                    if (m.WParam == DBT_DEVICEARRIVAL)
                    {
                        MessageBox.Show("MEDIA FOUND");
                    }
            }

        }

    }
}

Added so that you could suggest any updations in the same. Thanks


回答1:


You have to declare and define the values of the constants:

private const int DBT_DEVICEARRIVAL = 0x8000;
private const int WM_DEVICECHANGE = 0x0219;


来源:https://stackoverflow.com/questions/15877851/the-name-wm-devicechange-does-not-exist-in-the-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!