我需要设置一个 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/