SEHException : External component has thrown an exception.

I'm getting the above exception in the following part of the code when I run the program.

Code:
            IntPtr fcode = IntPtr.Zero;
            bpf_program bpf = new bpf_program();
            fcode = Marshal.AllocHGlobal(Marshal.SizeOf(bpf));
            Marshal.StructureToPtr(bpf,fcode,false);
            string fter = "ip and tcp";
            if (wpcap_wrap.pcap_compile(ref adhandle, ref fcode, fter, 1, nmask) < 0)
            {
                Console.WriteLine("Unable to compile filter");
            }
            if (wpcap_wrap.pcap_setfilter(ref adhandle, ref fcode) < 0)
                Console.WriteLine("Error setting filter");
            Marshal.FreeHGlobal(fcode);
The exception occurs at this line
Code:
 if (wpcap_wrap.pcap_setfilter(ref adhandle, ref fcode) < 0)
I declared the method as follows
Code:
[DllImport("wpcap.dll")]
public static extern int pcap_setfilter(ref IntPtr p,ref IntPtr fp);
I'm trying to write an app that filters packets.
Any help would be greatly appreciated.