Add a firewall rule for Distributed Transaction Coordinator (msdtc.exe)
Asked Answered
R

1

8

I tried to use firewallAPI.dll to add a rule. It works fine for calc.exe (or some other files) as described bellow but fails for msdtc.exe with the following exception:

System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'

Example:

static void Main(string[] args)
{
    var manager = GetFirewallManager();
    if (manager.LocalPolicy.CurrentProfile.FirewallEnabled)
    {
        var path = @"C:\Windows\System32\calc.exe";
        //var path = @"C:\Windows\System32\msdtc.exe"; // System.IO.FileNotFoundException: 'The system cannot find the file specified.
        AuthorizeApplication("Test", path, NET_FW_SCOPE_.NET_FW_SCOPE_ALL, NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
    }
}

private const string CLSID_FIREWALL_MANAGER =
    "{304CE942-6E39-40D8-943A-B913C40C9CD4}";

private static NetFwTypeLib.INetFwMgr GetFirewallManager()
{
    Type objectType = Type.GetTypeFromCLSID(
        new Guid(CLSID_FIREWALL_MANAGER));
    return Activator.CreateInstance(objectType)
        as NetFwTypeLib.INetFwMgr;
}

private const string PROGID_AUTHORIZED_APPLICATION =
    "HNetCfg.FwAuthorizedApplication";
public static bool AuthorizeApplication(string title, string applicationPath,
    NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion)
{
    // Create the type from prog id
    Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION);
    INetFwAuthorizedApplication auth = Activator.CreateInstance(type)
        as INetFwAuthorizedApplication;
    auth.Name = title;
    auth.ProcessImageFileName = applicationPath;
    auth.Scope = scope;
    auth.IpVersion = ipVersion;
    auth.Enabled = true;

    INetFwMgr manager = GetFirewallManager();
    manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth);
    return true;
}

Note: I checked the folder and see the file is located properly... Could anybody help to add firewall rule for Distributed Transaction Coordinator? Maybe I should try to add another file to firewall (not msdtc.exe)?

Rath answered 2/6, 2017 at 22:56 Comment(0)
Q
8

Project > Properties > Build tab, untick the "Prefer 32-bit" checkbox. You don't prefer it, there is no 32-bit version of msdtc.exe.

Why the file system redirector caused the FileNotFoundException is explained well in this MSDN article.

Quintinquintina answered 5/6, 2017 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.