有谁知道,如何使用sendkeys保护将按键发送到应用程序?
我尝试了sendkeys和记事本它完美无缺,但当我运行目标应用程序并尝试我的程序时,它没有,并且在记事本中它也不起作用,当这个应用程序运行时。
我不知道如何绕过这种保护。
答案 0 :(得分:1)
不明白受保护应用程序的含义是什么?
无论哪种方式,.NET框架附带的SendKeys都是有限的。你可以使用windows api:
[DllImport("user32.dll")]
private static extern UInt32 SendInput(UInt32 nInputs,[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] Input[] pInputs, Int32 cbSize);
此外,您可能希望将其他应用程序设置为前台:
[DllImport("User32.dll")]
private static extern int SetForegroundWindow(IntPtr point);
然后,您将获得应用程序主窗口句柄的过程,如下所示:
var processes = Process.GetProcessesByName(processName);
// Note that this line will get the first process with the given name:
// if there are multiple, you will only get the first, and you should
// also include a check that the array isn't empty!
var handle = processes[0].MainWindowHandle;
然后
SetForegroundwindow(handle);
SendInput(....);
如果您想将密钥发送到游戏应用程序,则需要额外的工作。大多数游戏都使用DirectX输入。 简而言之,如果您需要更详细的信息,请告诉我。
用于API调用的结构和枚举:
[StructLayout(LayoutKind.Sequential)]
struct MouseInput
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct KeyboardInput
{
public short wVk; //Virtual KeyCode (not needed here)
public short wScan; //Directx Keycode
public int dwFlags; //This tells you what is use (Keyup, Keydown..)
public int time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HardwareInput
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
struct Input
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MouseInput mi;
[FieldOffset(4)]
public KeyboardInput ki;
[FieldOffset(4)]
public HardwareInput hi;
}
[Flags]
public enum KeyFlag
{
KeyDown = 0x0000,
ExtendedKey = 0x0001,
KeyUp = 0x0002,
UniCode = 0x0004,
ScanCode = 0x0008
}