locked
How to Access Firewall in C# on Visual Studio 2010

    Question

  • I have the code listed below which compiles and works perfectly on VS2008, but fails to work on VS2010 RTM. The INetFwMgr and INetFwAuthorizedApplication interfaces are simply not found in VS2010 only. This one method is keeping a huge 60 project solution from building in VS2010.

    In order to test compiling, c:\windows\system32\hnetcfg.dll needs to be added as a reference.

    Thanks for any help.

    Jim

    ----

    using System;

    using NetFwTypeLib;

     

    namespace TestFirewall

    {

    public class Firewall

    {

    static public bool AddApplicationToFirewallExceptions(string title, string applicationPath)

    {

    bool added = false;

    try

    {

    INetFwAuthorizedApplication fwApp = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication")) as INetFwAuthorizedApplication;

    fwApp.Name = title;

    fwApp.Enabled = true;

    fwApp.ProcessImageFileName = applicationPath;

    fwApp.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

    fwApp.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;

    INetFwMgr fwMgr = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")) as INetFwMgr;

    fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(fwApp);

    added = true;

    }

    catch { }

    return added;

    }

    }

    }


    Friday, April 23, 2010 1:34 AM

Answers

  • Hi Jim,

    I tried the code you posted, and it compiles fine over here. ( works in my machine... ;) )
    You might want to remove all the (non .NET) references in the project that doesn't build, and re-add them by browsing to the file again. And be sure to clean out the bin and obj folders. Visual Studio generates some interop assemblies, and I can imagine those generated by VS2008 aren't being accepted by VS2010.

    To be specific, these files are generated, so these files should be removed:

    • Interop.NATUPNPLib.dll
    • Interop.NETCONLib.dll
    • Interop.NetFwTypeLib.dll

    Hope this helps.


    Mark the best replies as answers - Twitter: @rickvdbosch - my blog: http://bloggingabout.net/blogs/rick
    • Marked as answer by Bin-ze Zhao Thursday, April 29, 2010 5:08 AM
    Friday, April 23, 2010 9:48 AM
  • Hi,

    I have run your code on my machine, window7 OS, vs2010 RTM, and it's running fine.

    To testing the code I added:

    c:\windows\system32\hnetcfg.dll

    c:\windows\system32\ FirewallAPI.dll

    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 Thursday, April 29, 2010 5:08 AM
    Wednesday, April 28, 2010 7:26 AM

All replies

  • Hi Jim,

    I tried the code you posted, and it compiles fine over here. ( works in my machine... ;) )
    You might want to remove all the (non .NET) references in the project that doesn't build, and re-add them by browsing to the file again. And be sure to clean out the bin and obj folders. Visual Studio generates some interop assemblies, and I can imagine those generated by VS2008 aren't being accepted by VS2010.

    To be specific, these files are generated, so these files should be removed:

    • Interop.NATUPNPLib.dll
    • Interop.NETCONLib.dll
    • Interop.NetFwTypeLib.dll

    Hope this helps.


    Mark the best replies as answers - Twitter: @rickvdbosch - my blog: http://bloggingabout.net/blogs/rick
    • Marked as answer by Bin-ze Zhao Thursday, April 29, 2010 5:08 AM
    Friday, April 23, 2010 9:48 AM
  • I have tried both a completely clean project as well as completely removing all references and deleting the obj and bin folder.

    I am just curious as to whether you compiled with VS2010 pre-release or RTM. I was able to compile this code with earlier beta releases. It is only with the RTM that it has stopped working.

    Jim

    Friday, April 23, 2010 4:46 PM
  • Another simple way to add a firewall exception is running these windows commands manually or dynamically:

     

    Win Vista and Seven:

    netsh  advfirewall firewall add rule name=”MY_RULE_NAME” dir=in action=allow program=PROGRAM_PATH enable=yes

     

    Win XP:

    netsh  firewall set allowedprogram PROGRAM_PATH  MY_RULE_NAME  ENABLE

     

    Change PROGRAM_PATH with the application path and  MY_RULE_NAME with a name for this exception.


    Visit my blog: http://csharptips.wordpress.com
    Saturday, April 24, 2010 5:28 AM
  • Same problem here! It depends on the target framework you are choosing. Framework 4 does not, but everything prior to it does have the problem.

    After referencing hnetcfg.dll the above mentioned Interop Objects are visible in object explorer. When targeting framework 2.0 or 3.5 Interop.NetFwTypeLib shows the same interfaces like Interop.NetConLib. Using framework 4.0 each Interop Object shows its own interfaces correctly.

    I assume that's a bug of VS2010.

    Anyone having further information?

    Monday, April 26, 2010 11:48 AM
  • Hi,

    I have run your code on my machine, window7 OS, vs2010 RTM, and it's running fine.

    To testing the code I added:

    c:\windows\system32\hnetcfg.dll

    c:\windows\system32\ FirewallAPI.dll

    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 Thursday, April 29, 2010 5:08 AM
    Wednesday, April 28, 2010 7:26 AM
  • @Bin-ze Zhao

    The above mentioned problems seam to occur only in one specific combination:

    - Windows XP 32bit
    - VS2010
    - Framework other than 4.0

    On my Win7 machine everything works fine.

    Any further ideas?

    Friday, April 30, 2010 3:00 PM
  • I am having the same problem as Christoph43.

    - Windows XP SP3 32 bit

    - Framework 3.5

    - VS2010

    And it doesn't work.  As soon as I change the Target Framework to ".NET Framework 4" then it works fine.

    However, I need to target .NET Framework 3.5.

     

    Tuesday, May 04, 2010 7:26 PM
  • Hi Wally

    I have the same problem. Let me know if you find a solution

    Monday, July 12, 2010 8:32 AM
  • Hi:

     

    I have the same problem. A very simple test project that compiled and ran fine under VS2008, can not find the references in NATUPNPLib.

    Environment: OS Win7 64 bit, VS 2010 Pro. (release).

    I deleted hnetcfg.dll, added it back, deleted the interop dlls, let them be regenerated.

    No Joy. Is it back to 2008 now?

    I hope there is a resolution to this problem.

     

    Thanks Alex

     

    Wednesday, July 28, 2010 7:14 PM