《深入浅出MFC第2版(PDF格式)》第304章


#0002 { 
#0003 #ifdef _DEBUG 
#0004 // all CDumpContext output is controlled by afxTraceEnabled 
#0005 if (!afxTraceEnabled) 
#0006 return; 
#0007 #endif 
#0008 
#0009 // use C…runtime/OutputDebugString when m_pFile is NULL 
#0010 if (m_pFile == NULL) 
#0011 { 
#0012 AfxOutputDebugString(lpsz); 
#0013 return; 
#0014 } 
#0015 
#0016 // otherwise; write the string to the file 
#0017 m_pFile…》Write(lpsz; lstrlen(lpsz)*sizeof(TCHAR)); 
#0018 } 
圖一 CDumpContext::OutputString 原始碼 
924 
…………………………………………………………Page 987……………………………………………………………
附錄D 以MFC 重建DBWIN
// in MFC 4。x AFX。H 
#0001 class CDumpContext 
#0002 { 
#0003 。。。 
#0004 public: 
#0005 CFile* m_pFile; 
#0006 }; 
圖二 CDumpContext 原始碼 
CMfxTrace 圖三 CFile 
好,如果我們能夠設計個類別 ( ),衍生自 ,然後為它設計 
個初始化成員函式,令函式之檢查 afxDump。m_pFile 內容,並且如果是 NULL ,就將 
它指向我們的新類別,那麼 CDumpContext::OutputString #17 行的 m_pFile…》Write 就會 
CMfxTrace Write 
因此指向新類別 的 函式,於是我們就可以在其予取予求啦。 
注意,theTracer 是個 static 成員變數,需要做初始化動作(請參考深入湷錾钊霚出MFC (侯 
深入湷錾钊霚出 
/
俊傑 松崗)第2章「靜態成員」節),因此你必須在類別之外的任何方加這行: 
CMfxTrace CMfxTrace::theTracer; 
#0001 class CMfxTrace : public CFile 
#0002 { 
#0003 static CMfxTrace theTracer; // one…and…only tracing object 
#0004 CMfxTrace(); // private constructor 
#0005 public: 
#0006 virtual void Write(const void* lpBuf; UINT nCount); 
#0007 static void Init(); 
#0008 }; 
#0001 // Initialize tracing。 Replace global afxDump。m_pFile with me。 
#0002 void CMfxTrace::Init() 
#0003 { 
#0004 if (afxDump。m_pFile == NULL) { 
#0005 afxDump。m_pFile = &theTracer; 
#0006 } else if (afxDump。m_pFile != &theTracer) { 
#0007 TRACE(〃afxDump is already using a file: TRACEWIN not installed。n〃); 
#0008 } 
#0009 } 
圖三 CMfxTrace 
925 
…………………………………………………………Page 988……………………………………………………………
第五篇 附錄
InterProcess munication IPC 
行程通訊( , ) 
CMfxTrace Write Write 
把 TRACE 輸出字串攔截到 :: 之後,我們得想辦法在 函式把字 
串丟給 Tracewin 程式視窗。在 Windows 3。1 之這不是難事,因為所有的行程 
process 
( )共同在同個位址空間生存,直接把字串指標(代表個邏輯位址)丟給 
對方就是了。在 Windows 95 和 Windows NT 困難度就高了許多,因為每個行程擁 
有自己的位址空間,你把字串指標丟給別的行程,對別的行程而言是洠в杏玫摹H绻恪?br />
Memory Map File Win32
為此使用到記憶體映射檔( , 之唯的種行程通訊方法), 
又似乎有點小睿笞鳌!?br />
Paul DiLascia global atom atom Win32
的方法是,利用所謂的 。 並不是 的新產物,有 
Win16 DDE Dynamic Data Exchange atom
過 ( ,動態資料交換)程式經驗的就知道, 被 
handle global atom
用來當作行程之間傳遞字串的識別物。說穿了它就是個 。 可以跨越 
CMfxTrace Write 
Win32 行程間的位址空間隔離。面是 :: 函式的動作步驟: 
1。 利用 FindWindow 找出 Tracewin 視窗。 
2。 利用 GlobalAddAtom 把字串轉換為個 global atom 。 
3。 Tracewin gloabl atom
送出個特定訊息給 ,並以述的 為參數。 
4。 刪除 global atom 。 
5。 Tracewin :: OutputDebugString TRACE
萬洠в姓业健 ∫暣埃艚小 。尅 【藜瘬碛小?br />
原本該有的行為。 
圖四 CMfxTrace Write 
就是 :: 函式碼。其的兩個常數分別定義為: 
#define TRACEWND_CLASSNAME 〃MfxTraceWindow〃 // 視窗類別名稱 
#define TRACEWND_MESSAGE 〃*WM_TRACE_MSG〃 // 自行裕缘摹indows 訊息 
#0001 // Override Write function to Write to TRACEWIN applet instead of file。 
#0002 // 
#0003 void CMfxTrace::Write(const void* lpBuf; UINT nCount) 
#0004 { 
#0005 if (!afxTraceEnabled) 
926 
…………………………………………………………Page 989………………………
小说推荐
返回首页返回目录