Comments (5)
This has been supported for years. 😀
from com-rs.
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.
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.
@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.
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)
- Parameters of class implementation should be borrowed, not owned
- Rust Analyzer can't recognize the IUnknown HOT 7
- Compatibility with Windows SDK / winapi crate HOT 1
- Drop of COM class not working in webview2 example?
- Future of this crate HOT 6
- Interfaces nested more than 2 levels deep do not compile HOT 1
- Does class! support generic types? HOT 1
- docs.rs failed to build com-0.4.0 HOT 1
- Request to add an "Accessible" example
- Question: How do I get interface vptr from a struct? HOT 2
- com::interfaces! is not clean of clippy warnings HOT 1
- New release? HOT 2
- Generate rust code from IDL/TLB/COM dll? HOT 14
- How to use com-rs? HOT 1
- Could not find `class` in `com` HOT 3
- Support local (not/temporarily heap allocated) class instances
- Incompatible with Windows-rs HOT 4
- This repo is missing important files
- any way to use activex dll?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from com-rs.