How can I set HINSTANCE (Rust windows API) to NULL

144 views Asked by At

I'm using windows-rs (version 0.52.0). I wanted to capture certain events (in my case, a keyboard input) for the entire system (by that I mean not limited to a single window thread). To achieve it, I've set the last argument, dwThreadId to 0 as the Microsoft learn page states:

The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

Which from what I understand hMod is not needed in my case, therefor I wanted to set it to NULL.

The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

Code below is a minimum replication for this problem

use windows::Win32::{UI::WindowsAndMessaging::{SetWindowsHookExA, WH_MOUSE}, Foundation::{WPARAM, LRESULT, LPARAM, HINSTANCE}};

unsafe extern "system" fn callback(code: i32, param: WPARAM, lpdata: LPARAM) -> LRESULT {
    LRESULT(0)
}

fn main() {
    unsafe {
        println!("{:?}", SetWindowsHookExA(WH_MOUSE, Some(callback), std::ptr::null_mut::<HINSTANCE>(), 0));
    }
}

And this is the error I get when running the code.

error[E0277]: the trait bound `*mut HINSTANCE: CanInto<HINSTANCE>` is not satisfied
    --> src\main.rs:8:70
     |
8    |         println!("{:?}", SetWindowsHookExA(WH_MOUSE, Some(callback), std::ptr::null_mut::<HINSTANCE>(), 0));
     |                          -----------------                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `CanInto<HINSTANCE>` is not implemented for `*mut HINSTANCE`
     |                          |
     |                          required by a bound introduced by this call
     |
     = help: the trait `CanInto<HMODULE>` is implemented for `HINSTANCE`
     = note: required for `*mut HINSTANCE` to implement `IntoParam<HINSTANCE, CopyType>`

How can I set the hMod to be NULL?

I've tried setting the hMod variable to std::ptr::null_mut::<HINSTANCE>() and HINSTANCE(0).

The first one is a problem with requirement of the IntoParam trait which I couldn't find much information for. The second one returns an error result of Err(Error { code: HRESULT(0x80070594), message: "Cannot set nonlocal hook without a module handle." }).

0

There are 0 answers