运用JNA来操作DLL函数,获取VB窗口程序句柄,进而向VB程序窗口程序的消息队列发送消息,代码如下:
package com.stupidzhe;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import java.util.concurrent.TimeUnit;
public class JnaTest {
public static void main(String[] args) throws InterruptedException {
char[] windowText = new char[512];
HWND hwnd = User32.INSTANCE.GetForegroundWindow(); // then you can call it!
User32.INSTANCE.GetWindowText(hwnd, windowText, 512);
System.out.println(Native.toString(windowText));
HWND btn = User32.INSTANCE.FindWindowEx(hwnd, null, null, null);
WinUser.MSG msg = new WinUser.MSG();
int result;
while (true) {
WinDef.LRESULT res = User32.INSTANCE.SendMessage(btn, 0xF5, new WinDef.WPARAM(0), new WinDef.LPARAM(0));
User32.INSTANCE.GetWindowText(btn, windowText, 512);
System.out.println(Native.toString(windowText));
TimeUnit.SECONDS.sleep(2);
}
}
}