none
Can the CLSID of Firewall Manager Class be obtained programmatically by C# code?

    Question

  • I'm using the following code (which I found on the net) to register an exe as a Windows Firewall Exception using C# code.

     

    Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");
    INetFwAuthorizedApplication authorizedApp = Activator.CreateInstance(type) as INetFwAuthorizedApplication;
    
    // Set properties of authorizedApp here.
    
    Type objectType = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
    INetFwMgr firewallMgr = Activator.CreateInstance(objectType) as INetFwMgr;
    firewallMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authorizedApp);
    

     

    This code works fine and gets my job done, thought I don't fully understand the code. However, my problem is, can I obtain this CLSID of Firewall Manager Class, namely "{304CE942-6E39-40D8-943A-B913C40C9CD4}", programmatically, without hard coding it like here? Is this information stored somewhere in the OS or the .NET framework? It has to be there right?

     

    Tuesday, May 11, 2010 4:33 AM

Answers

  • Hi,

    Try the following code:

               RegistryKey regVersionString = Registry.ClassesRoot.OpenSubKey("HNetCfg.FwMgr");
                Console.WriteLine("VERSION: " + regVersionString.GetValue(""));

                RegistryKey regClassId = Registry.ClassesRoot.OpenSubKey(regVersionString.GetValue("") + "\\CLSID");
                Console.WriteLine("CLSID: " + regClassId.GetValue(""));

            

    Thanks

    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Tuesday, May 18, 2010 11:44 AM
    Tuesday, May 18, 2010 11:44 AM

All replies

  • I also happen to notice that this ID is has a registry entry in the registry in the following location.

     

    HKEY_CLASSES_ROOT/CLSID/{304CE942-6E39-40D8-943A-B913C40C9CD4}

     

    However, the problem is the key itself is the registry entry name so it is kind of no use to me here. What I need is a dynamic way of obtaining this key. Anybody has got any idea?

    Tuesday, May 11, 2010 4:47 AM
  • Hi,

    Try the following code:

               RegistryKey regVersionString = Registry.ClassesRoot.OpenSubKey("HNetCfg.FwMgr");
                Console.WriteLine("VERSION: " + regVersionString.GetValue(""));

                RegistryKey regClassId = Registry.ClassesRoot.OpenSubKey(regVersionString.GetValue("") + "\\CLSID");
                Console.WriteLine("CLSID: " + regClassId.GetValue(""));

            

    Thanks

    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Tuesday, May 18, 2010 11:44 AM
    Tuesday, May 18, 2010 11:44 AM