hey folks can anyone help me a bit? I have the problem w/ this snippet:
fn main() -> windows::Result<()> {
unsafe {
let h_bmp = LoadBitmapA(None,"C:\\Users\\grass\\Desktop\\codes\\Rust\\catso\\src\\OIP.jpg"); // does that load bitmap?
let hdc = CreateCompatibleDC(HDC::NULL); // It suppose to have default 1px monochrome Bitmap, does it change on select object?
SelectObject(hdc,h_bmp); // Do I need to create bitmap in the hdc and fill it from file smh?
let time_back: SystemTime = SystemTime::now();
while SystemTime::now().duration_since(time_back).unwrap().as_secs() < 10 as u64 {
BitBlt(HDC::NULL, 0, 0, 474, 266, hdc, 0, 0, ROP_CODE(13)); // is ROP_CODE(13) refers to SRCCOPY?
}
println!("DELETED: {:?}",DeleteDC(hdc));
}
Ok(())
}
I think that there are several places where it can go wrong so I commented them. I don't get compiler errors so I'm bit in the dark
Update: I`ve realized that I can use return type of BitBlt to do further debugging so I adjusted the code like so
println!("succeeded? : {:?}",BitBlt(HDC::NULL, 0, 0, 474, 266, hdc, 0, 0, ROP_CODE(13)));
and it yielded BOOL(0), which I assume is equivalent to false, and hence I assume the issue is with BitBlt? Can it be that I need some extra arguments?
Sequentially I tried using GetLastError and found out that I have invalid handle error (6).
Update: So I tried printing the results of LoadBitmapA and it returns null, I've looked over generated docs and realized I had to change arguments to
let h_bmp = LoadBitmapA(HINSTANCE::NULL,PSTR(bmp_name));
Sadly I'm not sure what bmp_name should be here, as windows API webpage seem to indicate its a string but rust docs claim its a mut u8. I do know that u8 is a numeric used in utf_8 but it seems that it needs just one mutable u8 rather then an array as the only thing it does error out on is
let h_bmp = LoadBitmapA(HINSTANCE::NULL,PSTR(&mut (2 as u8)));
.
Update: Ok, so as suggested by Aiden4 in comments I looked at Cstring documentation but I didn't find a way to convert it into *mut u8. Closes I found was
let h_bmp = LoadBitmapA(HINSTANCE::NULL,
PSTR(CString::new("C:\\Users\\grass\\Desktop\\codes\\Rust\\catso\\src\\OIP.jpg").unwrap().into_raw()));
which resulted in the *mut i8, casting it with
as *mut u8
does work but doesn't resolve the issue while not casting gives a compiler error.
Update: So after being pointed out that jpeg file will not work, and that its enough to pass the string slice as well as advice to append \0 to the end of the slice I've adjusted my snippet accordingly:
let h_bmp = LoadBitmapA(HINSTANCE::NULL,"C:\\Users\\grass\\Desktop\\codes\\Rust\\catso\\src\\OIP.bmp\0");
Which sadly still doesnt show the bitmap.
Here is a bitmap file: