@@ -7,8 +7,17 @@ use std::fmt::{Debug, Error as FmtError, Formatter};
7
7
use std:: mem:: forget;
8
8
use std:: ops:: Deref ;
9
9
use std:: ptr:: { null_mut, NonNull } ;
10
- use winapi:: um:: unknwnbase:: IUnknown ;
11
- use winapi:: Interface ;
10
+ use winapi:: {
11
+ shared:: {
12
+ minwindef:: DWORD ,
13
+ winerror:: HRESULT ,
14
+ } ,
15
+ um:: {
16
+ combaseapi,
17
+ unknwnbase:: IUnknown ,
18
+ }
19
+ } ;
20
+ use winapi:: { Class , Interface } ;
12
21
13
22
// ComPtr to wrap COM interfaces sanely
14
23
#[ repr( transparent) ]
@@ -57,6 +66,34 @@ impl<T> ComPtr<T> {
57
66
}
58
67
}
59
68
}
69
+ /// Instantiate the given class type.
70
+ ///
71
+ /// This is a shorthand for [`CoCreateInstance`], using `C` and `T`'s `GUID`s for `rclsid` and
72
+ /// `riid`, respectively. `cls_context` is passed into [`CoCreateInstance`]'s `dwClsContext`
73
+ /// parameter, so see that documentation for details on what to pass for it.
74
+ ///
75
+ /// Returns `Err(HRESULT)` if the call to [`CoCreateInstance`] failed.
76
+ ///
77
+ /// [`CoCreateInstance`]: https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance
78
+ pub fn new_class < C : Class > ( cls_context : DWORD ) -> Result < ComPtr < T > , HRESULT >
79
+ where T : Interface
80
+ {
81
+ unsafe {
82
+ let mut raw_ptr: * mut T = null_mut ( ) ;
83
+ let hr = combaseapi:: CoCreateInstance (
84
+ & C :: uuidof ( ) ,
85
+ null_mut ( ) ,
86
+ cls_context,
87
+ & T :: uuidof ( ) ,
88
+ & mut raw_ptr as * mut _ as * mut _ ,
89
+ ) ;
90
+ if hr == 0 {
91
+ Ok ( ComPtr :: from_raw ( raw_ptr) )
92
+ } else {
93
+ Err ( hr)
94
+ }
95
+ }
96
+ }
60
97
/// Casts up the inheritance chain
61
98
pub fn up < U > ( self ) -> ComPtr < U >
62
99
where
@@ -77,7 +114,7 @@ impl<T> ComPtr<T> {
77
114
unsafe { & * ( self . as_raw ( ) as * mut IUnknown ) }
78
115
}
79
116
/// Performs QueryInterface fun.
80
- pub fn cast < U > ( & self ) -> Result < ComPtr < U > , i32 >
117
+ pub fn cast < U > ( & self ) -> Result < ComPtr < U > , HRESULT >
81
118
where
82
119
U : Interface ,
83
120
{
0 commit comments