Visual C++ 2010 - Windows Froms Application & WinPcap

Hello i am trying to make a packet capture application similar to Wireshark
with visual C++ 2010 - Windows Forms Application but i cant find any tutorials
how to set up Winpcap on my visual studio c++ 2010 - Windows Forms Application.
i don't want to use any other compiler because Windows Forms Application seems to be a easiest way to do this.

Please help me.
All help regarding this matter will be much appreciated.
Last edited on
There is no Wireshark for Visual Studio 2010 tutorial. Why would you think there is one?

How much of Wireshark do you intend to replicate? I'll tell you now, it's a huge task.
Sorry for not being 100% specific what i am trying to do here. You miss understood me.
I don't want a Wireshark for Visual Studio 2010 tutorial and i am not asking for one.
I want to instal Winpcap on my visual studio c++ 2010 and i want to use Windows Forms Application to create a simple listbox and add my captured packets to it.
And that's how much of Wireshark i want to replicate just to make a big listbox witch will be populated with packets.
I want to instal Winpcap on my visual studio c++ 2010
Actually what you need to do is install Winpcap on Windows.

i want to use Windows Forms Application to create a simple listbox and add my captured packets to it.
Once Winpcap is installed, you can you the pcap C interface to capture packets. I don't have the details to hand, but the idea is you first query pcap to see what interfaces it recognises. Then you open an interface and start reading packets from it. You'll get a header then the body. If the system can't keep up with the message rate, it'll drop parts of the body. You can detect this because the header lists the packet length and body length, which are the same unless parts of the body have been dropped.

You can also read using raw sockets on Windows without installing Winpcap, and you handle the packets in the same way. But you only see incomming packets, not outgoing (that was dropped in XP SP2).

Good luck.
Last edited on
After long search on google i returned.
And i have problems but i managed to set up WinPcap with visual studio 2010.

Before i start i want you to notice that im using visual c++ Windows Forms Application.

This is what i did:
1. Add preprocessor definitions WPCAP, HAVE_REMOTE
2. Add these lines of code in my stdafx.h file :
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wpcap.lib")
#include "pcap.h"
#include "stdlib.h"
#define LINE_LEN 16

and my project compiled with no errors but that's not the case.
Now i added this code below the #pragma endregion line in my Form1.h file :

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];

/* Retrieve the device list from the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
Application::Exit();

//fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
//exit(1);

}

/* Print the list */
for(d= alldevs; d != NULL; d= d->next)
{

++i;
d->name;
/*
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
*/
}

if (i == 0)
{
Application::Exit();

//printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
//return;
}

/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);


}

Just to see if it runs but i cant compile this code i get these errors :

error LNK2028: unresolved token (0A00000F) "extern "C" void __clrcall pcap_freealldevs(struct pcap_if *)" (?pcap_freealldevs@@$$J0YMXPAUpcap_if@@@Z) referenced in function "private: void __clrcall newapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@newapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>newapp.obj : error LNK2028: unresolved token (0A000011) "extern "C" int __clrcall pcap_findalldevs_ex(char *,struct pcap_rmtauth *,struct pcap_if * *,char *)" (?pcap_findalldevs_ex@@$$J0YMHPADPAUpcap_rmtauth@@PAPAUpcap_if@@0@Z) referenced in function "private: void __clrcall newapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@newapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>newapp.obj : error LNK2019: unresolved external symbol "extern "C" void __clrcall pcap_freealldevs(struct pcap_if *)" (?pcap_freealldevs@@$$J0YMXPAUpcap_if@@@Z) referenced in function "private: void __clrcall newapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@newapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>newapp.obj : error LNK2019: unresolved external symbol "extern "C" int __clrcall pcap_findalldevs_ex(char *,struct pcap_rmtauth *,struct pcap_if * *,char *)" (?pcap_findalldevs_ex@@$$J0YMHPADPAUpcap_rmtauth@@PAPAUpcap_if@@0@Z) referenced in function "private: void __clrcall newapp::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@newapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Icant understand any of this.
for me code looks fine.
Can any one please explain me what is happening where is these errors from how to fix this or if someone have a source code of a similar program on visual c++ 2010 Windows Forms Application please post it if you can.

All help will be much appreciated.
Those are link errors. It can't find the library that has:
pcap_freealldevs
pcap_findalldevs_ex
Now i was thinking that for some reason my compiler cant find these files:
"ws2_32.lib"
"wpcap.lib"
"pcap.h"

So under Project->Linker->General->Additional Library Directories
i added these directories:
C:\WpdPack_4_1_2\WpdPack
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib

Trying to compile... More MORE errors.

