首页 新闻 会员 周边 捐助

C#打开有密码的Access并发送密码

0
悬赏园豆:100 [已关闭问题] 关闭于 2010-06-04 16:24

我想用C#打开有密码的Access,发送密码后,点击确定,现在遇到问题如下,如果分成两步操作(一个按钮打开,一个按钮发密码)没有问题,但是合在一起就不能成功,好像Process p = Process.Start(Application.StartupPath + "\\MyDB.mdb");后面的代码就不执行,一直等待密码录入以后才能执行后面的代码,请问如何解决,代码如下

//打开数据库,并发送密码
        private void btnOpenDB_Click(object sender, EventArgs e)
        {
            Process p = Process.Start(Application.StartupPath + "\\MyDB.mdb");
            p.WaitForInputIdle(1000);
            SendAccessPassWord();//发送Access数据库密码
        }   
//发送Access数据库密码
private void SendAccessPassWord()
        {
            try
            {
                IntPtr ParenthWnd = new IntPtr(0);//父窗口
                IntPtr _PassWnd = new IntPtr(0);//密码窗口
                IntPtr _RichEdit20WWnd = new IntPtr(0);//密码框
                IntPtr _ButtonWnd = new IntPtr(0); //确定按钮

                ParenthWnd = FindWindow("OMain", "Microsoft Access");
                if (ParenthWnd != IntPtr.Zero)//如果窗口有效
                {
                    _PassWnd = FindWindowEx((IntPtr)0, _PassWnd, "#32770", "要求输入密码"); //找到密码窗口
                    if (_PassWnd != IntPtr.Zero)
                    {
                        _RichEdit20WWnd = FindWindowEx(_PassWnd, (IntPtr)0, "RichEdit20W", ""); //密码框
                        if (_RichEdit20WWnd != IntPtr.Zero)//找到密码文本框
                        {
                            int result_send_pass = SendMessage(_RichEdit20WWnd, WM_SETTEXT, (IntPtr)0, "12345"); //发送消息
                            if (result_send_pass != 0)//发送成功
                            {
                                _ButtonWnd = FindWindowEx(_PassWnd, (IntPtr)0, "Button", "确定");
                                if (_ButtonWnd != IntPtr.Zero) //如果找到确定按钮
                                {
                                    int result_send_button = SendMessage(_ButtonWnd, WM_CLICK, (IntPtr)0, "0");//发送消息,点击确定按钮
                                    lblState.Text = "发送Access密码OK";
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                lblState.Text = "发送Access密码Error";
            }
        }   

绿色梦想的主页 绿色梦想 | 菜鸟二级 | 园豆:206
提问于:2010-05-27 11:24
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册