I'm trying to port a c ++ code for C #. My goal is to block a site using WFP. After translating the code, it seems that I made a mistake because when I add a filter I receive the message "An enumerator is not valid" (FWP_E_INVALID_ENUMERATOR).
I've checked the code several times, including debugging along with the original code and I could not find what's wrong. The code is too long because it contains declarations of structures, enumerators, and methods of calling api from windows. Therefore, I leave below only the excerpt that you are throwing. The complete code can be found in the pastebin from this link .
var filterConditions = new List<FWPM_FILTER_CONDITION0_>();
filterConditions.Add(new FWPM_FILTER_CONDITION0_ {
fieldKey = FWPM_CONDITION_IP_REMOTE_ADDRESS,
matchType = FWP_MATCH_TYPE_.FWP_MATCH_EQUAL,
conditionValue = new FWP_CONDITION_VALUE0_
{
type = FWP_DATA_TYPE_.FWP_V4_ADDR_MASK,
v4AddrMask = site.ToIntPtr() // site é um objeto do tipo FWP_V4_ADDR_AND_MASK_
}
});
var filterIn = new FWPM_FILTER0_ {
subLayerKey = _subLayerGuid,
layerKey = FWPM_LAYER_INBOUND_IPPACKET_V4,
displayData = new FWPM_DISPLAY_DATA0_ { name = "Filter Name" },
action = new FWPM_ACTION0_ { type = NativeConstants.FWP_ACTION_BLOCK },
filterCondition = filterConditions.ToIntPtr(),
numFilterConditions = (uint)filterConditions.Count,
weight = new FWP_VALUE0_
{
type = FWP_DATA_TYPE_.FWP_UINT8,
uint8 = 0x00
}
};
var hr = NativeMethods.FwpmEngineOpen0(null, RPC_C_AUTHN_WINNT, IntPtr.Zero, IntPtr.Zero, ref _engineHandle);
Marshal.ThrowExceptionForHR((int)hr);
hr = NativeMethods.FwpmSubLayerAdd0(_engineHandle, ref subLayer, IntPtr.Zero);
Marshal.ThrowExceptionForHR((int)hr);
hr = NativeMethods.FwpmFilterAdd0(_engineHandle, ref filterIn, IntPtr.Zero, ref filterIn.filterId);
Marshal.ThrowExceptionForHR((int)hr); // aqui recebo a exception FWP_E_INVALID_ENUMERATOR
I've been having this problem for days unresolved. I put a question on the stackoverflow in English and I got some downvotes so I know I've done a lot of research to carry this code (do not have the necessary references on pinvoke.net) and if something is not clear, please comment that I add any necessary information. / p>