Skip to content

Commit

Permalink
all four cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 15, 2023
1 parent 0d29d18 commit 86b4806
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
8 changes: 8 additions & 0 deletions crates/libs/core/src/imp/weak_ref_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ impl TearOff {
unsafe extern "system" fn StrongQueryInterface(ptr: *mut std::ffi::c_void, iid: *const crate::GUID, interface: *mut *mut std::ffi::c_void) -> crate::HRESULT {
let this = Self::from_strong_ptr(ptr);

if iid.is_null() || interface.is_null() {
return ::windows_core::HRESULT(-2147467261); // E_POINTER
}

// Only directly respond to queries for the the tear-off's strong interface. This is
// effectively a self-query.
if *iid == IWeakReferenceSource::IID {
Expand All @@ -142,6 +146,10 @@ impl TearOff {
unsafe extern "system" fn WeakQueryInterface(ptr: *mut std::ffi::c_void, iid: *const crate::GUID, interface: *mut *mut std::ffi::c_void) -> crate::HRESULT {
let this = Self::from_weak_ptr(ptr);

if iid.is_null() || interface.is_null() {
return ::windows_core::HRESULT(-2147467261); // E_POINTER
}

// While the weak vtable is packed into the same allocation as the strong vtable and
// tear-off, it represents a distinct COM identity and thus does not share or delegate to
// the object.
Expand Down
57 changes: 21 additions & 36 deletions crates/tests/implement/tests/query.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
use windows::{core::*, Foundation::*, Win32::Foundation::*};

#[test]
fn delegate() {
unsafe {
let interface: EventHandler<i32> = EventHandler::<i32>::new(move |_, _| todo!());

// Successful query
{
let mut unknown: Option<IUnknown> = None;
let hr = interface.query(&IUnknown::IID, &mut unknown as *mut _ as *mut _);
assert_eq!(hr, S_OK);
assert_eq!(interface.cast::<IUnknown>().unwrap(), unknown.unwrap());
}
// Unsuccessful query
{
let mut closable: Option<IClosable> = None;
let hr = interface.query(&IClosable::IID, &mut closable as *mut _ as *mut _);
assert_eq!(hr, E_NOINTERFACE);
assert_eq!(closable, None);
}
// iid param is null
{
let mut unknown: Option<IUnknown> = None;
let hr = interface.query(std::ptr::null(), &mut unknown as *mut _ as *mut _);
assert_eq!(hr, E_POINTER);
assert_eq!(unknown, None);
}
// interface param is null
{
let hr = interface.query(&IUnknown::IID, std::ptr::null_mut());
assert_eq!(hr, E_POINTER);
}
}
}
use windows::{core::*, Foundation::*, Win32::Foundation::*, Win32::System::WinRT::*};

#[implement(IStringable)]
struct Stringable;
Expand All @@ -44,31 +10,50 @@ impl IStringable_Impl for Stringable {
}

#[test]
fn implement() {
fn test() {
unsafe {
// This covers the four distinct implementations of QueryInterface.

let delegate: EventHandler<i32> = EventHandler::<i32>::new(move |_, _| todo!());
test_query(&delegate);

let interface: IStringable = Stringable.into();
test_query(&interface);

let source: IWeakReferenceSource = interface.cast().unwrap();
test_query(&source);

let weak = source.GetWeakReference().unwrap();
test_query(&weak);
}
}

fn test_query<I: ComInterface>(interface: &I) {
unsafe {
// Successful query
{
let mut unknown: Option<IUnknown> = None;
let hr = interface.query(&IUnknown::IID, &mut unknown as *mut _ as *mut _);
assert_eq!(hr, S_OK);
assert_eq!(interface.cast::<IUnknown>().unwrap(), unknown.unwrap());
}

// Unsuccessful query
{
let mut closable: Option<IClosable> = None;
let hr = interface.query(&IClosable::IID, &mut closable as *mut _ as *mut _);
assert_eq!(hr, E_NOINTERFACE);
assert_eq!(closable, None);
}

// iid param is null
{
let mut unknown: Option<IUnknown> = None;
let hr = interface.query(std::ptr::null(), &mut unknown as *mut _ as *mut _);
assert_eq!(hr, E_POINTER);
assert_eq!(unknown, None);
}

// interface param is null
{
let hr = interface.query(&IUnknown::IID, std::ptr::null_mut());
Expand Down

0 comments on commit 86b4806

Please sign in to comment.