@@ -335,7 +335,7 @@ const std::wstring LxssNetworkingFirewall::s_FriendlyNamePrefix(L"WSLRULE_177744
335335
336336LxssNetworkingFirewall::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
341341void LxssNetworkingFirewall::CopyPartialArray (SAFEARRAY* Destination, SAFEARRAY* Source, ULONG DestinationIndexStart, ULONG SourceIndexStart, ULONG ElementsToCopy)
@@ -388,8 +388,7 @@ void LxssNetworkingFirewall::CopyPartialArray(SAFEARRAY* Destination, SAFEARRAY*
388388
389389std::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
559557void 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;
0 commit comments