Skip to content

Commit 6690434

Browse files
benhillisBen Hillis
andauthored
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>
1 parent d9c69a5 commit 6690434

File tree

8 files changed

+21
-29
lines changed

8 files changed

+21
-29
lines changed

src/windows/common/svccomm.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,13 @@ wsl::windows::common::SvcComm::SvcComm()
540540
};
541541

542542
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); },
546544
std::chrono::seconds(1),
547545
std::chrono::minutes(1),
548546
retry_pred);
549547

550548
// 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>();
553550

554551
// Get the current proxy blanket settings.
555552
DWORD authnSvc, authzSvc, authnLvl, capabilities;

src/windows/inc/comservicehelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace Windows { namespace Internal {
7777
// Tell COM how to mask fatal exceptions.
7878
if (ownProcess)
7979
{
80-
Microsoft::WRL::ComPtr<IGlobalOptions> pIGLB;
80+
wil::com_ptr<IGlobalOptions> pIGLB;
8181
RETURN_IF_FAILED(CoCreateInstance(CLSID_GlobalOptions, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIGLB)));
8282
RETURN_IF_FAILED(pIGLB->Set(COMGLB_EXCEPTION_HANDLING, TExceptionPolicy));
8383
}
@@ -294,7 +294,7 @@ namespace Windows { namespace Internal {
294294
bool m_addedModuleReference = false;
295295

296296
// COM callback object to support unloading shared-process services
297-
Microsoft::WRL::ComPtr<IContextCallback> m_icc;
297+
wil::com_ptr<IContextCallback> m_icc;
298298

299299
// COM Server descriptor
300300
ServerDescriptor m_serverDescriptor{};

src/windows/service/exe/LxssIpTables.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ const std::wstring LxssNetworkingFirewall::s_FriendlyNamePrefix(L"WSLRULE_177744
335335

336336
LxssNetworkingFirewall::LxssNetworkingFirewall()
337337
{
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);
339339
}
340340

341341
void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY* Source, ULONG DestinationIndexStart, ULONG SourceIndexStart, ULONG ElementsToCopy)
@@ -388,8 +388,7 @@ void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY*
388388

389389
std::wstring LxssNetworkingFirewall::AddPortRule(const IP_ADDRESS_PREFIX& Address) const
390390
{
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);
393392

394393
// Open a port via the firewall by creating a rule that specifies the local
395394
// 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
412411
THROW_IF_FAILED(newRule->put_Description(s_DefaultRuleDescription.get()));
413412
THROW_IF_FAILED(newRule->put_Enabled(VARIANT_TRUE));
414413
// Add the rule to the existing set.
415-
Microsoft::WRL::ComPtr<INetFwRules> rules;
414+
wil::com_ptr<INetFwRules> rules;
416415
THROW_IF_FAILED(m_firewall->get_Rules(&rules));
417-
THROW_IF_FAILED(rules->Add(newRule.Get()));
416+
THROW_IF_FAILED(rules->Add(newRule.get()));
418417
// Return the unique rule name to the caller.
419418
return generatedName;
420419
}
@@ -423,12 +422,11 @@ void LxssNetworkingFirewall::CleanupRemnants()
423422
{
424423
auto firewall = std::make_shared<LxssNetworkingFirewall>();
425424
THROW_HR_IF(E_OUTOFMEMORY, !firewall);
426-
Microsoft::WRL::ComPtr<INetFwRules> rules;
425+
wil::com_ptr<INetFwRules> rules;
427426
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>();
432430
// Find any rules with the unique WSL prefix and destroy them.
433431
for (;;)
434432
{
@@ -440,7 +438,7 @@ void LxssNetworkingFirewall::CleanupRemnants()
440438
break;
441439
}
442440

443-
Microsoft::WRL::ComPtr<INetFwRule> nextRule;
441+
wil::com_ptr<INetFwRule> nextRule;
444442
THROW_IF_FAILED(next.pdispVal->QueryInterface(IID_PPV_ARGS(&nextRule)));
445443
wil::unique_bstr nextRuleName;
446444
THROW_IF_FAILED(nextRule->get_Name(nextRuleName.addressof()));
@@ -558,7 +556,7 @@ void LxssNetworkingFirewall::RemoveExcludedAdapter(const std::wstring& AdapterNa
558556

559557
void LxssNetworkingFirewall::RemovePortRule(const std::wstring& RuleName) const
560558
{
561-
Microsoft::WRL::ComPtr<INetFwRules> rules;
559+
wil::com_ptr<INetFwRules> rules;
562560
THROW_IF_FAILED(m_firewall->get_Rules(&rules));
563561
THROW_IF_FAILED(rules->Remove(wil::make_bstr_failfast(RuleName.c_str()).get()));
564562
}
@@ -572,8 +570,7 @@ LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(const std::shared_ptr<Lxs
572570
return;
573571
}
574572

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) :
577574
m_firewall(Firewall)
578575
{
579576
wil::unique_bstr ruleName;

src/windows/service/exe/LxssIpTables.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class LxssNetworkingFirewall
262262
/// <summary>
263263
/// COM firewall instance.
264264
/// </summary>
265-
Microsoft::WRL::ComPtr<INetFwPolicy2> m_firewall;
265+
wil::com_ptr<INetFwPolicy2> m_firewall;
266266

267267
/// <summary>
268268
/// Lock to protect class members.
@@ -295,7 +295,7 @@ class LxssNetworkingFirewallPort
295295
/// <summary>
296296
/// Constructor to take ownership of an existing rule.
297297
/// </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);
299299

300300
/// <summary>
301301
/// Destructor.

src/windows/service/exe/LxssUserSession.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,8 +2674,7 @@ try
26742674
THROW_IF_FAILED(shellLink->SetArguments(commandLine.c_str()));
26752675
THROW_IF_FAILED(shellLink->SetIconLocation(ShortcutIcon, 0));
26762676

2677-
Microsoft::WRL::ComPtr<IPersistFile> storage;
2678-
THROW_IF_FAILED(shellLink->QueryInterface(IID_IPersistFile, &storage));
2677+
auto storage = shellLink.query<IPersistFile>();
26792678
THROW_IF_FAILED(storage->Save(shortcutPath.c_str(), true));
26802679

26812680
registration.Write(Property::ShortcutPath, shortcutPath.c_str());

src/windows/service/exe/WslCoreVm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class WslCoreVm
351351
wsl::shared::SocketChannel m_miniInitChannel;
352352
wil::unique_socket m_notifyChannel;
353353
SE_SID m_userSid;
354-
Microsoft::WRL::ComPtr<DeviceHostProxy> m_deviceHostSupport;
354+
wil::com_ptr<DeviceHostProxy> m_deviceHostSupport;
355355
std::shared_ptr<LxssRunningInstance> m_systemDistro;
356356
_Guarded_by_(m_lock) std::bitset<MAX_VHD_COUNT> m_lunBitmap;
357357
_Guarded_by_(m_lock) std::map<AttachedDisk, DiskState> m_attachedDisks;

test/windows/PolicyTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ class PolicyTest
327327
const auto stop = std::chrono::steady_clock::now() + std::chrono::seconds{30};
328328
for (;;)
329329
{
330-
Microsoft::WRL::ComPtr<ILxssUserSession> session;
330+
wil::com_ptr<ILxssUserSession> session;
331331
result = CoCreateInstance(CLSID_LxssUserSession, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&session));
332332
if (result == expectedResult || std::chrono::steady_clock::now() > stop)
333333
{

test/windows/UnitTests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,8 +2448,7 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND
24482448
// Validate that the shortcut is actually in the start menu
24492449
VERIFY_IS_TRUE(shortcutPath.find(startMenu) != std::string::npos);
24502450

2451-
Microsoft::WRL::ComPtr<IPersistFile> storage;
2452-
VERIFY_SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, &storage));
2451+
auto storage = shellLink.query<IPersistFile>();
24532452

24542453
VERIFY_SUCCEEDED(storage->Load(shortcutPath.c_str(), 0));
24552454

0 commit comments

Comments
 (0)