1
I’m trying to bear a code of c++ to c#. My goal is to block a website using WFP. After translating the code, I seem to have missed something because when adding a filter I get 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 can’t find what’s wrong. The code is very long because it contains structure statements, Enumerators and call methods to the windows api. So I leave below just the snippet that is launching the Exception. The full code can be found in the Pastebin from of 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 had this problem for days. I posted a question on stackoverflow in English and I received some downvotes so know that I researched a lot to port this code (you do not have the necessary references in pinvoke.net) and, if something is not clear, please comment that I add any necessary information.
But that’s not the function of
Marshal.ThrowExceptionForHR
?– Thiago Lunardi
Its function is to actually launch Exception if hr is different from 0. I’m trying to figure out why hr is different from 0. I’m suspicious that there’s something wrong with my struct + Union translation, but I still haven’t found the right way to do it.
– Cleyton Bruno