Geeks With Blogs
.NET Nomad What I've learned along the way

In my copious amount of free time I've been messing around with network analysis and security.  I've always been generally interested in networking technology, but have never really had much practical exposure to it.  Sometimes, however, it is nice to be able to analyze a network and see what kind of information is actually coming across the wire.  In my last article I mentioned a tool called WireShark which is a free, open source network analyzer aka packet sniffer.

WireShark is a great tool and has its own set of extension points, but I wanted lower level access to the packets being captured.  My understanding of the politics and genesis is lacking, but it seems like the WinPCap library is the Windows version of the libpcap packet capture library from the *NIX world.  Naturally, WinPCap is coded in C and even though I have some background in it, the tool I am looking to develop requires a lot of UI work.  Instead of stepping back into the land of MFC/Win32, I tried to locate a Managed version of WinPCap.  The closest thing I could find was this Ancient Project on CodeProject.com.  It hasn't been updated since 2003 and isn't a "fully" managed wrapper (also, the source code in the download is just to the example, not the wrapper).

I figured, "If this guy can do PInvoke, so can I".  Thus, I downloaded the WinPCap developer pack and attempted to open the example solution in Visual Studio 2008.  Visual Studio 2008 alerted me to the fact that I had to upgrade the project (which was actually a VS 6.0 .dsw file) and I happily agreed.  The upgrade went smooth so I attempted to compile the solution, but received the following error:

"error C3163: '_vsnprintf': attributes inconsistent with previous declaration    c:\program files\Microsoft visual studio 9.0\vc\include\stdio.h    358    savedump"

Crap. Apparently this is a common problem when compiling older C++ code with the Visual Studio 2008 C++ compiler.  Now, I didn't find a solution for this on the net specific to WinPCap, but several forum posts across other projects lead me to the following solution.

First, find the pcap-stdinc.h file on your system. It should be located in: "...\WpdPack_4_0_2\WpdPack\Include"

Next, locate the following code near the bottom of your header:

#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define inline __inline

The problem, as we can tell from the compiler error, is that the "#define vsnprintf _vsnprintf" causes some incompatibilities with what is already in stdio.h.  Modify your code to the following and save the header:

#define snprintf _snprintf

#if !defined( __MINGW32__ )
# if _MSC_VER < 1500
    #define vsnprintf _vsnprintf
# endif
#endif

#define inline __inline

You should now be able to compile all the examples in the solution!

All that we've done is check the version of the compiler at compile time.  If the version is prior to MSC++ 9.0 then we go ahead and do the #define.  Otherwise, we don't do the #define and rely on what is in stdio.h.

This solution is general in nature, i.e. anything that defines _vsnprintf may exhibit this issue, but specific in the sense that the exact location of the code to modify will vary by project.  In the case of WinPCap, everything is groovy at this point.  Now I just need to learn everything I can about PInvoke : )

 
Posted on Wednesday, January 30, 2008 10:52 AM General .NET | Back to top


Comments on this post: WinPCap in Visual Studio 2008

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Hi there.
I didn't have that problem but I was getting a failure in netioapi.h not being able to resolve NDIS_PHYSICAL_MEDIUM.

This only seems to be happening on VS2008 running on Vista and seems to be a compatibility problem between the utilised header file at "\program files\microsoft sdks\windows\v6.0a\include\netioapi.h" not being able to resolve NDIS_PHYSICAL_MEDIUM. Principally, this is because netioapi.h is including <ntddndis.h> and the VS2008 shipped version of this file does not have the definition for NDIS_PHYSICAL_MEDIUM.

I fixed this (short term) by amending "\program files\microsoft sdks\windows\v6.0a\include\netioapi.h" and changing;

#include <ntddndis.h>

to

#include "ntddndis.h"

This causes netioapi.h to read its local ntddndis.h file, rather than the VS2008 version.
This gets my code to compile but I'm going to keep a close eye on VS2008 patches as I'd want to be referring to the "master" asap.

Cheers
Scott
Left by Skot on Feb 13, 2008 5:57 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Thank you for this post!!!!
You really saved me some time here, now I can press on with my progie. Keep it up!
Left by Blake on Feb 16, 2008 7:56 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Thank you Skot very-very-very much!!! Thank's to you I don't need to go back to VS 6.0.
Left by ZoliBoy on Feb 28, 2008 9:33 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Thanks Scott your tip helped me too.
Left by 97C5ENVY on Mar 26, 2008 5:24 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Thanks Skot. You're a lifesaver.
Left by l2noob on Jul 17, 2008 8:28 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
nice work. You should make a patch and send it to the Winpcap team. I'd do it myself but you should get credit :-).
Left by Joe on Sep 29, 2008 4:35 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
hello,please send me winpcap code with visual.
Left by isan on Nov 11, 2008 8:01 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
I worked around this by adding this line before including windows header files:

#define __IPHLPAPI_H__

This prevents inclusion of iphlpapi.h which includes the problematic ntddndis.h. This avoids having to mess around with library headers. Obviously, this solution won't work if you actually need iphpapi.h.
Left by Takin on Mar 02, 2009 5:09 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Hi,
thanks a lot, it really worked liek a charm. I was gettting crazy over this problem.
Left by adnan on Apr 24, 2009 10:15 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Thanks for the tips. I'm looking for a WinPcap coder who can deliver a desktop application and has experience with the library. do get in touch via my details at exceltasks ltd website.
Left by Nash on Sep 28, 2009 6:00 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
thank a lot dude, you're really saved my life:)
Left by pogo on Oct 05, 2009 5:20 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
thx a lot man.
but i am having some problems with creating new projects in vs08. i used to edit the examples in order to get everything working. what should i do in order to start a project from scratch? should i add libs? headers?
Left by scatman on Mar 07, 2010 10:25 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
hey , i've started with winpcap in c++ in visual studio 2008, and i have a very big problem , when i use in a consol program it work without problem but when it's on a "application form" i have this probleme :
error LNK2031: unable to generate p/invoke for "extern "C" char * __clrcall pcap_lookupdev(char *)" (?pcap_lookupdev@@$$J0YMPADPAD@Z); calling convention missing in metadata
1>nee.obj : error LNK2028: unresolved token (0A00000F) "extern "C" char * __clrcall pcap_lookupdev(char *)" (?pcap_lookupdev@@$$J0YMPADPAD@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
1>nee.obj : error LNK2019: unresolved external symbol "extern "C" char * __clrcall pcap_lookupdev(char *)" (?pcap_lookupdev@@$$J0YMPADPAD@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)

i tried every thing but i still lost,......pleeeeeez if anyone has an idea, pleeeeeeeeeaz help me, it's a project and i dont have enough time....
( and excuse my very bad english, i'm french)
Left by MARIA on Mar 11, 2010 1:37 AM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
Have you found a solution to this yet? I am having the exact same problem.
Left by Doug on Apr 07, 2010 3:19 PM

# re: WinPCap in Visual Studio 2008
Requesting Gravatar...
This website explains what you need to do. You will end up adding a bunch of typedef statements for the structures that your functions use. but it works.
Left by doug on Apr 10, 2010 9:15 AM

Your comment:
 (will show your gravatar)


Copyright © newman | Powered by: GeeksWithBlogs.net