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>
This commit is contained in:
Ben Hillis 2025-11-24 12:47:22 -08:00 committed by GitHub
parent d9c69a50ab
commit 66904342a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 21 additions and 29 deletions

View File

@ -540,16 +540,13 @@ wsl::windows::common::SvcComm::SvcComm()
}; };
wsl::shared::retry::RetryWithTimeout<void>( wsl::shared::retry::RetryWithTimeout<void>(
[this]() { [this]() { m_userSession = wil::CoCreateInstance<LxssUserSession, ILxssUserSession>(CLSCTX_LOCAL_SERVER); },
THROW_IF_FAILED(CoCreateInstance(__uuidof(LxssUserSession), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&m_userSession)));
},
std::chrono::seconds(1), std::chrono::seconds(1),
std::chrono::minutes(1), std::chrono::minutes(1),
retry_pred); retry_pred);
// Query client security interface. // Query client security interface.
wil::com_ptr_nothrow<IClientSecurity> clientSecurity; auto clientSecurity = m_userSession.query<IClientSecurity>();
THROW_IF_FAILED(m_userSession->QueryInterface(IID_PPV_ARGS(&clientSecurity)));
// Get the current proxy blanket settings. // Get the current proxy blanket settings.
DWORD authnSvc, authzSvc, authnLvl, capabilities; DWORD authnSvc, authzSvc, authnLvl, capabilities;

View File

