com_rs/unknown.rs
1// Copyright (c) 2016 com-rs developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::os::raw::c_void;
10
11use super::{AsComPtr, HResult, IID};
12
13/// Base interface for all COM types.
14///
15/// None of the methods on this struct should be called directly,
16/// use [`ComPtr`](struct.ComPtr.html) instead.
17
18#[derive(Debug)]
19#[repr(C)]
20pub struct IUnknown {
21 vtable: *const IUnknownVtbl
22}
23
24#[allow(missing_debug_implementations)]
25#[repr(C)]
26#[doc(hidden)]
27pub struct IUnknownVtbl {
28 query_interface: extern "stdcall" fn(
29 *const IUnknown, &IID, *mut *mut c_void) -> HResult,
30 add_ref: extern "stdcall" fn(*const IUnknown) -> u32,
31 release: extern "stdcall" fn(*const IUnknown) -> u32
32}
33
34extern {
35 static IID_IUnknown: IID;
36}
37
38impl IUnknown {
39 /// Retrieves pointers to the supported interfaces on an object.
40 /// Use [`ComPtr::from`](struct.ComPtr.html#method.from) instead.
41 pub unsafe fn query_interface(&self, iid: &IID, object: *mut *mut c_void)
42 -> HResult {
43 ((*self.vtable).query_interface)(self, iid, object)
44 }
45
46 /// Increments the reference count for an interface on an object.
47 /// Should never need to call this directly.
48 pub unsafe fn add_ref(&self) -> u32 {
49 ((*self.vtable).add_ref)(self)
50 }
51
52 /// Decrements the reference count for an interface on an object.
53 /// Should never need to call this directly.
54 pub unsafe fn release(&self) -> u32 {
55 ((*self.vtable).release)(self)
56 }
57}
58
59unsafe impl AsComPtr<IUnknown> for IUnknown { }
60
61unsafe impl ::ComInterface for IUnknown {
62 #[doc(hidden)]
63 type Vtable = IUnknownVtbl;
64 fn iid() -> ::IID { unsafe { IID_IUnknown } }
65}