java - 使用 SetWindowLong 覆盖 WndProc

标签 java windows-ce jna

我需要设置一个 WindowLong 来覆盖 WndProc,我正在使用 coredll.dll 中的此方法:

public interface CoreDll extends StdCallLibrary {
	//loads the coredll with unicode options
    CoreDll INSTANCE = (CoreDll)Native.loadLibrary("coredll", CoreDll.class,
                                                   W32APIOptions.UNICODE_OPTIONS);
    //native calls
    HMODULE GetModuleHandle(String lpModuleName);
    long GetWindowLong(HWND hwnd, int gwlWndproc);
    long SetWindowLong(HWND hWnd,int nIndex,Callback dwNewLong);
    LRESULT DefWindowProc(HWND hWnd, int uMsg, WPARAM uParam,LPARAM lParam);
  }

coredll中原始方法签名是:

LONG SetWindowLong( 
  HWND hWnd, 
  int nIndex, 
  LONG dwNewLong
); 

但是当我使用它时,它总是返回 0,根据 Microsoft 的说法“指定的 32 位整数的先前值表示成功。零表示失败”。无论如何,我正在尝试以这种方式执行它:

public interface CallbackProc extends Callback, StdCall {
	LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
}

//Get a handle to the current process
final HWND mainHwnd = CoreDll.INSTANCE.GetModuleHandle(null);

//Get a reference to the current process to send it in the new WndProc
long value=CoreDll.INSTANCE.GetWindowLong(new HWND(mainHwnd.getPointer()), -4);
final LONG_PTR prevWndProc = new LONG_PTR(value); 

//Sets the new Method to override WndProc
final RfidCallbackProc ptr=new RfidCallbackProc() {
	@Override
   	public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam,LPARAM lParam) {
        //returns the call to the process  
        return CoreDll.INSTANCE.CallWindowProc(prevWndProc, hWnd, uMsg, wParam, lParam);
    }
};

//Sets the new method to override Windows' WndProc
int num=(int)CoreDll.INSTANCE.SetWindowLong(new HWND(mainHwnd.getPointer()),-4 ,ptr);
//NUM IS ALWAYS 0

如果有人能给我提示,我将非常感激,谢谢。

最佳答案

您正在检索模块句柄(GetModuleHandle),该句柄引用加载到进程空间而不是窗口中的可执行文件(exe 或 dll)。 SetWindowsLong 失败,因为您传递给它的对象不是窗口。 IIRC 您在表单/控件上有一个方法来获取 native HWND,这是您应该传递给 SetWindowLong 的正确类型的对象。 查看代码,我假设您有一个 RFID 库,可以通过 Windows 消息发送代码。您可以考虑将其与一些 native 代码包装到一个对象中,然后生成“常规”.NET 事件。它会稍微复杂一些,但避免了 SetWindowLong 可能产生的一些令人头疼的问题(例如:如果窗口仍然有效时带有 wndproc 的对象被销毁)。

关于java - 使用 SetWindowLong 覆盖 WndProc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28458297/

相关文章:

java - 我无法在主类中获取 JPanel 的背景颜色

javascript - ActiveX html 控件不调用代码

c# - 如何避免 .NET CF 3.5 和 CE 6 R3 中的严重错误

c++ - 为什么对某些 Windows CE 目标的编译在 Visual Studio 2008 中得到 "skipped"?

java - 在 Java 中将 native 窗口句柄传递给 DLL

java - 采用 spring-session 后迁移 HttpSessionListener

java - Android 应用程序在多次切换 fragment 时崩溃

java - 在 Java 中处理重定向 302

java - Bamboo 升级 5.5->5.7 后 AIX 代理失败

java - JNA:无法创建结构子类的对象 - 结构字段无效