1
votes

I have code that use firewall.dll to add a rule to windows firewall but i get this exception"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" this is my code:

using NetFwTypeLib;
namespace Tserver
{
    class Program
    {
    static void Main(string[] args)
    {

        INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
        Type.GetTypeFromProgID("HNetCfg.FWRule"));
                   firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
        firewallRule.Description = "Used to block all internet access.";
        firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
        firewallRule.Enabled = true;
        firewallRule.InterfaceTypes = "All";
        firewallRule.Name = "Block Internet";

        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(firewallRule);
}
1
Does the current user of operation system have the admin rights?Batu.Khan
You will need elevetated security level. Which will also imply UAC and so on. Btw it should think twice if you would REALLY chagne firewall settings by codeBoas Enkler

1 Answers

2
votes

You need to have admin rights to do what you're doing.

I've had the same problem a client machine, but as soon as I tried to do the same thing on a machine with administrator rights (Windows 7), everything worked perfectly.