我想模拟VK_ENTER
到控制台程序。
当我手动VK_ENTER
启动程序时,我可以成功发布cmd.exe
条消息。
它不会找到由ProcessBuilder
启动的控制台程序的hwnd。
@Test
public void testGetPid(){
ProcessBuilder pb = new ProcessBuilder("test.exe"); // not work
// ProcessBuilder pb = new ProcessBuilder("notepad.exe"); // work
Process p = null;
try {
p = pb.start();
//"handle"
System.out.println(p);
Field f = p.getClass().getDeclaredField( "handle");
f.setAccessible( true);
long procHandle = f.getLong( p);
System.out.println("Handle: " + procHandle);
//Kernel32.INSTANCE.
HANDLE handle = new HANDLE();
handle.setPointer(Pointer.createConstant(procHandle));
final int pid = Kernel32.INSTANCE.GetProcessId(handle);
System.out.println("Pid: " + pid);
ThreadUtil.sleep(5*1000);
sendKeyToProcess(pid);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ThreadUtil.sleep(60*1000);
if( p != null )
p.destroy();
}
}
private void sendKeyToProcess(final int pid) {
final User32 user32Lib = User32.INSTANCE;
final int WM_KEYDOWN = 0x0100;
final int WM_KEYUP = 0x0101;
final int WM_CHAR = 0x0102;
user32Lib.EnumWindows(new WinUser.WNDENUMPROC() {
@Override
public boolean callback(HWND paramHWND, Pointer paramPointer) {
IntByReference intRef = new IntByReference();
user32Lib.GetWindowThreadProcessId(paramHWND, intRef);
if( pid == intRef.getValue() ){
// final WPARAM wPARAM = new WPARAM(13); // enter
final WPARAM wPARAM = new WPARAM(48); // '0'
final LPARAM lPARAM = new LPARAM(0);
System.out.println("Match hwnd: " + paramHWND.toString());
user32Lib.PostMessage(paramHWND,
WM_KEYDOWN, wPARAM, lPARAM);
User32.INSTANCE.EnumChildWindows(paramHWND,
new WinUser.WNDENUMPROC() {
@Override
public boolean callback(HWND childParamHWND,
Pointer paramPointer) {
System.out.println("send");
user32Lib.PostMessage(childParamHWND,
WM_KEYDOWN, wPARAM, lPARAM);
return true;
}
}, null);
//return false;
}
return true;
}
}, null);
}
答案 0 :(得分:0)
它为我工作:(所有JNA解决方案)