This commit is contained in:
Ryan Levick 2019-09-20 18:13:30 +02:00
Родитель 4c30ac9157
Коммит 382ab03b7c
2 изменённых файлов: 11 добавлений и 21 удалений

Просмотреть файл

@ -37,11 +37,11 @@ pub trait IAnimal: IUnknown {
```
Short explanation: This generates the VTable layout for IUnknown and implements the trait on ComPtr so that it dereferences the correct function pointer entry within the VTable.
Short explanation: This generates the VTable layout for IUnknown and implements the trait on `com::InterfaceRc` so that it dereferences the correct function pointer entry within the VTable.
### Consuming a COM component
Interaction with COM components are always through an Interface Pointer (a pointer to a pointer to a VTable). We represent such an Interface Pointer with the `ComPtr` struct, which helps manage the lifetime of the COM component through IUnknown methods.
Interaction with COM components are always through an Interface Pointer (a pointer to a pointer to a VTable). We represent such an Interface Pointer with the `com::InterfaceRc` struct, which helps manage the lifetime of the COM component through IUnknown methods.
```rust
use com::runtime::ApartmentThreadedRuntime as Runtime;
@ -52,10 +52,10 @@ let runtime = Runtime::new().expect("Failed to initialize COM Library");
// Get a COM instance's interface pointer, by specifying
// - The CLSID of the COM component
// - The interface of the COM component that you want
// runtime.create_instance returns a ComPtr<dyn IAnimal> in this case.
// runtime.create_instance returns a InterfaceRc<dyn IAnimal> in this case.
let mut cat = runtime.create_instance::<dyn IAnimal>(&CLSID_CAT_CLASS).expect("Failed to get a cat");
// All IAnimal methods will be defined on ComPtr<T: IAnimal>
// All IAnimal methods will be defined on InterfaceRc<T: IAnimal>
cat.eat();
```

Просмотреть файл

@ -71,25 +71,15 @@ impl WindowsFileManager {
let user_field_two = 40;
let mut wfm = WindowsFileManager::allocate(user_field_one, user_field_two);
// Instantiate object to aggregate
// TODO: Should change to use safe ComPtr methods instead.
let mut unknown_file_manager = std::ptr::null_mut::<c_void>();
let hr = unsafe {
CoCreateInstance(
&CLSID_LOCAL_FILE_MANAGER_CLASS as REFCLSID,
&*wfm as *const _ as winapi::um::unknwnbase::LPUNKNOWN,
CLSCTX_INPROC_SERVER,
&IID_IUNKNOWN as REFIID,
&mut unknown_file_manager as *mut LPVOID,
let runtime = Runtime::new().expect("Failed to get runtime!");
let iunknown = runtime
.create_aggregated_instance::<dyn IUnknown, WindowsFileManager>(
&CLSID_LOCAL_FILE_MANAGER_CLASS,
&mut *wfm,
)
};
if failed(hr) {
println!("Failed to instantiate aggregate! Error: {:x}", hr as u32);
panic!();
}
.expect("Failed to instantiate aggregate!");
// Instantiate aggregate that exposes ILocalFileManager.
wfm.set_aggregate_ilocal_file_manager(unknown_file_manager as *mut IUnknownVPtr);
wfm.set_aggregate_ilocal_file_manager(iunknown);
wfm
}