GithubHelp home page GithubHelp logo

Comments (5)

kennykerr avatar kennykerr commented on June 2, 2024 1

This has been supported for years. 😀

from com-rs.

danielegarciav avatar danielegarciav commented on June 2, 2024

This is an issue I came across as well. I ended up making a really quick and dirty AbiWrapper struct. I'm not even sure it's correct, but it works for my use case (FFI with windows::Windows::Win32::System::Variant::VARIANT and windows::Windows::Win32::UI::Accessibility::UIA_PROPERTY_ID).

#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
struct AbiWrapper<T: Clone>(T);
unsafe impl<T: Clone> com::AbiTransferable for AbiWrapper<T> {
  type Abi = T;
  fn get_abi(&self) -> Self::Abi {
    self.0.clone()
  }
  fn set_abi(&mut self) -> *mut Self::Abi {
    &mut self.0
  }
}

I'm sure someone else can polish this into something more correct and polished.

For completeness, here's the IUIAutomationPropertyChangedEventHandler COM interface and class definitions I wrote (had to rewrite the interface using Microsoft's com since the windows crate doesn't support producing COM classes yet):

com::interfaces! {
  #[uuid("40CD37D4-C756-4B0C-8C6F-BDDFEEB13B50")]
  unsafe interface IChangeEventHandler: IUnknown {
    fn HandlePropertyChangedEvent(
      &self,
      sender: *mut std::ffi::c_void,
      propertyid: AbiWrapper<UIA_PROPERTY_ID>,
      newvalue: AbiWrapper<VARIANT>,
    ) -> ::windows::core::HRESULT;
  }
}

com::class! {
  pub class EventHandler: IChangeEventHandler {}

  impl IChangeEventHandler for EventHandler {
    fn HandlePropertyChangedEvent(&self, _sender: *mut std::ffi::c_void, _propertyid: AbiWrapper<UIA_PROPERTY_ID>, newvalue: AbiWrapper<VARIANT>) -> windows::core::HRESULT {
      let new_scroll_value = unsafe { newvalue.0.Anonymous.Anonymous.Anonymous.dblVal };
      tracing::debug!("scroll event: {}", new_scroll_value);
      windows::core::Result::Ok(()).into()
    }
  }
}

from com-rs.

MarijnS95 avatar MarijnS95 commented on June 2, 2024

since the windows crate doesn't support producing COM classes yet

are you sure? Defining your own interface in the same way the generator does (if you cannot produce a winmd which is extremely trivial based on a C++ header) should be easy, and their #[implement] attribute should then allow you to produce class instances?

from com-rs.

danielegarciav avatar danielegarciav commented on June 2, 2024

@MarijnS95 @kennykerr sorry y'all, I just found about windows_implement few hours after that comment 😅. Absolutely love how simple it is to define a class for an existing interface, rust_analyzer wrote the impl block for me and everything:

#[windows_implement::implement(IUIAutomationPropertyChangedEventHandler)]
struct ScrollEventHandler {
  /* fields */
}

impl IUIAutomationPropertyChangedEventHandler_Impl for ScrollEventHandler {
  fn HandlePropertyChangedEvent(
    &self,
    sender: Option<&IUIAutomationElement>,
    propertyid: UIA_PROPERTY_ID,
    newvalue: &VARIANT,
  ) -> windows::core::Result<()> {
    /* impl */
  }
}

I found out about windows_implement from surfing through github issues, AFAIK it's not documented yet.

One thing I don't understand is, I was under the impression that COM instances must be heap allocated and pinned in memory. The implement macro adds From impls to convert from our custom struct into either our implemented interfaces or into a COM IUnknown, and those impls box the instance, but they don't pin it.

The safety docs for the cast method say that the object must be heap allocated and pinned using the provided methods, but the provided methods don't pin. Is that expected? The older com crate does use Pin<Box<T>> (link).

from com-rs.

MarijnS95 avatar MarijnS95 commented on June 2, 2024

AFAIK it's not documented yet.

Exactly, ran into this while writing the above reply and filed an issue for it: microsoft/windows-rs#2694. windows-implement isn't reachable from the windows/windows-core docs but it barely matters as the crate/docs have no doc-comments themselves anyway.


but they don't pin it

Perhaps there's nothing happening to the contents of these boxes that would make the pointer move around? Pin<> is only an extra safeguard, not a thing that "makes everything work" (IMO).

from com-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.