我正在解决这个问题:

在我的Java应用程序(安装在Windows OS计算机上)中,我必须捕获Win32 Event,该事件是由同一计算机上的另一个应用程序创建的。该应用程序是用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 catch Win32事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23653699/

10-10 21:35
查看更多