DumontEXE 0.0.1
InterSpy.cpp
00001 // InterSpy.cpp: implementation of the CInterSpy class.
00002 //
00003 //////////////////////////////////////////////////////////////////////
00004 
00005 #include "stdafx.h"
00006 #include "InterSpy.h"
00007 #include "ISMsgManager.h"
00008 
00009 #include "afxpriv.h"
00010 #include <atlbase.h>
00011 
00012 #ifdef _DEBUG
00013 #undef THIS_FILE
00014 static char THIS_FILE[]=__FILE__; 
00015 #define new DEBUG_NEW
00016 #endif
00017 
00018 //////////////////////////////////////////////////////////////////////
00019 
00020 CInterSpy::CInterSpy() : m_dwFlags(0), m_hwndOutput(NULL)
00021 {
00022 }
00023 
00024 CInterSpy::~CInterSpy()
00025 {
00026 }
00027 
00028 BOOL CInterSpy::Initialize(DWORD dwFlags)
00029 {
00030     if (GetInstance().InitHooks(HM_CALLWNDPROC | HM_GETMESSAGE))
00031     {
00032         GetInstance().SetFlags(dwFlags);
00033 
00034         return TRUE;
00035     }
00036 
00037     return FALSE;
00038 }
00039 
00040 void CInterSpy::IncludeMsg(UINT uMsg)
00041 {
00042     GetInstance().CInterSpy::IncludeMsg(uMsg, TRUE);
00043 }
00044 
00045 void CInterSpy::ExcludeMsg(UINT uMsg)
00046 {
00047     GetInstance().CInterSpy::IncludeMsg(uMsg, FALSE);
00048 }
00049 
00050 void CInterSpy::OnCallWndProc(const MSG& msg)
00051 {
00052     ProcessMessage(msg);
00053 }
00054 
00055 void CInterSpy::OnGetMessage(const MSG& msg)
00056 {
00057     ProcessMessage(msg);
00058 }
00059 
00060 void CInterSpy::SetFlags(DWORD dwFlags) 
00061 {
00062     m_dwFlags = dwFlags;
00063 
00064     if (m_dwFlags & IS_NOKICKIDLE)
00065         IncludeMsg(WM_KICKIDLE, FALSE);
00066 
00067     HWND hwndOutput = GetOutputWnd(TRUE);
00068 
00069     if ((m_dwFlags & IS_RESETONSTART) && hwndOutput)
00070     {
00071         // build copy data msg
00072         IS_COPYDATA data;
00073         data.uCmd = IS_CMD_RESET;
00074         
00075         COPYDATASTRUCT cds = { 1, sizeof(data), &data};
00076         
00077         ::SendMessage(hwndOutput, WM_COPYDATA, 0, (LPARAM)&cds);
00078     }
00079 }
00080 
00081 void CInterSpy::IncludeMsg(UINT uMsg, BOOL bInclude)
00082 {
00083     if (bInclude)
00084         m_mapExclusions.RemoveKey(uMsg);
00085     else
00086         m_mapExclusions[uMsg] = 1;
00087 }
00088 
00089 HWND CInterSpy::GetOutputWnd(BOOL bStartup)
00090 {
00091     static BOOL bReentrancy = FALSE;
00092     if (bReentrancy)
00093         return NULL;
00094 
00095     bReentrancy = TRUE;
00096 
00097     if (!m_hwndOutput || !::IsWindow(m_hwndOutput))
00098         m_hwndOutput = NULL;
00099 
00100     if (!m_hwndOutput)
00101         m_hwndOutput = ::FindWindow("InterspyWnd", NULL);
00102 
00103     if (bStartup && !m_hwndOutput && (m_dwFlags & IS_AUTOSTARTOUTPUT))
00104     {
00105         // lookup cached path in registry
00106         if (m_sOutputWndPath.IsEmpty())
00107         {
00108             CRegKey reg;
00109             
00110             if (reg.Open(HKEY_LOCAL_MACHINE, "Software\\AbstractSpoon\\InterSpy") == ERROR_SUCCESS)
00111             {
00112                 char szPath[MAX_PATH + 1];
00113                 DWORD dwLen = MAX_PATH;
00114                 
00115                 if (reg.QueryValue(szPath, "InterSpyWndPath", &dwLen) == ERROR_SUCCESS)
00116                     m_sOutputWndPath = szPath;
00117             }
00118         }
00119         
00120         BOOL bCancelled = FALSE;
00121 
00122         do
00123         {
00124             if (m_sOutputWndPath.IsEmpty())
00125             {
00126                 if (IDCANCEL == MessageBox(NULL, "Interspy could not determine the location of the InterSpy output window.\n\nClick 'OK' to browse to the location of the InterSpy output window, \nor 'Cancel' if you which to start the output window manually", 
00127                     "InterSpy", MB_OKCANCEL))
00128                 {
00129                     bCancelled = TRUE;
00130                 }
00131                 else
00132                 {
00133                     CFileDialog dialog(TRUE, NULL, m_sOutputWndPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
00134                         "InterSpy output windows|InterSpyWnd.exe||");
00135                     
00136                     if (dialog.DoModal() == IDOK)
00137                     {
00138                         m_sOutputWndPath = dialog.GetPathName();
00139                     }
00140                     else
00141                         bCancelled = TRUE;
00142                 }
00143             }
00144 
00145             if (!bCancelled && !m_sOutputWndPath.IsEmpty())
00146             {
00147                 // try to run the exe
00148                 TRACE ("running '%s'\n", m_sOutputWndPath);
00149                 if ((int)ShellExecute(NULL, NULL, m_sOutputWndPath, NULL, NULL, SW_SHOW) > 32)
00150                 {
00151                     int nCount = 10;
00152                     
00153                     while (nCount-- && !m_hwndOutput)
00154                     {
00155                         m_hwndOutput = ::FindWindow("InterspyWnd", NULL);
00156                         Sleep(100);
00157                     }
00158                 }
00159             }
00160         }
00161         while (!m_hwndOutput && !bCancelled);
00162 
00163         if (bCancelled)
00164             m_dwFlags &= ~IS_AUTOSTARTOUTPUT;
00165         else
00166         {
00167             // write to registry
00168             CRegKey reg;
00169             
00170             reg.SetValue(HKEY_LOCAL_MACHINE, "Software\\AbstractSpoon\\InterSpy", m_sOutputWndPath, "InterSpyWndPath");
00171         }
00172     }
00173 
00174     bReentrancy = FALSE;
00175 
00176     return m_hwndOutput;
00177 }
00178 
00179 void CInterSpy::ProcessMessage(const MSG& msg)
00180 {
00181     // check exclusions
00182     static UINT uTemp;
00183 
00184     if (m_mapExclusions.Lookup(msg.message, uTemp))
00185         return;
00186 
00187     // check duplicates
00188     if (m_dwFlags & IS_NODUPLICATES)
00189     {
00190         UINT uLastMsg;
00191 
00192         if (m_mapLastMsgs.Lookup(msg.hwnd, uLastMsg) && uLastMsg == msg.message)
00193             return;
00194     }
00195 
00196     m_mapLastMsgs[msg.hwnd] = msg.message;
00197 
00198     HWND hwndOutput = GetOutputWnd();
00199 
00200     if (!hwndOutput)
00201         return;
00202 
00203     // else
00204     CString sMsg;
00205     CStringArray aParams;
00206 
00207     if (CISMsgManager::HandleMsg(&msg, sMsg, &aParams))
00208     {
00209         // build copy data msg
00210         IS_COPYDATA data;
00211 
00212         data.uCmd = IS_CMD_ADDMSG;
00213         data.wParam = (WPARAM)msg.hwnd;
00214         data.lParam = msg.message;
00215 
00216         int nParams = aParams.GetSize();
00217 
00218         if (nParams)
00219         {
00220             sMsg += '|';
00221 
00222             for (int nParam = 0; nParam < nParams; nParam++)
00223             {
00224                 sMsg += aParams[nParam];
00225 
00226                 if (nParam < nParams - 1)
00227                     sMsg += '|'; // delimeter
00228             }
00229         }
00230 
00231         lstrcpy(data.sMsg, sMsg);
00232 
00233         COPYDATASTRUCT cds = { 1, sizeof(data), &data};
00234 
00235         ::SendMessage(hwndOutput, WM_COPYDATA, 0, (LPARAM)&cds);
00236     }
00237 }
00238 
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com