Error while trying to get PID with FindWindowA and GetWindowThreadProcessId

453 Views Asked by At

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
    }
}
1

There are 1 best solutions below

0
On

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.