This project is read-only.

Failed opening file Error

Nov 24, 2012 at 6:50 AM
Edited Nov 28, 2012 at 9:31 AM


i added the option to change ip in my code and i did it in 2 differents ways:

1. change single ip (to another ip)

2. change ip range: change specific ip and configure start ip and to how many ip i want to change this specific ip for example change the ip to and the range is 1000 ip's so i write a function that i send to this function the start ip ( and this function returns list of 1000 valid ip's start from

my problen is that after few hundred ip's the InvalidOperationExeption was unhandled by the user code and the error is Failed opening file C:\myFile.pcap.

this error didn't happen if  i change the ip range to 100 ip's, only if i am try to change to 1000 ip's its happen after few hundred  ip's.

Stack Trace:

at PcapDotNet.Core.OfflinePacketCommunicator.OpenFile(String filename)   at PcapDotNet.Core.OfflinePacketCommunicator..ctor(String fileName)   at PcapDotNet.Core.OfflinePacketDevice.Open(Int32 snapshotLength, PacketDeviceOpenAttributes attributes, Int32 readTimeout)   at packetPlayer.Pcap.playCapture(PacketDevice packetDevice) in d:\Drivers\PacketPlayer\PacketPlayer\classes\Pcap.cs:line 170   at PacketPlayer.MainWin.<>c__DisplayClass49.<btnPlay_Click>b__16(Object s3, DoWorkEventArgs e3) in d:\Drivers\PacketPlayer\PacketPlayer\mainWindows.cs:line 918   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)



this error cause too when i am sending the packet in regular way without changing the packet, my application has List Box that i am add all the pcap files inside, and after few hundred files this error cause again, maybe it is memory issue ?

Dec 1, 2012 at 8:13 PM

Hi gshhar,


It's hard to say without seeing some code.

Can you give a simple code snippet that causes this exception?



Dec 2, 2012 at 8:53 AM
Edited Dec 2, 2012 at 8:54 AM

My application takes a PCap file (Wireshark file) and sends the packets to the network card. If I send a file or files many times (usually ~500 times), my application crashes with the error Failed opening file C:\file.pcap. I tried to ask in the project forum, but the developer isn't there permanently, so maybe someone else could help me here.

The error cause is here in inputCommunicator, and when the error occurs, this object value is null. Please note that this happens only after a few hundred iterations (approximately 500).


using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
        using (mOutputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
            while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok && _isStop) //fill the buffer with the packets from the file
                using (PacketSendBuffer mSendBuffer = new PacketSendBuffer((uint)packet.Length * 4))
                    else if (packet != null)
                        lastTime = packet.Timestamp;
                        mSendBuffer.Enqueue(packet); //put the packet in the buffer

                    mOutputCommunicator.Transmit(mSendBuffer, _isBurst); //send the packet
Dec 2, 2012 at 10:30 AM
Edited Dec 2, 2012 at 10:32 AM

i also try your code sample Sending packets using Send Buffer, i took pcap file and run it in loop and again after 509 iteration same error

Dec 3, 2012 at 10:44 PM

Hi gshhar,


I've reproduced this and it seem to me like a bug in WinPcap.

I've sent an email to and I hope to get a response that would help me solve it.


When you create an OfflineCommunicator, Pcap.Net opens the file using _wfopen() (or fopen(), it doesn't matter), and then calls pcap_fopen_offline().

When you dispose your OfflineCommunicator, Pcap.Net calls pcap_close().

It seems that there are files that are left open and that's why you process crashes on opening too many files.

Calling fclose() only crashes the process faster.


I hope I'll find a solution for this soon,



Dec 16, 2012 at 2:06 PM

any new with the bug ?

Dec 28, 2012 at 4:06 PM

Unfortunately not.


Jan 17, 2013 at 11:16 AM
Edited Jan 17, 2013 at 11:40 AM

I am having the same problem, i could only open the files upto 509. I have used wireshark's file dumping method to write the packets. Then used the following way to process the packets.

                OfflinePacketDevice selectedDevice = new OfflinePacketDevice(filename);
                PacketCommunicator communicator =
                    communicator.ReceivePackets(0, DispatcherHandler);

   When the file count reaches 509 it shows an error that Failed to open the file . Is there any solution for this ?

     Following is the exception i got ...

    Failed opening file e:\packets\h_00510_20130117110311.Stack Trace  :   at PcapDotNet.Core.OfflinePacketCommunicator.OpenFile(String filename)   at PcapDotNet.Core.OfflinePacketDevice.Open(Int32 snapshotLength, PacketDeviceOpenAttributes attributes, Int32 readTimeout)   at PacketReader.PacketReader.PacketRead(String filename)



Jan 18, 2013 at 5:34 PM

Hi Gladiator123,


As I've mentioned above, this is a WinPcap bug as far as I understand.

You can open different processes to open different files.


I hope this helps,



Jan 19, 2013 at 6:19 AM
Edited Jan 19, 2013 at 6:21 AM

Ok . how can i do that. I need to read the files in one apllication itself. Is there any other way to read the packets? i just opened the file in other processes , but faced the same exception. Will you please suggest the way clearly?

Jan 19, 2013 at 6:43 AM
Edited Jan 19, 2013 at 7:05 AM


Jan 19, 2013 at 7:45 AM

I think you'll have to create a process for each 500 files read batch.

This will definitely make the code more complex, but I don't know how to solve it without the help of WinPcap people.

Mar 16, 2013 at 8:41 PM
Edited Mar 16, 2013 at 8:42 PM

new Winpcap version is available (4.1.3) but the crash is still occur.
maybe fix from your side is require ?

Apr 15, 2013 at 6:32 PM
Any updates to this significant issue?
Apr 15, 2013 at 7:58 PM
is it possible maybe to restart Winpcap service or something to avoid this crash ?
Apr 16, 2013 at 8:06 AM

Since there's no update in WinPcap on this, I'm considering removing support for non ASCII pcap filenames in the next version of WinPcap.
In order to implement non ASCII pcap filenames support I've used the less common WinPcap API, which seem to not work well for closing files.

I might keep a separate method for opening non ASCII filenames that will still cause this issue.

Apr 16, 2013 at 11:08 AM
when you think this version will be ready ?
Apr 20, 2013 at 11:57 AM
Hi gshhar,

No ETA for that.
This is purely volunteering so I get to it whenever I can prioritize time for that.

Apr 27, 2013 at 11:07 AM
I have fixed this issue in the code.
If you don't want to wait for the next Pcap.Net release, you can download and build the code.
Apr 28, 2013 at 1:08 AM
Edited Apr 28, 2013 at 1:12 AM
Good job Brickner! My solution was to use SharPcap and PcapDotNet together but would much rather just use PcapDotNet alone.

Was the problem items that werent being disposed properly by PcapDotNet?
Apr 30, 2013 at 12:35 AM
from where i can download this code ?
May 3, 2013 at 8:17 AM
The problem is still the bug in WinPcap.

I just avoid using the problematic functions when the filenames are ASCII or ISO-8859-1.
If the filenames are other, I still use the problematic functions without the ability to properly release the files resources.

You can download the code by going to Source Code of the Pcap.Net project here.
May 4, 2013 at 12:55 PM
Edited May 10, 2013 at 12:43 PM