#COM #variant #interop #safearray


Crate to manage conversions to/from SAFEARRAY, VARIANT, and BSTR data structures in COM interop

5 releases

0.1.4 Oct 9, 2018
0.1.3 Oct 9, 2018
0.1.2 Oct 9, 2018
0.1.1 Oct 8, 2018
0.1.0 Oct 8, 2018

#82 in FFI

Download history 26/week @ 2018-10-10

8 downloads per month

MIT license



Extracted types to wrap common Windows data structures - SAFEARRAY, VARIANT



A module to handle conversion to and from common OLE/COM types - VARIANT, SAFEARRAY, and BSTR.

This module provides some convenience types as well as traits and trait implementations for built in rust types - u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, String, bool to and from VARIANT structures. In addition, Vec<T> can be converted into a SAFEARRAY where T is one of i8, u8, u16, i16, u32, i32, String, f32, f64, bool.

As well, IUnknown, IDispatch pointers can be marshalled back and forth across boundaries.

There are some convenience types provided for further types that VARIANT/SAFEARRAY support: SCode, Int, UInt, Currency, Date, DecWrapper, VtEmpty, VtNull

The relevant traits to use are: BStringExt, SafeArrayElement, SafeArrayExt, and VariantExt


An example of how to use the module:

extern crate oaidl;
extern crate widestring;
extern crate winapi;

use widestring::U16String;
use winapi::um::oaidl::VARIANT;

use oaidl::{BStringExt, IntoVariantError, VariantExt};

//simulate an FFI function
unsafe fn c_masq(s: *mut VARIANT, p: *mut VARIANT) {
    println!("vt of s: {}", (*s).n1.n2_mut().vt);
    println!("vt of p: {}", (*p).n1.n2_mut().vt);
    //free the memory allocated for these VARIANTS
    let s = *s;
    let p = *p;

fn main() -> Result<(), IntoVariantError> {
    let mut u = 1337u32;
    let mut sr = U16String::from_str("Turing completeness.");
    let p = VariantExt::<u32>::into_variant(u)?;
    let s = VariantExt::<*mut u16>::into_variant(sr)?;
    unsafe {c_masq(s.as_ptr(), p.as_ptr())};


~23K SLoC