none
NetFwTypeLib and ComElevation dllhost fails to exit RRS feed

  • Question

  • I am using the comelevation sample from the vistabridge library in the windows sdk to elevate a .net class I exported to a com typelib.

    To demonstrate what I have done consider something like this:

        class Firewall:IDisposable
        {
            INetFwPolicy2 policy = (INetFwPolicy2)Activator.CreateInstance(...);
           
            bool FirewallEnabled
            {
                get { return policy.get_FirewallEnabled(profileType2); }
                set { policy.set_FirewallEnabled(profileType2, value); }
            }
    
            void EnableRule(string group, string ruleName, bool enable)
            {
                IEnumerator enumerator = null;
    
                try
                {
                    enumerator = this.policy.Rules.GetEnumerator();
    
                    while (enumerator.MoveNext())
                    {
    
                        INetFwRule iNetRule = (INetFwRule)enumerator.Current;
    
                        if (string.Equals(iNetRule.Grouping, group, StringComparison.OrdinalIgnoreCase))
                        {
                            if (string.Equals(iNetRule.Name, ruleName, StringComparison.OrdinalIgnoreCase))
                            {
                                iNetRule.Enabled = enable;
                                Marshal.ReleaseComObject(iNetRule);
                                break;
                            }
                        }
                        Marshal.ReleaseComObject(iNetRule);
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
    
    
    

     

    public void Dispose()

     

    Marshal.ReleaseComObject(policy.Rules);

     

    Marshal.ReleaseComObject(policy);

     

    }

    }

     

    {

     

    When I create my elevated com object and change the value of the property, all is well. When I do the same and call the function, it works but the dllhost process fails to exit. I am calling Marshal.ReleaseComObject on my elevated class. I narrowed the problem down to: Any time I iterate through the INetFwPolicy2.Rules the dllhost process fails to exit. In the above codeblock the function calls Marshal.Release... on each rule, I have tried without this call as well to no avail.

    Any thoughts?

    1000001 1100001 1110010 1101111 1101110 100000 1000001 1110011 1101000 1100101 1110010
    Monday, October 26, 2009 11:59 PM

Answers

  • You missed at least one interface pointer, INetFwRules returned by the Rules property.  Then there's the IUnknown pointer returned by INetFwRules::get__NewEnum().  That's a hard one, it got nicely translated by .NET to IEnumerable so that you can use a foreach loop to iterate the collection.

    Clearly, explicitly releasing the COM interface pointers is not a really great idea.  COM interop was designed to let the garbage collector release the pointers.  If you absolute have to ensure the .exe exits at a predictable time (why?), you'll need to use GC.Collect().

    Hans Passant.
    • Marked as answer by eryang Monday, November 2, 2009 7:48 AM
    • Unmarked as answer by element109 Monday, November 2, 2009 10:08 AM
    • Marked as answer by element109 Saturday, November 14, 2009 5:00 AM
    Tuesday, October 27, 2009 1:21 AM
    Moderator