Skip to content

Commit ae5827e

Browse files
committed
WinRT: Replace custom ClassFactory with WIL CppWinRTClassFactory
Underlying implementation: https://github.com/microsoft/wil/blob/master/include/wil/cppwinrt_register_com_server.h
1 parent f2527dd commit ae5827e

File tree

6 files changed

+11
-38
lines changed

6 files changed

+11
-38
lines changed

MyDllServerWinRt/Main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Windows.h>
22
#include <unknwn.h>
33
#include <winrt/Windows.Foundation.h>
4+
#include <wil/cppwinrt_register_com_server.h>
45
#include "../support/WinRtUtils.hpp"
56
#include "../MyExeServerWinRt/MyServerImpl.hpp"
67

@@ -32,8 +33,8 @@ STDAPI DllGetClassObject(::GUID const& clsid, ::GUID const& iid, void** result)
3233
*result = nullptr;
3334

3435
if (clsid == __uuidof(MyServer)) {
35-
// TODO: Replace with wil::details::CppWinRTClassFactory after https://github.com/microsoft/wil/issues/534 is resolved
36-
return winrt::make<ClassFactory<MyServerImpl>>().as(iid, result);
36+
// TODO: Avoid relying on an "internal" WIL class here (see https://github.com/microsoft/wil/issues/534)
37+
return winrt::make<wil::details::CppWinRTClassFactory<MyServerImpl>>().as(iid, result);
3738
}
3839

3940
return CLASS_E_CLASSNOTAVAILABLE;

MyDllServerWinRt/MyDllServerWinRt.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
102102
<ImportGroup Label="ExtensionTargets">
103103
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets')" />
104+
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.250325.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.250325.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
104105
</ImportGroup>
105106
<Target Name="ServerUsage" AfterTargets="Build" DependsOnTargets="AssignTargetPaths">
106107
<Message Importance="High" Text="%0a**************************************%0a*** $(MSBuildProjectName) usage instructions ***%0a**************************************" />
@@ -114,5 +115,6 @@
114115
</PropertyGroup>
115116
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
116117
<Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
118+
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.250325.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.250325.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
117119
</Target>
118120
</Project>

MyDllServerWinRt/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Windows.CppWinRT" version="2.0.250303.1" targetFramework="native" />
4+
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.250325.1" targetFramework="native" />
45
</packages>

MyExeServerWinRt/Main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define WINRT_CUSTOM_MODULE_LOCK
55
#include <wil/resource.h>
66
#include <wil/cppwinrt_notifiable_module_lock.h>
7+
#include <wil/cppwinrt_register_com_server.h>
78

89
#include "../support/WinRtUtils.hpp"
910
#include "MyServerImpl.hpp"
@@ -40,9 +41,8 @@ int wmain(int argc, wchar_t* argv[]) {
4041
});
4142

4243
// register class factory in current process
43-
// TODO: Replace with wil::register_com_server after https://github.com/microsoft/wil/pull/533 is fixed.
44-
DWORD registration = 0;
45-
winrt::check_hresult(::CoRegisterClassObject(__uuidof(MyServer), winrt::make<ClassFactory<MyServerImpl>>().get(), CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &registration));
44+
// TODO: Fix buggy wil::register_com_server implementation (see https://github.com/microsoft/wil/pull/533)
45+
auto revoker = wil::register_com_server<MyServerImpl>(); // registers with CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE
4646

4747
wprintf(L"Waiting for COM class creation requests...\n");
4848

MyExeServerWinRt/MyServerImpl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class NumberCruncher : public winrt::implements<NumberCruncher, INumberCruncher,
3232

3333

3434
/** Creatable COM class that needs a CLSID. */
35-
class MyServerImpl : public winrt::implements<MyServerImpl, IMyServer, winrt::no_weak_ref> {
35+
class __declspec(uuid("AF080472-F173-4D9D-8BE7-435776617347")) // __uuidof(MyServer)
36+
MyServerImpl : public winrt::implements<MyServerImpl, IMyServer, winrt::no_weak_ref> {
3637
public:
3738
MyServerImpl() {
3839
#ifndef NDEBUG

support/WinRtUtils.hpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,6 @@
1010
#include "../support/CurrentModule.hpp"
1111

1212

13-
/** Minimal COM class factory implementation.
14-
TODO: Replace with wil::detail::CppWinRTClassFactory after https://github.com/microsoft/wil/pull/533 and https://github.com/microsoft/wil/issues/534 are resolved. */
15-
template <class T>
16-
class ClassFactory : public winrt::implements<ClassFactory<T>, IClassFactory, winrt::no_module_lock> {
17-
public:
18-
ClassFactory() {
19-
#ifndef NDEBUG
20-
wprintf(L"ClassFactory ctor\n");
21-
#endif
22-
}
23-
24-
~ClassFactory() {
25-
#ifndef NDEBUG
26-
wprintf(L"ClassFactory dtor\n");
27-
#endif
28-
}
29-
30-
HRESULT CreateInstance(IUnknown* outer, const IID& iid, void** result) noexcept override {
31-
*result = nullptr;
32-
if (outer)
33-
return CLASS_E_NOAGGREGATION; // aggregation not supported yet
34-
35-
// create object
36-
return winrt::make<T>().as(iid, result);
37-
}
38-
39-
HRESULT LockServer(BOOL) noexcept override {
40-
return S_OK;
41-
}
42-
};
43-
44-
4513
/** COM type library (un)registration function.
4614
TODO: Replace this function with WIL alternative if https://github.com/microsoft/wil/issues/531 is resolved. */
4715
::GUID RegisterTypeLibrary(bool do_register, std::wstring tlb_path) {

0 commit comments

Comments
 (0)