Error while creating SAFEARRAY using Windows Rust crate.

Sanil Raut 0 Reputation points Microsoft Employee
2023-09-11T17:39:55.62+00:00

Hi,
I am trying to create a safearray of type BSTR and add one BSTR Variant to it with windows rust crate: https://github.com/microsoft/windows-rs (windows = "0.48.0")

pub fn createSafeArray() {
    unsafe {
        let mut var = VARIANT::default();
        (*var.Anonymous.Anonymous).vt = VT_BSTR;
        (*var.Anonymous.Anonymous).Anonymous.bstrVal =
                ManuallyDrop::new(BSTR::from("3"));

        // Create SAFEARRAY of type BSTR
        /*
        let safearray = SafeArrayCreate(
            Com::VT_BSTR,
            1,
            &SAFEARRAYBOUND {
                cElements: 1,
                lLbound: 0,
            },
        ); */

        let safearray = SafeArrayCreateVector(VT_BSTR, 0, 1);

        // Add to safe array
        let res = SafeArrayPutElement(
            safearray,
            0 as _,
            &test as *const Com::VARIANT as *const c_void,
        );
        
        println!("Res: {:#?}", res);

    }
}

And getting the following error

Err(
    Error {
        code: HRESULT(0x80070057),
        message: "The parameter is incorrect.",
    },
)

Is there anything wrong or missing in the above code?

Windows development Windows API - Win32
{count} votes

1 answer

Sort by: Most helpful
  1. Kenny Kerr 0 Reputation points Microsoft Employee
    2023-09-12T14:23:42.59+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.