想实现自动弹出优盘,向"安全删除硬件"对话框,发PostMessage消息,
替代鼠标点击"停止"按钮,让优盘自动弹出。
 
尽量不用API函数,因为用API函数比较麻烦,
普通用户下无法使用CreateFile获取设备句柄;
请大家帮忙看下,谢谢//弹出"安全删除硬件"的对话框(VC6.0)
void CProg001Dlg::OnButton5() 

    char strSystemDirectory[256]; 
    GetSystemDirectory(strSystemDirectory, 256 );     CString strTemp = strSystemDirectory; 
    strTemp += "\\rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";     WinExec( strTemp, SW_SHOW ); 

///////////////////////////////////////////////向记事本文件发送字符串(C#)
UInt32 notepad = FindWindow("Notepad", null);
UInt32 edit = FindWindowEx(notepad, 0, "Edit", null);
PostMessage(edit, 0x102, 'A', 1);   //发消息
PostMessage(edit, 0x102, 'b', 1);

解决方案 »

  1.   

    弹出U盘代码
    //以下代码来自网上,需DDK、SDK支持
      

  2.   

    还想请教一个问题:
    如果在Administrator和普通用户下,
    都可以对优盘和移动硬盘进行控制(包括锁盘,弹出盘,判断硬盘是否本地盘等),
    使用DDK和SDK可以实现么?
    之前只是研究ARP攻击程序的时候了解一些DDK....
      

  3.   


    //reject_btn值为0
    //UInt32 reject_btn = FindWindowEx(reject_dlg, 0, "Button", "停止"); 
    请大家帮忙看下,谢谢!using System.Runtime.InteropServices; //DLL
    using System.Diagnostics;namespace USB
    {
        public partial class Form1 : Form
        {
            #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);
            #endregion        public Form1()
            {
                InitializeComponent();
            }        private void btn_Reject_Click(object sender, EventArgs e)
            {
                //弹出"安全删除硬件"对话框
                 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(); //启动             //查找“停止”按钮并发Click消息
                const int BM_CLICK = 0x00F5;
                UInt32 reject_dlg = FindWindow("#32770", "安全删除硬件");            //reject_btn值为0
       UInt32 reject_btn = FindWindowEx(reject_dlg, 0, "Button", "停止"); 
                PostMessage(reject_btn, BM_CLICK, 0, 0);   //发消息
                //测试向记事本发信息
                //UInt32 notepad = FindWindow("Notepad", null);
                //UInt32 edit = FindWindowEx(notepad, 0, "Edit", null);
                //PostMessage(edit, 0x102, 'A', 1);   //发消息
                //PostMessage(edit, 0x102, 'b', 1);
            }
        }
    }
      

  4.   


           (1)先插入优盘再运行程序,可以弹出;
           (2)先运行程序,通过系统消息来执行弹出优盘的操作,不可以;
           感觉刚插入优盘时跳出的对话框,有干扰作用,但是单步调试可以弹出;
           (3)搞不明白是时间问题,还是没有设置当前活动窗口问题;
           (4)为避免用户选择取消,还准备暂时屏蔽鼠标和键盘几秒。 
            //弹出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(2000);   //等待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设备"); 
                }
            }
      

  5.   

    记得可以操作wmi然后弹出的,LZ也可以搜索一下这方面的资料