从C#(WPF)程序向putty.exe实例发送命令
我要说的第一件事是通过C#/a C#plugin等进行SSH'ing。在这种情况下,这不是一个选项从C#(WPF)程序向putty.exe实例发送命令,c#,ssh,putty,C#,Ssh,Putty,我要说的第一件事是通过C#/a C#plugin等进行SSH'ing。在这种情况下,这不是一个选项 此外,还需要假设putty已经在运行,我不打算通过C#程序执行putty。我想测试的第一件事是一个简单的“退出”按钮,它将抓取putty的当前实例,并将“退出”写入窗口。您很可能有几个选项。下面的代码显示了如何模拟击键并将其发送到窗口(SendInput)。您可以使用 sEdKEdis,这可能是一个更好的解决方案,但是 SDENTIMEP>/代码>应该与任何事情一起工作——尽管用户可以在自动化过程
此外,还需要假设putty已经在运行,我不打算通过C#程序执行putty。我想测试的第一件事是一个简单的“退出”按钮,它将抓取putty的当前实例,并将“退出”写入窗口。您很可能有几个选项。下面的代码显示了如何模拟击键并将其发送到窗口(
SendInput
)。您可以使用<代码> sEdKEdis<代码>,这可能是一个更好的解决方案,但是<代码> SDENTIMEP>/代码>应该与任何事情一起工作——尽管用户可以在自动化过程中开始敲击键盘时干扰键击。目前我手头没有SendKeys示例
这个粗糙的示例应该获得Putty窗口的窗口句柄,将窗口向前/聚焦在窗口上,然后模拟击键。对不起,不是最干净的,但它可以满足你的需要
// Register Win32 API stuff
const int INPUT_KEYBOARD = 1;
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
public enum VirtualKeys : ushort
{
SHIFT = 0x10,
CONTROL = 0x11,
MENU = 0x12,
ESCAPE = 0x1B,
BACK = 0x08,
TAB = 0x09,
RETURN = 0x0D,
PRIOR = 0x21,
NEXT = 0x22,
END = 0x23,
HOME = 0x24,
LEFT = 0x25,
UP = 0x26,
RIGHT = 0x27,
DOWN = 0x28,
SELECT = 0x29,
PRINT = 0x2A,
EXECUTE = 0x2B,
SNAPSHOT = 0x2C,
INSERT = 0x2D,
DELETE = 0x2E,
HELP = 0x2F,
NUMPAD0 = 0x60,
NUMPAD1 = 0x61,
NUMPAD2 = 0x62,
NUMPAD3 = 0x63,
NUMPAD4 = 0x64,
NUMPAD5 = 0x65,
NUMPAD6 = 0x66,
NUMPAD7 = 0x67,
NUMPAD8 = 0x68,
NUMPAD9 = 0x69,
MULTIPLY = 0x6A,
ADD = 0x6B,
SEPARATOR = 0x6C,
SUBTRACT = 0x6D,
DECIMAL = 0x6E,
DIVIDE = 0x6F,
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
OEM_1 = 0xBA, // ',:' for US
OEM_PLUS = 0xBB, // '+' any country
OEM_COMMA = 0xBC, // ',' any country
OEM_MINUS = 0xBD, // '-' any country
OEM_PERIOD = 0xBE, // '.' any country
OEM_2 = 0xBF, // '/?' for US
OEM_3 = 0xC0, // '`~' for US
MEDIA_NEXT_TRACK = 0xB0,
MEDIA_PREV_TRACK = 0xB1,
MEDIA_STOP = 0xB2,
MEDIA_PLAY_PAUSE = 0xB3,
LWIN = 0x5B,
RWIN = 0x5C
}
private KEYBDINPUT CreateKeyboardInput(short wVK, uint flag)
{
KEYBDINPUT i = new KEYBDINPUT();
i.wVk = (ushort)wVK;
i.wScan = (ushort)MapVirtualKey((uint)wVK, 0);
i.time = 0;
i.dwExtraInfo = IntPtr.Zero;
i.dwFlags = flag;
return i;
}
public static short GetCharacterValue(char c)
{
//http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx
if (c >= 'a' && c <= 'z') return (short)(c - 'a' + 0x41);
else if (c >= '0' && c <= '9') return (short)(c - '0' + 0x30);
else if (c == '-') return 0x6D; // Note it's NOT 0x2D as in ASCII code!
else if (c == ' ') return 0x20;
else return 0; // default
}
//创建击键列表并将其发送到窗口
List inputList=新列表();
//这将键入“HI”。一个是向下击键,然后是向上击键(这就是为什么有4个)
输入kbInput;
kbInput=新输入();
kbInput.type=输入\键盘;
kbInput.ki=CreateKeyboardInput(GetCharacterValue('H'),0);
inputList.Add(kbInput);
kbInput=新输入();
kbInput.type=输入\键盘;
kbInput.ki=CreateKeyboardInput(GetCharacterValue('H'),KEYEVENTF\u KEYUP);
inputList.Add(kbInput);
kbInput=新输入();
kbInput.type=输入\键盘;
kbInput.ki=CreateKeyboardInput(GetCharacterValue('I'),0);
inputList.Add(kbInput);
kbInput=新输入();
kbInput.type=输入\键盘;
kbInput.ki=CreateKeyboardInput(GetCharacterValue('I'),KEYEVENTF\u KEYUP);
inputList.Add(kbInput);
INPUT[]inp=inputList.ToArray();
SendInput((uint)inp.Length、inp、Marshal.SizeOf(new INPUT());
谢谢,我会考虑使用这个
// Get window handle
var windowHandle = FindWindow(null, "Putty Window Title");
// Bring window forward and focus
ShowWindow(windowHandle, SW_RESTORE);
SetFocus(windowHandle);
SetForegroundWindow(windowHandle);
// Create list of keystrokes and send them to the window
List<INPUT> inputList = new List<INPUT>();
// This will type "HI". One is a downward keystroke and then the upward (that's why there's 4)
INPUT kbInput;
kbInput = new INPUT();
kbInput.type = INPUT_KEYBOARD;
kbInput.ki = CreateKeyboardInput(GetCharacterValue('H'), 0);
inputList.Add(kbInput);
kbInput = new INPUT();
kbInput.type = INPUT_KEYBOARD;
kbInput.ki = CreateKeyboardInput(GetCharacterValue('H'), KEYEVENTF_KEYUP);
inputList.Add(kbInput);
kbInput = new INPUT();
kbInput.type = INPUT_KEYBOARD;
kbInput.ki = CreateKeyboardInput(GetCharacterValue('I'), 0);
inputList.Add(kbInput);
kbInput = new INPUT();
kbInput.type = INPUT_KEYBOARD;
kbInput.ki = CreateKeyboardInput(GetCharacterValue('I'), KEYEVENTF_KEYUP);
inputList.Add(kbInput);
INPUT[] inp = inputList.ToArray();
SendInput((uint)inp.Length, inp, Marshal.SizeOf(new INPUT()));