How to use a COM VARIANT in Rust winapi programming?

柔情痞子 提交于 2020-07-09 09:42:39

问题


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]
    }
  }

回答1:


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.



来源:https://stackoverflow.com/questions/56571553/how-to-use-a-com-variant-in-rust-winapi-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!