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

How can I manage windows of applications opened using Win32 API (Notepad, Word, Outlook, Chrome etc.)

我们如何管理我们 运行 使用 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

答案是您不能将 window 过程 cross-process 替换为 SetWindowLongPtr 并且 SetClassLongPtr.

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

Calling SetClassLongPtr with the GCLP_WNDPROC index creates a subclass of the window class that affects all windows subsequently created 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 the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the process the calling thread resides in.

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

如果要监控window条消息,建议使用SetWindowsHookEx。 您可以阅读此文档:Using Hooks

您也可以 create your own window,使用相同的 WNDCLASSEX(目标中的 GetClassLongPtr window)和样式(目标中的 GetWindowLongPtr window)。

或者你已经知道自己在做什么,试试dll注入。