I am trying to make a clipboard manager for windows in rust and am using winapi-rs
crate to use winapi.
My current implementation is as follows:
src/clipboard.rs:
use std::mem::zeroed;
use std::ptr;
use winapi::shared::windef::HWND;
use winapi::um::libloaderapi::GetModuleHandleW;
use winapi::um::winuser::{
AddClipboardFormatListener,
RemoveClipboardFormatListener,
CreateWindowExW,
RegisterClassW,
WNDCLASSW,
WM_CREATE,
WM_DESTROY,
WM_CLIPBOARDUPDATE,
HWND_MESSAGE,
DefWindowProcW
};
use winapi::shared::minwindef::{LRESULT, UINT, WPARAM, LPARAM, BOOL};
static mut ADDED_LISTENER: BOOL = 0;
unsafe extern "system" fn callback_proc(h_wnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
println!("called");
(match msg {
WM_CREATE => {
ADDED_LISTENER = AddClipboardFormatListener(h_wnd);
if ADDED_LISTENER == 1 { 0 } else { -1 }
}
WM_DESTROY => {
if ADDED_LISTENER == 1 {
RemoveClipboardFormatListener(h_wnd);
ADDED_LISTENER = 0;
}
0
}
WM_CLIPBOARDUPDATE => {
println!("clipboard updated.");
0
},
_ => DefWindowProcW(h_wnd, msg, wparam, lparam)
}) as LRESULT
}
pub fn run() {
unsafe {
let hinst = GetModuleHandleW(ptr::null_mut());
let wnd_class = WNDCLASSW {
hInstance: hinst,
lpfnWndProc: Some(callback_proc),
lpszClassName: &[67 as u16, 108 as u16, 105 as u16, 112 as u16, 98 as u16, 111 as u16, 97 as u16, 114 as u16, 100 as u16, 77 as u16, 101 as u16, 115 as u16, 115 as u16, 97 as u16, 103 as u16, 101 as u16, 87 as u16, 105 as u16, 110 as u16, 100 as u16, 111 as u16, 119 as u16] as *const u16,
..zeroed::<WNDCLASSW>()
};
let class_atom = RegisterClassW(&wnd_class as *const WNDCLASSW);
CreateWindowExW(class_atom.into(), wnd_class.lpszClassName, ptr::null(), 0, 0, 0, 0, 0, HWND_MESSAGE, ptr::null_mut(), hinst, ptr::null_mut());
}
loop { } // Added this as the code was exiting immediately
}
src/main.rs:
mod clipboard;
fn main() {
clipboard::run();
}
I got help from a c++ impl from this post from stackoverflow and this implementation in python.
But here I am not getting any output nor any error messages.
Note: using Rust v1.66.0-stable