| 1 |
// Copyright © 2017 winapi-rs developers |
| 2 |
// Licensed under the Apache License, Version 2.0 |
| 3 |
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 4 |
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 5 |
// All files in the project carrying such notice may not be copied, modified, or distributed |
| 6 |
// except according to those terms. |
| 7 |
|
| 8 |
#![allow(unused)] |
| 9 |
|
| 10 |
use crate::windows::{ |
| 11 |
winapi::{IUnknown, Interface}, |
| 12 |
windows_sys::{ |
| 13 |
CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE, |
| 14 |
S_OK, |
| 15 |
}, |
| 16 |
}; |
| 17 |
use std::{ |
| 18 |
convert::TryInto, |
| 19 |
ffi::{OsStr, OsString}, |
| 20 |
mem::ManuallyDrop, |
| 21 |
ops::Deref, |
| 22 |
os::windows::ffi::{OsStrExt, OsStringExt}, |
| 23 |
ptr::{null, null_mut}, |
| 24 |
slice::from_raw_parts, |
| 25 |
}; |
| 26 |
|
| 27 |
pub fn initialize() -> Result<(), HRESULT> { |
| 28 |
let err = unsafe { CoInitializeEx(null(), COINIT_MULTITHREADED.try_into().unwrap()) }; |
| 29 |
if err != S_OK && err != S_FALSE { |
| 30 |
// S_FALSE just means COM is already initialized |
| 31 |
Err(err) |
| 32 |
} else { |
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
pub struct ComPtr<T>(*mut T) |
| 38 |
where |
| 39 |
T: Interface; |
| 40 |
impl<T> ComPtr<T> |
| 41 |
where |
| 42 |
T: Interface, |
| 43 |
{ |
| 44 |
/// Creates a `ComPtr` to wrap a raw pointer. |
| 45 |
/// It takes ownership over the pointer which means it does __not__ call `AddRef`. |
| 46 |
/// `T` __must__ be a COM interface that inherits from `IUnknown`. |
| 47 |
pub unsafe fn from_raw(ptr: *mut T) -> ComPtr<T> { |
| 48 |
assert!(!ptr.is_null()); |
| 49 |
ComPtr(ptr) |
| 50 |
} |
| 51 |
/// Casts up the inheritance chain |
| 52 |
pub fn up<U>(self) -> ComPtr<U> |
| 53 |
where |
| 54 |
T: Deref<Target = U>, |
| 55 |
U: Interface, |
| 56 |
{ |
| 57 |
ComPtr(self.into_raw() as *mut U) |
| 58 |
} |
| 59 |
/// Extracts the raw pointer. |
| 60 |
/// You are now responsible for releasing it yourself. |
| 61 |
pub fn into_raw(self) -> *mut T { |
| 62 |
ManuallyDrop::new(self).0 |
| 63 |
} |
| 64 |
/// For internal use only. |
| 65 |
fn as_unknown(&self) -> &IUnknown { |
| 66 |
unsafe { &*(self.0 as *mut IUnknown) } |
| 67 |
} |
| 68 |
/// Performs `QueryInterface` fun. |
| 69 |
pub fn cast<U>(&self) -> Result<ComPtr<U>, i32> |
| 70 |
where |
| 71 |
U: Interface, |
| 72 |
{ |
| 73 |
let mut obj = null_mut(); |
| 74 |
let err = unsafe { self.as_unknown().QueryInterface(&U::uuidof(), &mut obj) }; |
| 75 |
if err < 0 { |
| 76 |
return Err(err); |
| 77 |
} |
| 78 |
Ok(unsafe { ComPtr::from_raw(obj as *mut U) }) |
| 79 |
} |
| 80 |
} |
| 81 |
impl<T> Deref for ComPtr<T> |
| 82 |
where |
| 83 |
T: Interface, |
| 84 |
{ |
| 85 |
type Target = T; |
| 86 |
fn deref(&self) -> &T { |
| 87 |
unsafe { &*self.0 } |
| 88 |
} |
| 89 |
} |
| 90 |
impl<T> Clone for ComPtr<T> |
| 91 |
where |
| 92 |
T: Interface, |
| 93 |
{ |
| 94 |
fn clone(&self) -> Self { |
| 95 |
unsafe { |
| 96 |
self.as_unknown().AddRef(); |
| 97 |
ComPtr::from_raw(self.0) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
impl<T> Drop for ComPtr<T> |
| 102 |
where |
| 103 |
T: Interface, |
| 104 |
{ |
| 105 |
fn drop(&mut self) { |
| 106 |
unsafe { |
| 107 |
self.as_unknown().Release(); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
pub struct BStr(BSTR); |
| 112 |
impl BStr { |
| 113 |
pub unsafe fn from_raw(s: BSTR) -> BStr { |
| 114 |
BStr(s) |
| 115 |
} |
| 116 |
pub fn to_osstring(&self) -> OsString { |
| 117 |
let len = unsafe { SysStringLen(self.0) }; |
| 118 |
let slice = unsafe { from_raw_parts(self.0, len as usize) }; |
| 119 |
OsStringExt::from_wide(slice) |
| 120 |
} |
| 121 |
} |
| 122 |
impl Drop for BStr { |
| 123 |
fn drop(&mut self) { |
| 124 |
unsafe { SysFreeString(self.0) }; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
pub trait ToWide { |
| 129 |
fn to_wide(&self) -> Vec<u16>; |
| 130 |
fn to_wide_null(&self) -> Vec<u16>; |
| 131 |
} |
| 132 |
impl<T> ToWide for T |
| 133 |
where |
| 134 |
T: AsRef<OsStr>, |
| 135 |
{ |
| 136 |
fn to_wide(&self) -> Vec<u16> { |
| 137 |
self.as_ref().encode_wide().collect() |
| 138 |
} |
| 139 |
fn to_wide_null(&self) -> Vec<u16> { |
| 140 |
self.as_ref().encode_wide().chain(Some(0)).collect() |
| 141 |
} |
| 142 |
} |
| 143 |
pub trait FromWide |
| 144 |
where |
| 145 |
Self: Sized, |
| 146 |
{ |
| 147 |
fn from_wide(wide: &[u16]) -> Self; |
| 148 |
fn from_wide_null(wide: &[u16]) -> Self { |
| 149 |
let len = wide.iter().take_while(|&&c| c != 0).count(); |
| 150 |
Self::from_wide(&wide[..len]) |
| 151 |
} |
| 152 |
} |
| 153 |
impl FromWide for OsString { |
| 154 |
fn from_wide(wide: &[u16]) -> OsString { |
| 155 |
OsStringExt::from_wide(wide) |
| 156 |
} |
| 157 |
} |