1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(53): error C2870: 'vc_attributes' : a namespace definition must appear either at file scope or immediately within another namespace definition
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C2146: syntax error : missing ';' before identifier 'SA_YesNoMaybe'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C2065: 'SA_YesNoMaybe' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2146: syntax error : missing ';' before identifier 'SA_Yes'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2377: 'YesNoMaybe' : redefinition; typedef cannot be overloaded with any other symbol
1> c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244) : see declaration of 'YesNoMaybe'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2065: 'SA_Yes' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2039: 'Yes' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245): error C2065: 'Yes' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2146: syntax error : missing ';' before identifier 'SA_No'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2370: 'YesNoMaybe' : redefinition; different storage class
1> c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245) : see declaration of 'YesNoMaybe'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2065: 'SA_No' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2039: 'No' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(246): error C2065: 'No' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2146: syntax error : missing ';' before identifier 'SA_Maybe'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2370: 'YesNoMaybe' : redefinition; different storage class
1> c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(245) : see declaration of 'YesNoMaybe'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2065: 'SA_Maybe' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2039: 'Maybe' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(247): error C2065: 'Maybe' : undeclared identifier
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(249): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(249): error C2039: 'AccessType' : is not a member of '`global namespace''
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(249): error C2146: syntax error : missing ';' before identifier 'SA_AccessType'
1>c:\Program Files)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(249): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
...............................................
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

There is is much more errors like these but i cant post text that long.

Please help me.
All help will be much appreciated.
You shouldn't need to do anything to get ws2_32.lib, that's WinSock 2 and necessary for everything these days.

I think WinPcap comes with another lib, but I rarely get onto a Windows box these days so I can't check. Check it out and add it if there is one.

As for the compile errors, you're mixing C# and C++. That's all a bit beyond me. Maybe someone else can help out on that part.
Try to create a new, UNMANAGED project and see if it builds and link correctly.
You shouldn't need to do anything to get ws2_32.lib, that's WinSock 2 and necessary for everything these days.

But this library is a requirement if you are using WinPcap in your programs.
As written on :
http://www.winpcap.org/docs/docs_40_2/html/group__wpcapsamps.html
Why i shouldn't need to do anything to get ws2_32.lib?

Try to create a new, UNMANAGED project and see if it builds and link correctly.
I created a console c++ project i set it up with WinPcap and it compiled and worked.
As a result i got my internet card name and bunch numbers.

But i don't want console i want GUI.
I want to populate ListBox with packets.

As for the compile errors, you're mixing C# and C++. That's all a bit beyond me. Maybe someone else can help out on that part.

On Windows Forms Application i only know how to use controls and how to populate them and how to add text i will need much more knowledge of C# & C++ in order to mix them and nobody will be able to help me because most of the functions you are using in c++ code you have to know in C# code in order to mix C# and C++

Now i am considering switching to Code::Blocks and learning some c++ GUI.
and making GUI myself in c++

So looks like again i was thinking that this will be easy but it turns out incredibly hard.



Why i shouldn't need to do anything to get ws2_32.lib?
You shouldn't need to fiddle around with paths to get it. If you can find the C and C++ runtime you will already be set up to find Winsock 2.

I created a console c++ project i set it up with WinPcap and it compiled and worked.
As a result i got my internet card name and bunch numbers.
That's good. You're set up and talking to winpcap. You do realise the packet will come back as a bunch of numbers that you have to decode, right?

Now i am considering switching to Code::Blocks
Ugh! You have to decide what language you want to use and use it. I've never just Code::Blocks, but I bet it doesn't have a C# compiler. I think it's an unreasonable decision.

I can't really add anything more until you decide what technologies you're going to use.
You can use Visual Studio built in resource editor for a drag and drop interface, you know. So create a new WTL or MFC project and create a dialog based application and add the controls into it.

It will be a native C++ application and it will work. (You are using sdk 7.0A, which means you have payed for version of VS)
You can use You can use Visual Studio built in resource editor for a drag and drop interface, you know. So create a new WTL or MFC project and create a dialog based application and add the controls into it.

It will be a native C++ application and it will work., you know. So create a new WTL or MFC project and create a dialog based application and add the controls into it.

It will be a native C++ application and it will work.

No i didn't know anything about this
Visual Studio built in resource editor for a drag and drop interface
but i will make a google search on it.

You do realise the packet will come back as a bunch of numbers that you have to decode, right?

Yes i know that, but packet decoding i will leave for later first i have to add them to the ListBox. At the momen it doesn't matter to me what kind of format packets are because
i am not that far in to finishing my program.

Ugh! You have to decide what language you want to use and use it. I've never just Code::Blocks, but I bet it doesn't have a C# compiler. I think it's an unreasonable decision.


May be unreasonable but in my case its not because i know way more C++ then C#
that's why i chose Windows Forms Application , i was thinking that i will drop in the ListBox
and populating it with with packets will be as easy as populating ListBox with plain text.
But its not easy.

I will be switching to Code::Blocks and learning c++ GUI because i have more experience
in c++ and i believe that i will learn faster c++ GUI that learning enough C# in order for my code to compile in visual studio.
Some people might take a different approach....

BIG Thank you for all your help i understood everything i need to about this matter.




Topic archived. No new replies allowed.