cleanup: switch from Microsoft::WRL::ComPtr to wil::com_ptr (#13767)
* cleanup: switch from Microsoft::WRL::ComPtr to wil::com_ptr * reformat --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
Ben Hillis committed
Nov 24, 2025 at 12:47 UTC
66904342a5a3547c78a36a26b70d408b19925e28
8 files changed
+21
-29
src/windows/common/svccomm.cpp
+2
-5
@@ -540,16 +540,13 @@ wsl::windows::common::SvcComm::SvcComm()
540
};
541
542
wsl::shared::retry::RetryWithTimeout<void>(
543
- [this]() {
544
- THROW_IF_FAILED(CoCreateInstance(__uuidof(LxssUserSession), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&m_userSession)));
545
- },
543
+ [this]() { m_userSession = wil::CoCreateInstance<LxssUserSession, ILxssUserSession>(CLSCTX_LOCAL_SERVER); },
544
std::chrono::seconds(1),
545
std::chrono::minutes(1),
546
retry_pred);
547
548
// Query client security interface.
551
- wil::com_ptr_nothrow<IClientSecurity> clientSecurity;
552
- THROW_IF_FAILED(m_userSession->QueryInterface(IID_PPV_ARGS(&clientSecurity)));
549
+ auto clientSecurity = m_userSession.query<IClientSecurity>();
550
551
// Get the current proxy blanket settings.
552
DWORD authnSvc, authzSvc, authnLvl, capabilities;
src/windows/inc/comservicehelper.h
+2
-2
@@ -77,7 +77,7 @@ namespace Windows { namespace Internal {
77
// Tell COM how to mask fatal exceptions.
78
if (ownProcess)
79
{
80
- Microsoft::WRL::ComPtr<IGlobalOptions> pIGLB;
80
+ wil::com_ptr<IGlobalOptions> pIGLB;
81
RETURN_IF_FAILED(CoCreateInstance(CLSID_GlobalOptions, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIGLB)));
82
RETURN_IF_FAILED(pIGLB->Set(COMGLB_EXCEPTION_HANDLING, TExceptionPolicy));
83
}
@@ -294,7 +294,7 @@ namespace Windows { namespace Internal {
294
bool m_addedModuleReference = false;
295
296
// COM callback object to support unloading shared-process services
297
- Microsoft::WRL::ComPtr<IContextCallback> m_icc;
297
+ wil::com_ptr<IContextCallback> m_icc;
298
299
// COM Server descriptor
300
ServerDescriptor m_serverDescriptor{};
src/windows/service/exe/LxssIpTables.cpp
+11
-14
@@ -335,7 +335,7 @@ const std::wstring LxssNetworkingFirewall::s_FriendlyNamePrefix(L"WSLRULE_177744
335
336
LxssNetworkingFirewall::LxssNetworkingFirewall()
337
{
338
- THROW_IF_FAILED(::CoCreateInstance(__uuidof(NetFwPolicy2), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_firewall)));
338
+ m_firewall = wil::CoCreateInstance<NetFwPolicy2, INetFwPolicy2>(CLSCTX_INPROC_SERVER);
339
}
340
341
void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY* Source, ULONG DestinationIndexStart, ULONG SourceIndexStart, ULONG ElementsToCopy)
@@ -388,8 +388,7 @@ void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY*
388
389
std::wstring LxssNetworkingFirewall::AddPortRule(const IP_ADDRESS_PREFIX& Address) const
390
{
391
- Microsoft::WRL::ComPtr<INetFwRule> newRule;
392
- THROW_IF_FAILED(::CoCreateInstance(__uuidof(NetFwRule), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&newRule)));
391
+ auto newRule = wil::CoCreateInstance<NetFwRule, INetFwRule>(CLSCTX_INPROC_SERVER);
392
393
// Open a port via the firewall by creating a rule that specifies the local
394
// address and the local port to allow. Currently this rule only applies to
@@ -412,9 +411,9 @@ std::wstring LxssNetworkingFirewall::AddPortRule(const IP_ADDRESS_PREFIX& Addres
411
THROW_IF_FAILED(newRule->put_Description(s_DefaultRuleDescription.get()));
412
THROW_IF_FAILED(newRule->put_Enabled(VARIANT_TRUE));
413
// Add the rule to the existing set.
415
- Microsoft::WRL::ComPtr<INetFwRules> rules;
414
+ wil::com_ptr<INetFwRules> rules;
415
THROW_IF_FAILED(m_firewall->get_Rules(&rules));
417
- THROW_IF_FAILED(rules->Add(newRule.Get()));
416
+ THROW_IF_FAILED(rules->Add(newRule.get()));
417
// Return the unique rule name to the caller.
418
return generatedName;
419
}
@@ -423,12 +422,11 @@ void LxssNetworkingFirewall::CleanupRemnants()
422
{
423
auto firewall = std::make_shared<LxssNetworkingFirewall>();
424
THROW_HR_IF(E_OUTOFMEMORY, !firewall);
426
- Microsoft::WRL::ComPtr<INetFwRules> rules;
425
+ wil::com_ptr<INetFwRules> rules;
426
THROW_IF_FAILED(firewall->m_firewall->get_Rules(&rules));
428
- Microsoft::WRL::ComPtr<IUnknown> enumInterface;
429
- THROW_IF_FAILED(rules->get__NewEnum(enumInterface.GetAddressOf()));
430
- Microsoft::WRL::ComPtr<IEnumVARIANT> rulesEnum;
431
- THROW_IF_FAILED(enumInterface.As(&rulesEnum));
427
+ wil::com_ptr<IUnknown> enumInterface;
428
+ THROW_IF_FAILED(rules->get__NewEnum(enumInterface.addressof()));
429
+ auto rulesEnum = enumInterface.query<IEnumVARIANT>();
430
// Find any rules with the unique WSL prefix and destroy them.
431
for (;;)
432
{
@@ -440,7 +438,7 @@ void LxssNetworkingFirewall::CleanupRemnants()
438
break;
439
}
440
443
- Microsoft::WRL::ComPtr<INetFwRule> nextRule;
441
+ wil::com_ptr<INetFwRule> nextRule;
442
THROW_IF_FAILED(next.pdispVal->QueryInterface(IID_PPV_ARGS(&nextRule)));
443
wil::unique_bstr nextRuleName;
444
THROW_IF_FAILED(nextRule->get_Name(nextRuleName.addressof()));
@@ -558,7 +556,7 @@ void LxssNetworkingFirewall::RemoveExcludedAdapter(const std::wstring& AdapterNa
556
557
void LxssNetworkingFirewall::RemovePortRule(const std::wstring& RuleName) const
558
{
561
- Microsoft::WRL::ComPtr<INetFwRules> rules;
559
+ wil::com_ptr<INetFwRules> rules;
560
THROW_IF_FAILED(m_firewall->get_Rules(&rules));
561
THROW_IF_FAILED(rules->Remove(wil::make_bstr_failfast(RuleName.c_str()).get()));
562
}
@@ -572,8 +570,7 @@ LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(const std::shared_ptr<Lxs
570
return;
571
}
572
575
-LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(
576
- const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const Microsoft::WRL::ComPtr<INetFwRule>& Existing) :
573
+LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const wil::com_ptr<INetFwRule>& Existing) :
574
m_firewall(Firewall)
575
{
576
wil::unique_bstr ruleName;
src/windows/service/exe/LxssIpTables.h
+2
-2
@@ -262,7 +262,7 @@ private:
262
/// <summary>
263
/// COM firewall instance.
264
/// </summary>
265
- Microsoft::WRL::ComPtr<INetFwPolicy2> m_firewall;
265
+ wil::com_ptr<INetFwPolicy2> m_firewall;
266
267
/// <summary>
268
/// Lock to protect class members.
@@ -295,7 +295,7 @@ public:
295
/// <summary>
296
/// Constructor to take ownership of an existing rule.
297
/// </summary>
298
- LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const Microsoft::WRL::ComPtr<INetFwRule>& Existing);
298
+ LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const wil::com_ptr<INetFwRule>& Existing);
299
300
/// <summary>
301
/// Destructor.
src/windows/service/exe/LxssUserSession.cpp
+1
-2
@@ -2674,8 +2674,7 @@ try
2674
THROW_IF_FAILED(shellLink->SetArguments(commandLine.c_str()));
2675
THROW_IF_FAILED(shellLink->SetIconLocation(ShortcutIcon, 0));
2676
2677
- Microsoft::WRL::ComPtr<IPersistFile> storage;
2678
- THROW_IF_FAILED(shellLink->QueryInterface(IID_IPersistFile, &storage));
2677
+ auto storage = shellLink.query<IPersistFile>();
2678
THROW_IF_FAILED(storage->Save(shortcutPath.c_str(), true));
2679
2680
registration.Write(Property::ShortcutPath, shortcutPath.c_str());
src/windows/service/exe/WslCoreVm.h
+1
-1
@@ -351,7 +351,7 @@ private:
351
wsl::shared::SocketChannel m_miniInitChannel;
352
wil::unique_socket m_notifyChannel;
353
SE_SID m_userSid;
354
- Microsoft::WRL::ComPtr<DeviceHostProxy> m_deviceHostSupport;
354
+ wil::com_ptr<DeviceHostProxy> m_deviceHostSupport;
355
std::shared_ptr<LxssRunningInstance> m_systemDistro;
356
_Guarded_by_(m_lock) std::bitset<MAX_VHD_COUNT> m_lunBitmap;
357
_Guarded_by_(m_lock) std::map<AttachedDisk, DiskState> m_attachedDisks;
test/windows/PolicyTests.cpp
+1
-1
@@ -327,7 +327,7 @@ class PolicyTest
327
const auto stop = std::chrono::steady_clock::now() + std::chrono::seconds{30};
328
for (;;)
329
{
330
- Microsoft::WRL::ComPtr<ILxssUserSession> session;
330
+ wil::com_ptr<ILxssUserSession> session;
331
result = CoCreateInstance(CLSID_LxssUserSession, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&session));
332
if (result == expectedResult || std::chrono::steady_clock::now() > stop)
333
{
test/windows/UnitTests.cpp
+1
-2
@@ -2448,8 +2448,7 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND
2448
// Validate that the shortcut is actually in the start menu
2449
VERIFY_IS_TRUE(shortcutPath.find(startMenu) != std::string::npos);
2450
2451
- Microsoft::WRL::ComPtr<IPersistFile> storage;
2452
- VERIFY_SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, &storage));
2451
+ auto storage = shellLink.query<IPersistFile>();
2452
2453
VERIFY_SUCCEEDED(storage->Load(shortcutPath.c_str(), 0));
2454