Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions strings/base_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ WINRT_EXPORT namespace winrt
impl::get_factory_cache().clear();
}

template <typename Interface>
auto try_create_instance(guid const& clsid, uint32_t context = 0x1 /*CLSCTX_INPROC_SERVER*/, void* outer = nullptr)
{
return try_capture<Interface>(WINRT_IMPL_CoCreateInstance, clsid, outer, context);
}

template <typename Interface>
auto create_instance(guid const& clsid, uint32_t context = 0x1 /*CLSCTX_INPROC_SERVER*/, void* outer = nullptr)
{
Expand Down
27 changes: 27 additions & 0 deletions strings/base_com_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ WINRT_EXPORT namespace winrt
*other = m_ptr;
}

template <typename F, typename...Args>
bool try_capture(F function, Args&&...args)
{
return function(args..., guid_of<T>(), put_void()) >= 0;
}

template <typename O, typename M, typename...Args>
bool try_capture(com_ptr<O> const& object, M method, Args&&...args)
{
return (object.get()->*(method))(args..., guid_of<T>(), put_void()) >= 0;
}

template <typename F, typename...Args>
void capture(F function, Args&&...args)
{
Expand Down Expand Up @@ -204,6 +216,21 @@ WINRT_EXPORT namespace winrt
type* m_ptr{};
};

template <typename T, typename F, typename...Args>
impl::com_ref<T> try_capture(F function, Args&& ...args)
{
void* result{};
function(args..., guid_of<T>(), &result);
return { result, take_ownership_from_abi };
}

template <typename T, typename O, typename M, typename...Args>
impl::com_ref<T> try_capture(com_ptr<O> const& object, M method, Args&& ...args)
{
void* result{};
(object.get()->*(method))(args..., guid_of<T>(), &result);
return { result, take_ownership_from_abi };
}
template <typename T, typename F, typename...Args>
impl::com_ref<T> capture(F function, Args&& ...args)
{
Expand Down
24 changes: 24 additions & 0 deletions test/old_tests/UnitTests/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,27 @@ TEST_CASE("capture")
REQUIRE_THROWS_AS(capture<IDispatch>(a, &ICapture::CreateMemberCapture, 0), hresult_no_interface);
REQUIRE_THROWS_AS(d.capture(a, &ICapture::CreateMemberCapture, 0), hresult_no_interface);
}

TEST_CASE("try_capture")
{
// Identical to the "capture" test above, just with different
// error handling.
com_ptr<ICapture> a = try_capture<ICapture>(CreateCapture, 10);
REQUIRE(a->GetValue() == 10);
a = nullptr;
REQUIRE(a.try_capture(CreateCapture, 20));
REQUIRE(a->GetValue() == 20);

auto b = try_capture<ICapture>(a, &ICapture::CreateMemberCapture, 30);
REQUIRE(b->GetValue() == 30);
b = nullptr;
REQUIRE(b.try_capture(a, &ICapture::CreateMemberCapture, 40));
REQUIRE(b->GetValue() == 40);

com_ptr<IDispatch> d;

REQUIRE(!try_capture<IDispatch>(CreateCapture, 0));
REQUIRE(!d.try_capture(CreateCapture, 0));
REQUIRE(!try_capture<IDispatch>(a, &ICapture::CreateMemberCapture, 0));
REQUIRE(!d.try_capture(a, &ICapture::CreateMemberCapture, 0));
}
9 changes: 9 additions & 0 deletions test/old_tests/UnitTests/create_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ TEST_CASE("create_instance")
com_ptr<IFileOpenDialog> dialog = create_instance<IFileOpenDialog>(guid_of<FileOpenDialog>());
REQUIRE(dialog);
}

TEST_CASE("try_create_instance")
{
com_ptr<IFileOpenDialog> dialog = try_create_instance<IFileOpenDialog>(guid_of<FileOpenDialog>());
REQUIRE(dialog);

dialog = try_create_instance<IFileOpenDialog>(CLSID_NULL);
REQUIRE(!dialog);
}