none
Add C# application to firewall exception list of windows 7

    Question

  • Hi
    I want to add my aplication to the firewall exception list. it is working well with XP, but when in installing windows 7, it is added to the firewall exception list but the "Domain" is still unchecked. Therefore my aplication is blocked by the firewall. Here i copying the code.

    INetFwAuthorizedApplications

    applications;

     

    INetFwAuthorizedApplication application;

     

    private void btnAuthenticate_Click(object sender, EventArgs e)

    {

    application = (

    INetFwAuthorizedApplication)getInstance("INetAuthApp");

    application.Name =

    "myApp";

    application.ProcessImageFileName =

    @"C:\Program Files\EIS\Test\myApp.exe";

    application.Enabled =

    true;

     

    Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);

     

    INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);

    applications = (

    INetFwAuthorizedApplications)mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;

    applications.Add(application);

    }

    Please help!!!

    Regards
    Rajesh VR

    Friday, January 29, 2010 6:07 AM

Answers

  • This shall do the trick:

            public static INetFwMgr WinFirewallManager()

            {

                Type type = Type.GetTypeFromCLSID(

                    new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));

                return Activator.CreateInstance(type) as INetFwMgr;

            }

            public bool AuthorizeProgram(string title, string path,

                NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver)

            {

                Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");

                INetFwAuthorizedApplication authapp = Activator.CreateInstance(type)

                    as INetFwAuthorizedApplication;

                authapp.Name = title;

                authapp.ProcessImageFileName = path;

                authapp.Scope = scope;

                authapp.IpVersion = ipver;

                authapp.Enabled = true;

     

                INetFwMgr mgr = WinFirewallManager();

                try{

                    mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp);

                }

                catch(Exception ex){

                    System.Diagnostics.Trace.Write(ex.Message);

                    return false;

                }

                return true;

            }

    You'll need to access the Windows Firewall API and then you
    need the GUID and then you must use the ID = HNetCfg.FwAuthorizedApplication.

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    • Marked as answer by Rajesh V R Monday, February 01, 2010 12:33 PM
    Saturday, January 30, 2010 12:03 PM

All replies

  • This shall do the trick:

            public static INetFwMgr WinFirewallManager()

            {

                Type type = Type.GetTypeFromCLSID(

                    new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));

                return Activator.CreateInstance(type) as INetFwMgr;

            }

            public bool AuthorizeProgram(string title, string path,

                NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver)

            {

                Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");

                INetFwAuthorizedApplication authapp = Activator.CreateInstance(type)

                    as INetFwAuthorizedApplication;

                authapp.Name = title;

                authapp.ProcessImageFileName = path;

                authapp.Scope = scope;

                authapp.IpVersion = ipver;

                authapp.Enabled = true;

     

                INetFwMgr mgr = WinFirewallManager();

                try{

                    mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp);

                }

                catch(Exception ex){

                    System.Diagnostics.Trace.Write(ex.Message);

                    return false;

                }

                return true;

            }

    You'll need to access the Windows Firewall API and then you
    need the GUID and then you must use the ID = HNetCfg.FwAuthorizedApplication.

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    • Marked as answer by Rajesh V R Monday, February 01, 2010 12:33 PM
    Saturday, January 30, 2010 12:03 PM
  • Thank you very much dear friend

    You saved my lot of time :) . Its wiorking very well in Windows 7 !!!!
    Monday, February 01, 2010 12:33 PM
  • Thanks people for this code.

    It works fine. However, I am encountering a problem that needs to be solved. That is, the "NetFwTypeLib" object resides in hnetcfg.dll in XP while it is inside the FirewallAPI.dll in Vista.

    Since I need my application be authorized irrespective of the OS, how am I to tackle that problem? I cannot use both the .dlls can I? And it is a must that one code should be able to do this.
    Wednesday, April 21, 2010 2:17 AM
  • Thanks people for this code.

    It works fine. However, I am encountering a problem that needs to be solved. That is, the "NetFwTypeLib" object resides in hnetcfg.dll in XP while it is inside the FirewallAPI.dll in Vista.

    Since I need my application be authorized irrespective of the OS, how am I to tackle that problem? I cannot use both the .dlls can I? And it is a must that one code should be able to do this.

    Hi, You can use both the .dlls. Also you can refer to the code offered by Coder24.com.
    Monday, July 12, 2010 8:14 AM
  • Does anyone have the same solution in C/C++?
    Saturday, October 09, 2010 12:28 AM
  • Please find the same in the below link.

     

    <<http://msdn.microsoft.com/en-us/library/aa364726%28VS.85%29.aspx>>

     

    Hope this helps.

    Monday, February 07, 2011 7:27 AM
  • HI All,

    The above code allows the program to pass thru firewall with Domain profile checked.kindly can anyone let me know what i have to do if i want the remaining profiles public and private also checked.

    Thanks,

    Ram

    Wednesday, February 08, 2012 6:25 AM
  • Hi.

    I have been looking to use these methods to add a Firewall Exception to Winows 7.

    I am having trouble using "INetFwAuthorizedApplications" in my VS 2010 project as I don't have the correct reference.

    Found on MSDN namespace of "Microsoft.TeamFoundation.Common" which I can't seem to find.

    Also saw as reference to add "hnetcfg.dll", but still get following error when I try to use "INetFwAuthorizedApplications":

    "The type or namespace name 'INetFwAuthorizedApplication' could not be found (are you missing a using directive or an assembly reference?)".

    Any help appreciated.

    Wednesday, April 25, 2012 1:03 AM
  • varfirewallApplication=Activator.CreateInstance(typeasINetFwAuthorizedApplication;                 // Set the process image file nameif (firewallApplication!=null)                 {                     firewallApplication.ProcessImageFileName=imageFileName;                     firewallApplication.Name=registerName;                     firewallApplication.Scope=NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET;                     firewallApplication.Enabled=true;

    I am doing this, but after that only public option is checked in firewall rules. I have tried all

    NET_FW_SCOPE enums, but only domain is not checked. What I am doing wrong here?

    thanks,

                                   
    Thursday, December 26, 2013 8:17 AM
  • On Windows Vista and later, you should use INetFwRule rather than INetFwAuthorizedApplication.  Then, set the INetFwRule.Profiles property, as shown in the Adding an Application Rule example.

    In the Windows XP SP2 firewall API, I suppose you can access the Domain and Standard profiles by passing different values to INetFwPolicy::GetProfileByType and authorize the same application for each profile. However, the NET_FW_PROFILE_TYPE enumeration does not mention a Private profile at all, so I don't know if you can use this old API to authorize your application for that profile.

    Friday, December 27, 2013 6:45 PM