-1

I'm trying to get the PID of a program in rust using the Windows crate, with the FindWindowA and GetWindowThreadProcessId functions. My problem is that GetWindowThreadProcessId fails with error 1400.

fn main(){
    unsafe{
        let process_name: PCSTR = windows::s!("ac_client.exe");         
        let window = windows::Win32::UI::WindowsAndMessaging::FindWindowA(None, process_name);
        let error = windows::Win32::Foundation::GetLastError();
        println!(" {:?}", error);  //0

        let mut pId= 0;
        
        windows::Win32::UI::WindowsAndMessaging::GetWindowThreadProcessId(window, Some(&mut pId));

        let error = windows::Win32::Foundation::GetLastError();
        println!("{:?}", error); // 1400
    }
}
johnsc
  • 21
  • 5
  • 1
    [`GetWindowThreadProcessId`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowthreadprocessid) doesn't report errors by setting the calling thread's last error. Whatever `GetLastError` returns is meaningless. – IInspectable Nov 13 '22 at 13:10
  • @IInspectable - `GetWindowThreadProcessId` set last error. end 1400 (`ERROR_INVALID_WINDOW_HANDLE`) say for self – RbMm Nov 13 '22 at 13:19
  • Not according to the documentation. At any rate, the second argument to [`FindWindowA`](https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/WindowsAndMessaging/fn.FindWindowA.html) is the window name. The code appears to be passing the name of the executable, which is unlikely to be correct. You would need to use a tool like Spy++ to find the window title (and, ideally, the class name as well). – IInspectable Nov 13 '22 at 13:24
  • Not according to the documentation, but by fact this api set last error – RbMm Nov 13 '22 at 13:46
  • @RbMm whether it does or not is undefined. It is not documented behavior, so you can't rely on it. Microsoft could decide to change it in the future. – Remy Lebeau Nov 13 '22 at 17:27
  • @RemyLebeau - i not say that we need rely on it. but as debug diagnostic - this is good. – RbMm Nov 13 '22 at 17:29
  • @RbMm even as a debug diagnostic, it is still undefined whether it will actually be meaningful of not. Today, it is. Tomorrow, it might not be. That's the point. – Remy Lebeau Nov 13 '22 at 18:00
  • @RemyLebeau i do not think so. the error code helps to understand (usually in more complex cases) the root of the problem. whether it is documented or not. and usually these codes are quite stable (do not change in new versions). in a particular situation, the error indicates an incorrect window handle. however, for such a simple api, it is clear and so – RbMm Nov 13 '22 at 18:08
  • @RbMm we'll just have to agree to disagree. Enough said. – Remy Lebeau Nov 13 '22 at 18:23

1 Answers1

0

error 1400: ERROR_INVALID_WINDOW_HANDLE Invalid window handle.

It seems that you used a wrong window handle. I suggest you could try to check the window handle. And you should make sure that the thread is valid before you call the GetWindowThreadProcessId.

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20