简体   繁体   English

C#在运行geth的命令提示符下执行

[英]C# execute on command prompt running geth

I have a listener service where I can send a command to it, but with this service, it's unable to send a command to a command prompt running ethereuem's geth. 我有一个侦听器服务,可以向其发送命令,但是通过此服务,它无法将命令发送给运行ethereuem的geth的命令提示符。 Is there a way to forcibly get the keyboard strokes through? 有没有办法强制通过键盘击键?

I notice that I have other code that can find a command prompt by name or id and able to bring that window to the front, but when I attempt to do that with the command prompt that is running geth, it can't seem to bring that window to the front. 我注意到我还有其他代码可以按名称或ID查找命令提示符,并且能够将该窗口置于最前面,但是当我尝试使用运行geth的命令提示符来执行此操作时,似乎无法那个窗户朝前。 I hope that bit of information may help. 我希望可以提供一些信息。

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);
        }
    }
}

ohhh there seems to be three process id's that windows has on that command prompt running geth. 哦,在运行geth的命令提示符下,Windows似乎有三个进程ID。 i'm not sure why there's three. 我不确定为什么有三个。 i tried all three, and one of them worked for me. 我尝试了全部三个,其中一个为我工作。 so i can't call the process by name of "geth", that doesn't seem to be the correct window or process to send the keystrokes to. 因此,我无法使用“ geth”的名称来调用该进程,这似乎不是向其发送击键的正确窗口或进程。 hopefully this helps someone else! 希望这可以帮助其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM