gpt4 book ai didi

go - 如何管理使用 Win32 API(记事本、Word、Outlook、Chrome 等)打开的应用程序窗口

转载 作者:行者123 更新时间:2023-12-01 22:45:45 28 4
gpt4 key购买 nike

我们如何管理使用 Win32 api 运行的应用程序的 wndproc 函数?我使用的软件语言是 Go。
我尝试了不止一种方法,但无法做到。你能帮我吗?
I am learning the HANDLE (HWND) value of notepad application with Microsoft Spy ++
Then I watch the changes on Notepad ++ over SPY ++.
就像使用 Spy++ 一样,请帮我管理我编写的自定义 WNPROC 函数。

func main() {

hwnd := w32.HWND(3736818)
go SetWindowLongTest(w32.HWND(hwnd))
time.Sleep(99999 * time.Second)
}


func SetWindowLongTest(hwnd w32.HWND) {
result, err := win.SetWindowLongPtr(win.HWND(hwnd), win.GWL_WNDPROC, syscall.NewCallback(MyNewWndProc))
if err != nil {
fmt.Println("SetWindowLongPtr", err)
}
fmt.Println(result)
}



func MyNewWndProc(hwnd w32.HWND, uMsg uint, wParam w32.WPARAM, lParam w32.LPARAM) uintptr {
fmt.Println(uMsg)
fmt.Println("myNewWndProc", hwnd)
return 0
}

Result:
PS C:\Users\Cingozr\go\src> go run .\main.go
SetWindowLongPtr Access is denied.
0
方法二:
func main() {

hwnd := w32.HWND(3736818)
go SetClassLongTest(w32.HWND(hwnd))
time.Sleep(99999 * time.Second)
}

func SetClassLongTest(hwnd w32.HWND) {
result, err := w32.SetClassLongPtrW(hwnd, -24, syscall.NewCallback(MyNewWndProc))
if err != nil {
fmt.Println("SetClassLongPtrW Err", err)
}
fmt.Println("SetClassLongPtrW", result)

}

func MyNewWndProc(hwnd w32.HWND, uMsg uint, wParam w32.WPARAM, lParam w32.LPARAM) uintptr {
fmt.Println(uMsg)
fmt.Println("myNewWndProc", hwnd)
return 0
}

Result:
PS C:\Users\Cingozr\go\src> go run .\main.go
SetClassLongPtrW Err Access is denied.
SetClassLongPtrW 0

最佳答案

答案是你不能用 SetWindowLongPtr 替换窗口过程跨进程。和
SetClassLongPtr .

Calling SetWindowLongPtr with the GWLP_WNDPROC index creates asubclass of the window class used to create the window. Anapplication can subclass a system class, but should not subclass awindow class created by another process.

Calling SetClassLongPtr with the GCLP_WNDPROC index creates asubclass of the window class that affects all windows subsequentlycreated with the class. An application can subclass a system class,but should not subclass a window class created by another process.


另外,SetWindowLongPtr的hwnd参数也有UIPI限制:

The SetWindowLongPtr function fails if the process that owns thewindow specified by the hWnd parameter is at a higher processprivilege in the UIPI hierarchy than the process the calling threadresides in.

Windows XP/2000: The SetWindowLongPtr function fails if the windowspecified by the hWnd parameter does not belong to the same process asthe calling thread.


推荐使用 SetWindowsHookEx , 如果你想监 window 口消息。
您可以阅读此文档: Using Hooks
你也可以 create your own window , 与 WNDCLASSEX 相同(来自目标窗口的 GetClassLongPtr)和样式(来自目标窗口的 GetWindowLongPtr)。
或者你已经知道你在做什么,试试 dll 注入(inject)。

关于go - 如何管理使用 Win32 API(记事本、Word、Outlook、Chrome 等)打开的应用程序窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63436731/

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