gpt4 book ai didi

java - 使用 Java JNA 捕获 Win32 事件

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:13 26 4
gpt4 key购买 nike

我正在解决这个问题:

在我的 Java 应用程序(安装在 Windows 操作系统计算机上)中,我必须捕获由同一计算机上的另一个应用程序创建的 Win32 事件。这个应用程序是用 C++ 编写的,无法更改它。我有信息表明我必须使用 OpenEvent 函数。我按照以下内容开始: Calling OpenEvent fails through JNA

这是我的代码:

    public class WinEventListener {
private Logger logger = LoggerFactory.getLogger(WinEventListener.class);

static {
Native.register("kernel32");
}

public static native HANDLE OpenEventW(int access, boolean inheritHandle, WString name);

public static native int WaitForSingleObject(HANDLE hHandle, int dwMilliseconds);

public static native boolean CloseHandle(HANDLE hObject);

public static class HANDLE extends PointerType {

public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}

static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {

{
super.setPointer(Pointer.createConstant(-1));
}

public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};

public void listen() throws Exception {
HANDLE handle = null;
do {
//logger.debug("Wainting for handle");
handle = OpenEventW(2, false, new WString("VLIT_SERVER_DATA"));
logger.debug("Handle:" + handle.toString());
Thread.sleep(1000);
} while (handle == null);
logger.debug("Handle obtained");

while(true){
int result = WaitForSingleObject(handle,Integer.MAX_VALUE);
if(result == 0){
logger.debug("Handle signalized");
VLITProcceserThread thread = new VLITProcceserThread();
thread.start();
CloseHandle(handle);
}
}

}


}

基本上我想在listen()方法中等待其他程序创建的HANDLE,如果它创建了,则等待它的信号状态,执行一些操作并释放句柄。

但是我没有成功。有人能指出我正确的方法吗?

非常感谢!

最佳答案

如果打开句柄失败,则很可能是权限问题。您的程序作为服务运行吗?我试图做类似的事情,并且当我通过作为服务运行的程序内的系统调用调用该程序时,能够让该程序运行并工作。

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.WString;
import com.sun.jna.FromNativeContext;

public class WinEventListener {
static {
Native.register("kernel32");
}

public static native HANDLE OpenEventW(int access, boolean inheritHandle, WString name);

public static native int WaitForSingleObject(HANDLE hHandle, int dwMilliseconds);

public static native boolean CloseHandle(HANDLE hObject);

public static class HANDLE extends PointerType {

public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}

static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {

{
super.setPointer(Pointer.createConstant(-1));
}

public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};

public void listen() {
try {


HANDLE handle = null;
do {
handle = OpenEventW(2031619, false, new WString("event_name"));
if(handle == null) {
System.out.print("Handle is null\n");
}
Thread.sleep(500);
} while (handle == null);

while(true){
// 50 second timeout
int result = WaitForSingleObject(handle, 50000);
if(result == 0){
System.out.print("Handle signaled\n");
}
else if (result == 258){
System.out.print("Timed out\n");
}
else{
System.out.print("Handle not signaled\n");
System.out.print(result);
}
System.out.print(result);
//System.out.print(handle);
Thread.sleep(100);
}
}
catch (Exception exc)
{
System.out.print(exc);
//Thread.sleep(10000);

//writer.writeln(exc);
}

}

public static void main(String[] args) {
WinEventListener listener = new WinEventListener();
listener.listen();
}

}

关于java - 使用 Java JNA 捕获 Win32 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com