【探索wireshark】 動態調用libwireshark.dll中的函數

來源:互聯網
上載者:User

// 所有原創文章轉載請註明作者及連結
// blackboycpp(AT)gmail.com
// QQ群: 135202158

 

 

 

環境: windows xp sp3, visual studio 2010, wireshark-1.0.0, wireshark-1.0.0原始碼,gtk+-bundle_2.16.6-20100207_win32(開發包,包括glib,gtk+,gdk等等)

 

WIN32版的wireshark根目錄下有一個libwireshark.dll, 此DLL封裝了wireshark大部分功能函數(個人見解)。用Dependency Walker開啟,如下圖所示:

 

 

我們可以動態載入此DLL,並使用其中匯出的函數。

我做了一個小實驗,匯出其中的一個簡單函數val_to_str(), 此函數位於原始碼目錄:/epan/value_string.c,原型為const gchar* val_to_str(guint32 val, const value_string *vs, const char *fmt)

 

用Visual Studio建立一個Win32控制台項目,並添加glib編譯的支援,相關設定可見http://blog.csdn.net/owe/archive/2007/05/10/1603744.aspx

 

以下是實驗工程的主要原始碼:

#include "stdafx.h" #include <glib.h> /* 此結構取自於源碼:wireshark-1.0.0/epan/value_string.h */ typedef struct _value_string { guint32 value; const gchar *strptr; } value_string; const value_string vs[] = { {1, "aaa"}, {2, "bbb"}, {3, "ccc"} }; const value_string* vs2 = NULL; typedef void (*f_ep_init_chunk) (void); typedef void (*f_ep_free_all) (void); typedef const gchar* (*f_val_to_str)(guint32 val, const value_string *vs, const char *fmt); int _tmain(int argc, _TCHAR* argv[]) { HINSTANCE hDLL; f_ep_init_chunk ep_init_chunk; f_ep_free_all ep_free_all; f_val_to_str val_to_str; const gchar *ret; hDLL = ::LoadLibrary(_T("C://Program Files//Wireshark//libwireshark.dll")); if(!hDLL) { printf("Can't load DLL!/n"); } else { ep_init_chunk = (f_ep_init_chunk)::GetProcAddress(hDLL, "ep_init_chunk"); ep_free_all = (f_ep_free_all)::GetProcAddress(hDLL, "ep_free_all"); val_to_str = (f_val_to_str)::GetProcAddress(hDLL, "val_to_str"); if(!hDLL) { ::FreeLibrary(hDLL); printf("Can't get function!/n"); } else { printf("Function is ready!/n"); ep_init_chunk(); /*ret = val_to_str(2, vs, "msg - %d");*/ ret = val_to_str(2, vs2, "msg - %d"); printf("%s/n", ret); ep_free_all(); } } system("PAUSE"); return 0; }

 

 

需要注意的是當val_to_str()函數的第2個參數為NULL指標的時候,不加ep_init_chunk()會出錯,因為那種情況下會調用epan/emem.c中的某些函數來動態分配記憶體,而在調用這些函數之前,必須先用ep_init_chunk()進行初始化,最後用ep_free_all()清理(這是wireshark自己的記憶體管理機制emem.h/.c)。所以說仔細閱讀源碼還是必要的。

 

調試的話,加斷點就行了。調試過程中,如果問你要某個原始碼,你找到相應的wireshark源碼就行了,visual C++會進行定位。

 

編譯產生後,將產生的exe拷貝到wireshark的根目錄下,執行即可。把wireshark根目錄加到PATH環境變數都不行,老報找不到某個DLL。

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.