先上代碼;
/*------------------------------------------------------------
win32, Winpcap, by bobo, 2018-09-09
------------------------------------------------------------*/
//#include <windows.h>
#include <pcap.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Winpcap"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
800, // initial x size
600, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
DWORD err;
pcap_if_t * allAdapters;
pcap_if_t * adapter;
char errorBuffer[PCAP_ERRBUF_SIZE];
int crtAdapter = 0;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &allAdapters, errorBuffer ) == -1 )
{
DrawText (hdc, TEXT (errorBuffer), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
return -1;
}
if( allAdapters == NULL )//不存在任何擴充卡
{
DrawText (hdc, TEXT ("No adapters found!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
return 0;
}
rect.top=5;
for( adapter = allAdapters; adapter != NULL; adapter = adapter->next)//周遊輸入擴充卡資訊(名稱和描述資訊)
{
DrawText (hdc, TEXT (adapter->name), -1, &rect,DT_SINGLELINE | DT_CENTER) ;
rect.top+=30;
DrawText (hdc, TEXT (adapter->description), -1, &rect,DT_SINGLELINE | DT_CENTER) ;
rect.top+=30; // 每輸出一行,Y坐标增加30
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
pcap_freealldevs( allAdapters); //釋放擴充卡清單
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
代碼是用Winpcap擷取本機網卡清單;
開發環境是Win10;VC++ 6.0;
先要安裝Winpcap的驅動和DLL;如果安裝了Wireshark封包捕獲工具,則Winpcap已經安裝好;沒有的話單獨下載下傳安裝;
再下載下傳Winpcap開發包,解壓至某個目錄;
編譯程式要把Windows.h 注釋掉;否則系統自帶的Winsock2.h 和 Winpcap的頭檔案中定義的宏有沖突;一堆錯誤;
第一次運作,沒有發現網卡;需要開啟NPF服務;
擷取到本機有8個網卡;
打開Wireshark軟體看一下;Wireshark也擷取到本機有8個網卡;其中包含一個VMWare虛拟機上的;
後面幾個圖是,添加包含檔案路徑,庫檔案路徑,lib檔案也要加到lib檔案清單;
為了使用Winpcap的遠端通路,必須在預處理器中加入HAVE_REMOTE;
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL9QjMZhXS6JWa50WWqpkMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zROBlL2QDNyUTMwMDM5AjMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)