質問

does anybody know, how to send key press to application with sendkeys protection ?

I tried sendkeys and to notepad it worked flawlessly, but when I ran target application and tried my program, it doesn't and in notepad it doesn't work too, when this app was running.

I don't know how to bypass that protection.

役に立ちましたか?

解決

Don't understand what you mean by protected application?

Either way the SendKeys that comes with the .NET framework is limited. You can use the windows api:

[DllImport("user32.dll")]
private static extern UInt32 SendInput(UInt32 nInputs,[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] Input[] pInputs, Int32 cbSize);

Also you might want to set the other application to foreground:

[DllImport("User32.dll")]
private static extern int SetForegroundWindow(IntPtr point);

You then get the process of the application's main window handle, something like this:

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;

Then

SetForegroundwindow(handle);
SendInput(....);

If you want to send keys to game applications it needs extra work. Most games make use of DirectX input. This is in a nutshell, if you need more detailed info let me know.

Structs and enum to use with API call:

[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
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top