How can we manage the wndproc function of the applications we run using the Win32 api? The software language I use is Go.
I tried more than one method but couldn't do it. Can you help?
I am learning the HANDLE (HWND) value of notepad application with Microsoft Spy ++
Then I watch the changes on Notepad ++ over SPY ++.
Just like with Spy ++, please help me manage the custom WNPROC function I wrote.
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
Method 2:
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
The answer is you cannot replace the window procedure cross-process with
SetWindowLongPtr
andSetClassLongPtr
.In addition, the hwnd parameter of SetWindowLongPtr also has UIPI restrictions:
It is recommended to use
SetWindowsHookEx
, if you want to monitor window messages. You could read this document: Using HooksYou could also create your own window, with the same
WNDCLASSEX
(GetClassLongPtr from the target window) and style(GetWindowLongPtr from the target window).Or you've already known what you are doing, try dll injection.