How to use a COM VARIANT in Rust winapi programming?

Viewed 947

I'm trying to convert the C++ COM code for TaskScheduler to Rust and am stuck with the VARIANT argument of ITaskService::Connect:

extern crate winapi;
use winapi::{
    ctypes::c_void,
    shared::{
        guiddef::{GUID, REFCLSID, REFIID},
        ntdef::{HRESULT, NULL},
        rpcdce::{RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE},
        winerror::FAILED,
        wtypes::VARENUM,
        wtypesbase::CLSCTX_INPROC_SERVER,
    },
    um::{
        combaseapi::{CoCreateInstance, CoInitializeEx, CoInitializeSecurity, CoUninitialize},
        oaidl,
        objbase::COINIT_MULTITHREADED,
        taskschd::{ITaskService, TaskScheduler},
    },
    Class, Interface,
}; // 0.3.7

fn main() {
    //  Create an instance of the Task Service.
    let mut p_service: *mut ITaskService = unsafe { std::mem::zeroed() };

    let hr = unsafe {
        CoCreateInstance(
            &TaskScheduler::uuidof(),
            std::ptr::null_mut(),
            CLSCTX_INPROC_SERVER,
            &ITaskService::uuidof(),
            &mut p_service as *mut *mut ITaskService as *mut *mut c_void,
        )
    };

    // [...]
    // //  Connect to the task service.
    // let hr = pService->Connect(_variant_t(), _variant_t(),
    //     _variant_t(), _variant_t());

    let hr = unsafe {
        (&mut *p_service).Connect(
            oaidl::VARIANT { /* VT_EMPTY HERE */ },
            oaidl::VARIANT { /* VT_EMPTY HERE */ },
            oaidl::VARIANT { /* VT_EMPTY HERE */ },
            oaidl::VARIANT { /* VT_EMPTY HERE */ },
        )
    };
}

The docs say:

_variant_t( ) Constructs an empty _variant_t object, VT_EMPTY.

I found no mention on how to use the variant as stated in the README:

Use std::mem::zeroed() to create an instance of the union, and then assign the value you want using one of the variant methods.

When checking the docs for "variant", there's no _variant_t fn on the list, so not sure what it is called in Rust

Errors

field `0` of struct `winapi::um::oaidl::VARIANT_n1` is private

field `0` is private

for

  oaidl::VARIANT {
    n1: oaidl::VARIANT_n1 {
      0: [0, 0, 0]
    }
  }
2 Answers

Initializing unions is explained in winapi's README:

How do I create an instance of a union?

Use std::mem::zeroed() to create an instance of the union, and then assign the value you want using one of the variant methods.

For instance, for VARIANT_n1, the methods are n2, n2_mut, decVal and decVal_mut. You will naturally need to use a _mut method to assign a value.

Note that, starting with winapi 0.3.7, you can also use Default::default() to obtain a zeroed union instead of using the unsafe std::mem::zeroed(). You need to enable the impl-default feature on the winapi crate for the Default trait to be implemented.

You can construct a variant with

use winapi::um::oaidl::VARIANT;

fn doStuff() {
  unsafe {
    let mut variant: VARIANT = std::mem::zeroed();
    // set values here
  }
}

variant.n1.n2().vt contains the type (e.g. winapi::shared::wtypes::VT_BOOL or winapi::shared::wtypes::VT_EMPTY, msdn describes the possible values here), the variant.n1.n2().n3 union contains the value.

Related