@ -77,7 +77,7 @@ namespace Windows { namespace Internal {
// Tell COM how to mask fatal exceptions. // Tell COM how to mask fatal exceptions.
if (ownProcess) if (ownProcess)
{ {
Microsoft::WRL::ComPtr<IGlobalOptions> pIGLB; wil::com_ptr<IGlobalOptions> pIGLB;
RETURN_IF_FAILED(CoCreateInstance(CLSID_GlobalOptions, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIGLB))); RETURN_IF_FAILED(CoCreateInstance(CLSID_GlobalOptions, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIGLB)));
RETURN_IF_FAILED(pIGLB->Set(COMGLB_EXCEPTION_HANDLING, TExceptionPolicy)); RETURN_IF_FAILED(pIGLB->Set(COMGLB_EXCEPTION_HANDLING, TExceptionPolicy));
} }
@ -294,7 +294,7 @@ namespace Windows { namespace Internal {
bool m_addedModuleReference = false; bool m_addedModuleReference = false;
// COM callback object to support unloading shared-process services // COM callback object to support unloading shared-process services
Microsoft::WRL::ComPtr<IContextCallback> m_icc; wil::com_ptr<IContextCallback> m_icc;
// COM Server descriptor // COM Server descriptor
ServerDescriptor m_serverDescriptor{}; ServerDescriptor m_serverDescriptor{};

View File

@ -335,7 +335,7 @@ const std::wstring LxssNetworkingFirewall::s_FriendlyNamePrefix(L"WSLRULE_177744
LxssNetworkingFirewall::LxssNetworkingFirewall() LxssNetworkingFirewall::LxssNetworkingFirewall()
{ {
THROW_IF_FAILED(::CoCreateInstance(__uuidof(NetFwPolicy2), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_firewall))); m_firewall = wil::CoCreateInstance<NetFwPolicy2, INetFwPolicy2>(CLSCTX_INPROC_SERVER);
} }
void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY* Source, ULONG DestinationIndexStart, ULONG SourceIndexStart, ULONG ElementsToCopy) void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY* Source, ULONG DestinationIndexStart, ULONG SourceIndexStart, ULONG ElementsToCopy)
@ -388,8 +388,7 @@ void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY*
std::wstring LxssNetworkingFirewall::AddPortRule(const IP_ADDRESS_PREFIX& Address) const std::wstring LxssNetworkingFirewall::AddPortRule(const IP_ADDRESS_PREFIX& Address) const
{ {
Microsoft::WRL::ComPtr<INetFwRule> newRule; auto newRule = wil::CoCreateInstance<NetFwRule, INetFwRule>(CLSCTX_INPROC_SERVER);
THROW_IF_FAILED(::CoCreateInstance(__uuidof(NetFwRule), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&newRule)));
// Open a port via the firewall by creating a rule that specifies the local // Open a port via the firewall by creating a rule that specifies the local
// address and the local port to allow. Currently this rule only applies to // 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
THROW_IF_FAILED(newRule->put_Description(s_DefaultRuleDescription.get())); THROW_IF_FAILED(newRule->put_Description(s_DefaultRuleDescription.get()));
THROW_IF_FAILED(newRule->put_Enabled(VARIANT_TRUE)); THROW_IF_FAILED(newRule->put_Enabled(VARIANT_TRUE));
// Add the rule to the existing set. // Add the rule to the existing set.
Microsoft::WRL::ComPtr<INetFwRules> rules; wil::com_ptr<INetFwRules> rules;
THROW_IF_FAILED(m_firewall->get_Rules(&rules)); THROW_IF_FAILED(m_firewall->get_Rules(&rules));
THROW_IF_FAILED(rules->Add(newRule.Get())); THROW_IF_FAILED(rules->Add(newRule.get()));
// Return the unique rule name to the caller. // Return the unique rule name to the caller.
return generatedName; return generatedName;
} }
@ -423,12 +422,11 @@ void LxssNetworkingFirewall::CleanupRemnants()
{ {
auto firewall = std::make_shared<LxssNetworkingFirewall>(); auto firewall = std::make_shared<LxssNetworkingFirewall>();
THROW_HR_IF(E_OUTOFMEMORY, !firewall); THROW_HR_IF(E_OUTOFMEMORY, !firewall);
Microsoft::WRL::ComPtr<INetFwRules> rules; wil::com_ptr<INetFwRules> rules;
THROW_IF_FAILED(firewall->m_firewall->get_Rules(&rules)); THROW_IF_FAILED(firewall->m_firewall->get_Rules(&rules));
Microsoft::WRL::ComPtr<IUnknown> enumInterface; wil::com_ptr<IUnknown> enumInterface;
THROW_IF_FAILED(rules->get__NewEnum(enumInterface.GetAddressOf())); THROW_IF_FAILED(rules->get__NewEnum(enumInterface.addressof()));
Microsoft::WRL::ComPtr<IEnumVARIANT> rulesEnum; auto rulesEnum = enumInterface.query<IEnumVARIANT>();
THROW_IF_FAILED(enumInterface.As(&rulesEnum));
// Find any rules with the unique WSL prefix and destroy them. // Find any rules with the unique WSL prefix and destroy them.
for (;;) for (;;)
{ {
@ -440,7 +438,7 @@ void LxssNetworkingFirewall::CleanupRemnants()
break; break;
} }
Microsoft::WRL::ComPtr<INetFwRule> nextRule; wil::com_ptr<INetFwRule> nextRule;
THROW_IF_FAILED(next.pdispVal->QueryInterface(IID_PPV_ARGS(&nextRule))); THROW_IF_FAILED(next.pdispVal->QueryInterface(IID_PPV_ARGS(&nextRule)));
wil::unique_bstr nextRuleName; wil::unique_bstr nextRuleName;
THROW_IF_FAILED(nextRule->get_Name(nextRuleName.addressof())); THROW_IF_FAILED(nextRule->get_Name(nextRuleName.addressof()));
@ -558,7 +556,7 @@ void LxssNetworkingFirewall::RemoveExcludedAdapter(const std::wstring& AdapterNa
void LxssNetworkingFirewall::RemovePortRule(const std::wstring& RuleName) const void LxssNetworkingFirewall::RemovePortRule(const std::wstring& RuleName) const
{ {
Microsoft::WRL::ComPtr<INetFwRules> rules; wil::com_ptr<INetFwRules> rules;
THROW_IF_FAILED(m_firewall->get_Rules(&rules)); THROW_IF_FAILED(m_firewall->get_Rules(&rules));
THROW_IF_FAILED(rules->Remove(wil::make_bstr_failfast(RuleName.c_str()).get())); THROW_IF_FAILED(rules->Remove(wil::make_bstr_failfast(RuleName.c_str()).get()));
} }
@ -572,8 +570,7 @@ LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(const std::shared_ptr<Lxs
return; return;
} }
LxssNetworkingFirewallPort::LxssNetworkingFirewallPort( LxssNetworkingFirewallPort::LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const wil::com_ptr<INetFwRule>& Existing) :
const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const Microsoft::WRL::ComPtr<INetFwRule>& Existing) :
m_firewall(Firewall) m_firewall(Firewall)
{ {
wil::unique_bstr ruleName; wil::unique_bstr ruleName;

View File

@ -262,7 +262,7 @@ private:
/// <summary> /// <summary>
/// COM firewall instance. /// COM firewall instance.
/// </summary> /// </summary>
Microsoft::WRL::ComPtr<INetFwPolicy2> m_firewall; wil::com_ptr<INetFwPolicy2> m_firewall;
/// <summary> /// <summary>
/// Lock to protect class members. /// Lock to protect class members.
@ -295,7 +295,7 @@ public:
/// <summary> /// <summary>
/// Constructor to take ownership of an existing rule. /// Constructor to take ownership of an existing rule.
/// </summary> /// </summary>
LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const Microsoft::WRL::ComPtr<INetFwRule>& Existing); LxssNetworkingFirewallPort(const std::shared_ptr<LxssNetworkingFirewall>& Firewall, const wil::com_ptr<INetFwRule>& Existing);
/// <summary> /// <summary>
/// Destructor. /// Destructor.

View File

@ -2674,8 +2674,7 @@ try
THROW_IF_FAILED(shellLink->SetArguments(commandLine.c_str())); THROW_IF_FAILED(shellLink->SetArguments(commandLine.c_str()));
THROW_IF_FAILED(shellLink->SetIconLocation(ShortcutIcon, 0)); THROW_IF_FAILED(shellLink->SetIconLocation(ShortcutIcon, 0));
Microsoft::WRL::ComPtr<IPersistFile> storage; auto storage = shellLink.query<IPersistFile>();
THROW_IF_FAILED(shellLink->QueryInterface(IID_IPersistFile, &storage));
THROW_IF_FAILED(storage->Save(shortcutPath.c_str(), true)); THROW_IF_FAILED(storage->Save(shortcutPath.c_str(), true));
registration.Write(Property::ShortcutPath, shortcutPath.c_str()); registration.Write(Property::ShortcutPath, shortcutPath.c_str());

View File

@ -351,7 +351,7 @@ private:
wsl::shared::SocketChannel m_miniInitChannel; wsl::shared::SocketChannel m_miniInitChannel;
wil::unique_socket m_notifyChannel; wil::unique_socket m_notifyChannel;
SE_SID m_userSid; SE_SID m_userSid;
Microsoft::WRL::ComPtr<DeviceHostProxy> m_deviceHostSupport; wil::com_ptr<DeviceHostProxy> m_deviceHostSupport;
std::shared_ptr<LxssRunningInstance> m_systemDistro; std::shared_ptr<LxssRunningInstance> m_systemDistro;
_Guarded_by_(m_lock) std::bitset<MAX_VHD_COUNT> m_lunBitmap; _Guarded_by_(m_lock) std::bitset<MAX_VHD_COUNT> m_lunBitmap;
_Guarded_by_(m_lock) std::map<AttachedDisk, DiskState> m_attachedDisks; _Guarded_by_(m_lock) std::map<AttachedDisk, DiskState> m_attachedDisks;

View File

@ -327,7 +327,7 @@ class PolicyTest
const auto stop = std::chrono::steady_clock::now() + std::chrono::seconds{30}; const auto stop = std::chrono::steady_clock::now() + std::chrono::seconds{30};
for (;;) for (;;)
{ {
Microsoft::WRL::ComPtr<ILxssUserSession> session; wil::com_ptr<ILxssUserSession> session;
result = CoCreateInstance(CLSID_LxssUserSession, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&session)); result = CoCreateInstance(CLSID_LxssUserSession, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&session));
if (result == expectedResult || std::chrono::steady_clock::now() > stop) if (result == expectedResult || std::chrono::steady_clock::now() > stop)
{ {

View File

@ -2448,8 +2448,7 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND
// Validate that the shortcut is actually in the start menu // Validate that the shortcut is actually in the start menu
VERIFY_IS_TRUE(shortcutPath.find(startMenu) != std::string::npos); VERIFY_IS_TRUE(shortcutPath.find(startMenu) != std::string::npos);
Microsoft::WRL::ComPtr<IPersistFile> storage; auto storage = shellLink.query<IPersistFile>();
VERIFY_SUCCEEDED(shellLink->QueryInterface(IID_IPersistFile, &storage));
VERIFY_SUCCEEDED(storage->Load(shortcutPath.c_str(), 0)); VERIFY_SUCCEEDED(storage->Load(shortcutPath.c_str(), 0));