因为需要使用回调函数来捕获系统消息,
在普通WinForm下可以使用,但是在WindowsService环境下Message不能使用
请大家帮忙看下,谢谢。
代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices; 
using System.IO;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;namespace USB_Control
{
    public partial class USBService : ServiceBase
    {
        #region WIN32 API
        [DllImport("user32.dll")]
        public static extern UInt32 FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern UInt32 FindWindowEx(UInt32 hwndParent, UInt32 hwndChildAfter, string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern bool PostMessage(UInt32 hWnd, uint Msg, uint wParam, uint lParam);
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, uint Msg, uint wParam, string lParam);
        [DllImport("user32.dll")]
        static extern void BlockInput(bool Block);
        #endregion        #region 引用C++语言的DLL
        [DllImport("MFCdll.dll")]
        public static extern void ShuoUSB();      //锁移动存储设备
        [DllImport("MFCdll.dll")]
        public static extern bool IsIDE(string str); //判断是否IDE硬盘
        #endregion        #region WndProc
        protected override void WndProc(ref  Message m)
        {
            //有存储设备插入时锁盘
            if (m.Msg == WndProMsgConst.WM_DEVICECHANGE)
            {
                switch (m.WParam.ToInt32())
                {
                    case WndProMsgConst.DBT_DEVICEARRIVAL:
                        ShuoUSB();  //锁移动盘
                        Reject_USB();  //没锁成功则退出该盘
                        break;
                }
            }
            base.WndProc(ref  m);
        }
        #endregion        public USBService()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {

            // TODO: 在此处添加代码以启动服务。
        }        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。        }        #region Reject_USB()
        private void Reject_USB()
        {
            //获取当前用户的权限是否Administrator
            //有移动盘插入则用钩子挂住键盘和鼠标
            //BlockInput(true);            POP_USB();
            const uint BM_CLICK = 0xF5; //鼠标点击的消息
            System.Threading.Thread.Sleep(1000);   //等待1秒接收消息
            UInt32 Error_dlg = FindWindow(null, "弹出 USB Mass Storage Device 时出问题");            if (Error_dlg != 0)
            {
                UInt32 OK_btn = FindWindowEx(Error_dlg, 0, null, "确定");
                PostMessage(OK_btn, BM_CLICK, 0, 0);   //单击"确定"按钮
                POP_USB();
            }            //BlockInput(false);
        }
        #endregion        #region POP_USB()
        //弹出USB设备
        private void POP_USB()

        {
            string str_cmd = "";
            str_cmd = "rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe"; //设定程序名
            p.StartInfo.Arguments = "/c " + str_cmd; //设定程序执行参数 
            p.StartInfo.UseShellExecute = false; //关闭Shell的使用 
            p.StartInfo.RedirectStandardInput = true; //复位向标准输入 
            p.StartInfo.RedirectStandardOutput = true; //复位向标准输出 
            p.StartInfo.RedirectStandardError = true; //复位向错误输出 
            p.StartInfo.CreateNoWindow = true; //设置不显示窗口 
            p.Start(); //启动             const uint BM_CLICK = 0xF5; //鼠标点击的消息
            System.Threading.Thread.Sleep(1000);   //等待1秒接收消息
            UInt32 reject_dlg = FindWindow(null, "安全删除硬件");            //进入"安全删除硬件"对话框
            if (reject_dlg != 0)
            {
                UInt32 reject_btn = FindWindowEx(reject_dlg, 0, null, "停止(&S)");
                UInt32 Close_btn = FindWindowEx(reject_dlg, 0, null, "关闭(&C)");                PostMessage(reject_btn, BM_CLICK, 0, 0);   //单击"停止"按钮                System.Threading.Thread.Sleep(2000);   //等待2秒
                UInt32 Check_dlg = FindWindow(null, "停用硬件设备");                System.Threading.Thread.Sleep(3000);   //等待1秒
                //进入"停用硬件设备"对话框
                if (Check_dlg != 0)
                {
                    UInt32 Check_btn = FindWindowEx(Check_dlg, 0, null, "确定");                    System.Threading.Thread.Sleep(2000);   //等待1秒
                    PostMessage(Check_btn, BM_CLICK, 0, 0);   //单击"确定"按钮                     System.Threading.Thread.Sleep(1000);   //等待1秒
                    PostMessage(Close_btn, BM_CLICK, 0, 0);   //单击"关闭"按钮
                }
            }            else
            {
                //MessageBox.Show("没有插入USB设备"); 
            }
        }
        #endregion    }    #region WndProMsgConst
    class WndProMsgConst
    {
        #region WndProc常量
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;
        public const int DBT_CONFIGCHANGED = 0x0018;
        public const int DBT_CUSTOMEVENT = 0x8006;
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;
        public const int DBT_DEVNODES_CHANGED = 0x0007;
        public const int DBT_QUERYCHANGECONFIG = 0x0017;
        public const int DBT_USERDEFINED = 0xFFFF;
        #endregion
    }
    #endregion
}