4个版本

0.1.3 2024年3月17日
0.1.2 2024年3月10日
0.1.1 2024年3月10日
0.1.0 2024年3月10日

#17 in #com-interface


wvwasi 中使用

MIT/Apache

18KB
216 代码行

为wvwasi crate提供的宏。

wvwasi_macro::create_type_info_crate

创建一个名为 create_disp_type_info 的方法。create_disp_type_info 方法返回以下 trait 的 ITypeInfo。

示例

#[wvwasi_macro::create_type_info_crate(ISyncIPCHandler_TypeInfo)]
#[windows::core::interface("094d70d6-5202-44b8-abb8-43860da5aca2")]
unsafe trait ISyncIPCHandler: windows::core::IUnknown {
  unsafe fn test(&self, message: windows::core::BSTR) -> windows::core::BSTR;
  unsafe fn test2(&self) -> u16;
}

#[windows::core::implement(windows::Win32::System::Com::IDispatch, ISyncIPCHandler)]
struct SyncIPCHandler {
  type_info: ITypeInfo,
}

impl ISyncIPCHandler_TypeInfo for SyncIPCHandler {}
impl ISyncIPCHandler_Impl for SyncIPCHandler {
  unsafe fn test(&self, message: windows::core::BSTR) {
    windows::core::BSTR::default()
  }
  unsafe fn test2(&self) -> u16 {
    123u16
  }
}
impl IDispatch_Impl for SyncIPCHandler {
  fn GetTypeInfoCount(&self) -> windows::core::Result<u32> {
    Ok(1)
  }
  fn GetTypeInfo(&self, itinfo: u32, _lcid: u32) -> windows::core::Result<ITypeInfo> {
    if itinfo != 0 {
      Err(windows::Win32::Foundation::DISP_E_BADINDEX.into())
    } else {
      Ok(self.type_info.clone())
    }
  }
  fn GetIDsOfNames(
    &self,
    riid: *const windows::core::GUID,
    rgsznames: *const windows::core::PCWSTR,
    cnames: u32,
    _lcid: u32,
    rgdispid: *mut i32,
  ) -> windows::core::Result<()> {
    unsafe {
      if riid.is_null() || *riid != windows::core::GUID::default() {
        Err(windows::Win32::Foundation::DISP_E_UNKNOWNINTERFACE.into())
      } else {
        windows::Win32::System::Ole::DispGetIDsOfNames(&self.type_info, rgsznames, cnames, rgdispid)
      }
    }
  }
  fn Invoke(
    &self,
    dispidmember: i32,
    riid: *const windows::core::GUID,
    _lcid: u32,
    wflags: DISPATCH_FLAGS,
    pdispparams: *const DISPPARAMS,
    pvarresult: *mut VARIANT,
    pexcepinfo: *mut EXCEPINFO,
    puargerr: *mut u32,
  ) -> windows::core::Result<()> {
    unsafe {
      if riid.is_null() || *riid != windows::core::GUID::default() {
        Err(windows::Win32::Foundation::DISP_E_UNKNOWNINTERFACE.into())
      } else {
        let this: ISyncIPCHandler = self.cast()?;
    
        let mut dispparams = if pdispparams.is_null() {
          None
        } else {
          Some(*pdispparams)
        };
        let pdispparams_mut = dispparams
          .as_mut()
          .map(|x| x as _)
          .unwrap_or(std::ptr::null_mut());
    
        windows::Win32::System::Ole::DispInvoke(
          windows::core::Interface::as_raw(&this),
          &self.type_info,
          dispidmember,
          wflags.0,
          pdispparams_mut,
          pvarresult,
          pexcepinfo,
          puargerr,
        )
      }
    }
  }
}

fn main() {
  let type_info = SyncIPCHandler::create_disp_type_info();
  let host_object = SyncIPCHandler { type_info };
}

依赖关系

~127MB
~2M SLoC