I have code that works great on Windows 7, I got help from this post ....
The problem occurs when the same code is compiled on a Win Xp Sp3 PC. Its not compiling ... says the interface is missing (error) ....
I can not understand. I again added a link to hnetcfg.dll (COM APi link) for windows xp (in Windows 7 I need to add nother dll " FirewallAPi.dll ") for the project
using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;
But still interfaces and other classes are not visible VS
pardon to publish all the code
Classes
Firewall manager
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;
namespace WindowsFirewallManager
{
public class FirewallHelper
{
#region Variables
private static FirewallHelper instance;
private INetFwMgr fireWallManager = null;
#endregion
#region Properties
public static FirewallHelper Instance
{
get
{
lock (typeof (FirewallHelper))
{
return instance ?? (instance = new FirewallHelper());
}
}
}
#endregion
#region Constructivat0r
private FirewallHelper()
{
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
fireWallManager = null;
if (fwMgrType != null)
{
try
{
fireWallManager =
(INetFwMgr) Activator.CreateInstance(fwMgrType);
}
catch (ArgumentException)
{
}
catch (NotSupportedException)
{
}
catch (TargetInvocationException)
{
}
catch (MissingMethodException)
{
}
catch (MethodAccessException)
{
}
catch (MemberAccessException)
{
}
catch (InvalidComObjectException)
{
}
catch (COMException)
{
}
catch (TypeLoadException)
{
}
}
}
#endregion
#region Helper Methods
public bool IsFirewallInstalled
{
get
{
return fireWallManager != null &&
fireWallManager.LocalPolicy != null &&
fireWallManager.LocalPolicy.CurrentProfile != null;
}
}
public bool IsFirewallEnabled
{
get
{
return IsFirewallInstalled &&
fireWallManager.LocalPolicy.CurrentProfile.
FirewallEnabled;
}
}
public bool AppAuthorizationsAllowed
{
get
{
return IsFirewallInstalled &&
!fireWallManager.LocalPolicy.CurrentProfile.
ExceptionsNotAllowed;
}
}
public void GrantAuthorization(string applicationFullPath,
string appName,
NET_FW_SCOPE_ scope,
NET_FW_IP_VERSION_ ipVersion)
{
#region Parameter checking
if (applicationFullPath == null)
throw new ArgumentNullException("applicationFullPath");
if (appName == null)
throw new ArgumentNullException("appName");
if (applicationFullPath.Trim().Length == 0)
throw new ArgumentException(
"applicationFullPath must not be blank");
if (applicationFullPath.Trim().Length == 0)
throw new ArgumentException("appName must not be blank");
if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0)
throw new ArgumentException(
"applicationFullPath must not contain invalid path characters");
if (!Path.IsPathRooted(applicationFullPath))
throw new ArgumentException(
"applicationFullPath is not an absolute path");
if (!File.Exists(applicationFullPath))
throw new FileNotFoundException("File does not exist",
applicationFullPath);
if (!IsFirewallInstalled)
throw new FirewallHelperException(
"Cannot grant authorization: Firewall is not installed.");
if (!AppAuthorizationsAllowed)
throw new FirewallHelperException(
"Application exemptions are not allowed.");
#endregion
if (!HasAuthorization(applicationFullPath))
{
Type authAppType =
Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication",
false);
INetFwAuthorizedApplication appInfo = null;
if (authAppType != null)
{
try
{
appInfo =
(INetFwAuthorizedApplication)
Activator.CreateInstance(authAppType);
}
catch (ArgumentException)
{
}
catch (NotSupportedException)
{
}
catch (TargetInvocationException)
{
}
catch (MissingMethodException)
{
}
catch (MethodAccessException)
{
}
catch (MemberAccessException)
{
}
catch (InvalidComObjectException)
{
}
catch (COMException)
{
}
catch (TypeLoadException)
{
}
}
if (appInfo == null)
throw new FirewallHelperException(
"Could not grant authorization: can't create INetFwAuthorizedApplication instance.");
appInfo.Name = appName;
appInfo.ProcessImageFileName = applicationFullPath;
appInfo.Scope = scope;
appInfo.IpVersion = ipVersion;
appInfo.Enabled = true;
fireWallManager.LocalPolicy.CurrentProfile.
AuthorizedApplications.Add(appInfo);
}
}
public void RemoveAuthorization(string applicationFullPath)
{
#region Parameter checking
if (applicationFullPath == null)
throw new ArgumentNullException("applicationFullPath");
if (applicationFullPath.Trim().Length == 0)
throw new ArgumentException(
"applicationFullPath must not be blank");
if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0)
throw new ArgumentException(
"applicationFullPath must not contain invalid path characters");
if (!Path.IsPathRooted(applicationFullPath))
throw new ArgumentException(
"applicationFullPath is not an absolute path");
if (!File.Exists(applicationFullPath))
throw new FileNotFoundException("File does not exist",
applicationFullPath);
if (!IsFirewallInstalled)
throw new FirewallHelperException(
"Cannot remove authorization: Firewall is not installed.");
#endregion
if (HasAuthorization(applicationFullPath))
{
fireWallManager.LocalPolicy.CurrentProfile.
AuthorizedApplications.Remove(applicationFullPath);
}
}
public bool HasAuthorization(string applicationFullPath)
{
#region Parameter checking
if (applicationFullPath == null)
throw new ArgumentNullException("applicationFullPath");
if (applicationFullPath.Trim().Length == 0)
throw new ArgumentException(
"applicationFullPath must not be blank");
if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0)
throw new ArgumentException(
"applicationFullPath must not contain invalid path characters");
if (!Path.IsPathRooted(applicationFullPath))
throw new ArgumentException(
"applicationFullPath is not an absolute path");
if (!File.Exists(applicationFullPath))
throw new FileNotFoundException("File does not exist.",
applicationFullPath);
if (!IsFirewallInstalled)
throw new FirewallHelperException(
"Cannot remove authorization: Firewall is not installed.");
#endregion
return
GetAuthorizedAppPaths().Cast<string>().Any(
appName =>
appName.ToLower() == applicationFullPath.ToLower());
}
public ICollection GetAuthorizedAppPaths()
{
if (!IsFirewallInstalled)
throw new FirewallHelperException(
"Cannot remove authorization: Firewall is not installed.");
ArrayList list = new ArrayList();
foreach (
INetFwAuthorizedApplication app in
fireWallManager.LocalPolicy.CurrentProfile.
AuthorizedApplications)
list.Add(app.ProcessImageFileName);
return list;
}
#endregion
}
}
An exception
using System;
namespace WindowsFirewallManager
{
public class FirewallHelperException : Exception
{
public FirewallHelperException(string message)
: base(message)
{ }
}
}
How can I make FirewallManager Compatibel with both versions of the OS or in any other way, if possible
thanks for the help....