再会,
我已经使用 JNA 与 Windows API 交互有一段时间了,现在我在创建窗口时陷入困境。据我做了以下事情:
1. 创建了现有窗口的子窗口并为其获取了有效的处理程序。
2. 了解Windows 中的每个窗口都有一个不间断的消息分发循环。
3. 了解将我的窗口包含在消息分发循环中的最佳方法是使用类似以下代码的代码(不是我的,但这也是我会做的):
final LONG_PTR prevWndProc = new LONG_PTR(User32.INSTANCE.GetWindowLong(hwnd, User32.GWL_WNDPROC)); //this is to obtain a pointer to the WNDPROC of the parent window, which we are going to need later
wndProcCallbackListener = new WndProcCallbackListener()
{
public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam)
{
if (uMsg == WTSAPI.WM_POWERBROADCAST)
{
System.out.println("WM_POWERBROADCAST Event: hWnd="+hwnd+", uMsg="+uMsg+", uParam="+uParam+", lParam="+lParam);
}
else if (uMsg == WTSAPI.WTS_SESSION_CHANGE)
{
System.out.println("WTS_SESSION_CHANGE Event: hWnd="+hwnd+", uMsg="+uMsg+", uParam="+uParam+", lParam="+lParam);
}
//Call the window's actual WndProc so the events get processed.
return User32.INSTANCE.CallWindowProc(prevWndProc, hWnd, uMsg, uParam, lParam);
}
};
//Set the WndProc function to use our callback listener instead of the window's one.
int result = User32.INSTANCE.SetWindowLong(hwnd, User32.GWL_WNDPROC, wndProcCallbackListener);
但是,我的问题是当我为父窗口调用 GetWindowLong() 时(我的第一行代码)我得到的指针为 0,这表明该函数未成功完成。随后调用 GetLastError() 并快速检查错误代码给我一个“访问被拒绝”错误。当然,这是合乎逻辑的,因为我试图从自己的线程访问另一个线程的 WNDPROC 地址,但我想知道是否有任何方法(当然应该有)来规避它。
有什么指点吗? (双关语)