【问题标题】:C# execute on command prompt running gethC# 在运行 geth 的命令提示符下执行
【发布时间】:2018-09-30 08:06:56
【问题描述】:

我有一个侦听器服务,我可以向它发送命令,但是使用此服务,它无法向运行 ethereuem 的 geth 的命令提示符发送命令。有没有办法强行让键盘敲击通过?

我注意到我有其他代码可以通过名称或 id 找到命令提示符,并且能够将该窗口置于最前面,但是当我尝试使用运行 geth 的命令提示符时,它不能似乎把那个窗口带到了前面。我希望这些信息能有所帮助。

namespace Listener
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var listener = new HttpListener())
            {
                listener.Prefixes.Add("http://localhost:8081/mytest/");

                listener.Start();

                string command = string.Empty;

                for (; ; )
                {
                    Console.WriteLine("Listening...");

                    HttpListenerContext context = listener.GetContext();
                    HttpListenerRequest request = context.Request;

                    // TODO: read and parse the JSON data from 'request.InputStream'

                    using (StreamReader reader = new StreamReader(request.InputStream))
                    {
                        // Would prefer string[] result = reader.ReadAllLines();
                        string[] result = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        foreach (var s in result)
                        {
                            command = s;
                        }
                    }

                    // send command to other geth window
                    sendKeystroke(command);

                    using (HttpListenerResponse response = context.Response)
                    {
                        // returning some test results

                        // TODO: return the results in JSON format

                        string responseString = "<HTML><BODY>Hello, world!</BODY></HTML>";
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                        response.ContentLength64 = buffer.Length;
                        using (var output = response.OutputStream)
                        {
                            output.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        private static void sendToOpenCmd(string command)
        {
            int processId = 13420;
            //processId = int.Parse(13420);
            System.Diagnostics.Process proc = (from n in System.Diagnostics.Process.GetProcesses()
                                               where n.ProcessName == "geth"
                                               //where n.Id == processId
                                               select n).FirstOrDefault();
            if (proc == null)
            {
                //MessageBox.Show("No such process.");
                Console.WriteLine("No such process.");
            }
            else
            {
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait(command + "{enter}");
                Console.WriteLine("Sent! " + command + " " + DateTime.Now.ToString());
            }

        }

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public static void sendKeystroke(string command)
        {
            const uint WM_KEYDOWN = 0x100;
            const uint WM_SYSCOMMAND = 0x018;
            const uint SC_CLOSE = 0x053;

            IntPtr WindowToFind = FindWindow(null, "geth");

            //ushort[] result = command.Where(i => ushort.TryParse(i, out short s)).Select(ushort.Parse);
            //ushort[] result = command.Where(i => { ushort r = 0; return ushort.TryParse(i, out r); }).Select(ushort.Parse);
            ushort result;
            ushort.TryParse(command, out result);
            IntPtr result3 = SendMessage(WindowToFind, WM_KEYDOWN, ((IntPtr)result), (IntPtr)0);
            //IntPtr result3 = SendMessage(WindowToFind, WM_KEYUP, ((IntPtr)c), (IntPtr)0);
        }
    }
}

【问题讨论】:

    标签: c# geth


    【解决方案1】:

    哦,在运行 geth 的命令提示符下,windows 似乎有三个进程 ID。我不知道为什么有三个。我尝试了所有三个,其中一个对我有用。所以我不能用“geth”的名字来调用这个进程,这似乎不是发送击键的正确窗口或进程。希望这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 2011-10-26
      • 2013-08-01
      • 2020-12-16
      • 2023-03-25
      • 2014-12-03
      • 2021-08-28
      • 1970-01-01
      相关资源
      最近更新 更多