如何激活执行文件和发送密钥 [英] How to activate execute file and send key

查看:87
本文介绍了如何激活执行文件和发送密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,





我在批处理文件中启动执行文件后立即发送密钥有问题,如果来自执行文件的shell不是问题,

请在我的代码中看到如下。



'这是有效的(从直接运行)执行文件)

A.

Dear all,


I've a problem about to send key immediately after launch the execute file in batch file, if shell from execute file directly is not a problem,
please see as below in my code.

'This is worked (run from directly execute file)
A.

Dim ProcID AS Integer
Shell("notepad.exe",AppWinStyle.NormalFocus) ' this line is worked
AppActivate(ProcID)
SendKeys.SendWait("Hello world")



'这不起作用(从批处理文件运行)

B.


'This is not worked (run from batch file)
B.

Dim ProcID AS Integer
Process.start("c:\gm.bat",AppWinStyle.NormalFocus) 'this line is not worked
AppActivate(ProcID)
SendKeys.SendWait("Hello world")





-----在c:\gm.bat -------



----- in c:\gm.bat -------

call c:\windows\system32\notepad.exe





请帮帮我。



最好的问候,



添加代码块[/ edit]



please help me.

Best Regards,

[edit]Code block added[/edit]

推荐答案

使用Process类启动外部进程时,请调用 WaitForInputIdle on您创建的Process对象。在发送密钥之前,你没有给这个过程实际启动任何时间。
When you launch an external process using the Process class, call WaitForInputIdle on the Process object you created. You're not giving any time for the process to actually start before you send keys to it.


好的,有点棘手。生成notepad.exe后,批处理执行的主线程进入等待状态。因此,您可以轮询其状态。



ok, a little bit tricky. the main thread of batch execution enters wait state after spawning notepad.exe. therefore, you can poll its state.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test_ExecuteFromBatch
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {

                int bid;
                // run batch file
                using (Process p = Process.Start("npad.bat"))
                {
                    bid = p.Id;
                    // wait its main thread state to be 'wait'
                    while (p.Threads[0].ThreadState != System.Diagnostics.ThreadState.Wait)
                    {
                        Thread.Sleep(500);
                        Console.WriteLine(p.Threads[0].ThreadState);
                        p.Refresh();
                    }
                };
                // list all 'notepad's
                var l = Process.GetProcessesByName("notepad").Select(i1 =>
                new
                {
                    ProcessName = i1.ProcessName,
                    Id = i1.Id,
                    StartTime = i1.StartTime
                }).OrderByDescending(i1 => i1.StartTime);

                int id = -1;
                foreach (var item in l)
                {
                    Console.WriteLine("{0} {1} {2}", item.ProcessName, item.Id, item.StartTime);
                    // check if batch is parent of this notepad
                    if (id < 0 && bid == GetOwnerPid(item.Id))
                        id = item.Id;
                }
                if (id > 0)
                {
                    // Do what you want.
                    Console.WriteLine("{0}/{1}", bid, id);
                    Console.WriteLine(Process.GetProcessById(id).MainWindowTitle);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }

        // wmi query for parent
        static int GetOwnerPid(int pid)
        {
            int oid = -1;
            string q = string.Format("Select * from Win32_Process Where ProcessID = {0}", pid);
            using (ManagementObjectSearcher s = new ManagementObjectSearcher(q))
            {
                var o = s.Get().Cast<ManagementObject>().FirstOrDefault();
                if (o != null)
                {
                    oid = Convert.ToInt32(o.GetPropertyValue("ParentProcessId"));
                }
            }
            return oid > 0 ? oid : -1;
        }
    }
}


这篇关于如何激活执行文件和发送密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