Home > database >  How to create a IWICImagingFactory instance in winapi-rs
How to create a IWICImagingFactory instance in winapi-rs

Time:02-03

I try this way,

    let mut factory: *mut IWICImagingFactory = std::ptr::null_mut();
    let hr = unsafe {
        CoCreateInstance(
            &CLSID_WICImagingFactory,
            std::ptr::null_mut(),
            CLSCTX_INPROC_SERVER,
            &IWICImagingFactory::uuidof(),
            &mut factory as *mut *mut _ as *mut *mut _,
        )
    };
    assert!(factory.is_null());

but the factory pointer still is null, the hr is -2147221008, I don't know what that means.

CodePudding user response:

The error code -2147221008 (or 0x800401F0) translates to CO_E_NOTINITIALIZED. The documentation for CoInitializeEx explains why you'd receive this error:

You need to initialize the COM library on a thread before you call any of the library functions [...]. Otherwise, the COM function will return CO_E_NOTINITIALIZED.

To instantiate an IWICImagingFactory interface implementation you'll have to initialize COM on the calling thread first:

let hr = unsafe { CoInitialize(std::ptr::null_mut()) };
// Handle errors

As a note, unless you have a specific reason to use the winapi crate, go with the windows crate instead. It makes COM programming in Rust a lot more convenient.

  • Related