?? mainfrm.cpp
字號:
///////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright 1999 - 2000 Mark Roddy
// All Rights Reserved
//
// Hollis Technology Solutions
// 94 Dow Road
// Hollis, NH 03049
// info@hollistech.com
//
// Synopsis:
//
//
// Version Information:
//
// $Header: /iphook/usr/IpMonitor/MainFrm.cpp 3 1/27/00 10:35p Markr $
//
///////////////////////////////////////////////////////////////////////////////
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "IpMonitor.h"
#include "MainFrm.h"
#include "winioctl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(START_HOOK, OnStartHook)
ON_COMMAND(STOP_HOOK, OnStopHook)
ON_UPDATE_COMMAND_UI(START_HOOK, OnUpdateStartHook)
ON_UPDATE_COMMAND_UI(STOP_HOOK, OnUpdateStopHook)
ON_MESSAGE(BUFFER_AVAILABLE_MSG, OnBufferMessage)
//}}AFX_MSG_MAP
// Global help commands
ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpFinder)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
m_Enabled = FALSE;
m_Started = FALSE;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if ((theApp.hookDriver.handle() == INVALID_HANDLE_VALUE) ||
(theApp.filterDriver.handle() == INVALID_HANDLE_VALUE)) {
m_Started = FALSE;
m_Enabled = FALSE;
} else {
m_Started = FALSE;
m_Enabled = TRUE;
}
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnStartHook()
{
if (theApp.hookDriver.handle() == INVALID_HANDLE_VALUE) {
m_Enabled = FALSE;
m_Started = FALSE;
return;
}
if (m_Started) {
return;
}
//
// send the start ioctl
//
DWORD bytesReturned;
BOOL result =
DeviceIoControl(theApp.hookDriver.handle(),
START_IP_HOOK, // operation control code
NULL, // input data buffer
0, // size of input data buffer
NULL, // output data buffer
0, // size of output data buffer
&bytesReturned, // byte count
NULL // overlapped information
);
if (FALSE == result) {
AfxErrorMessageBox(CString(_T("DeviceIoControl START_IP_HOOK")), GetLastError());
return;
}
//
// prime the pump with some buffers
//
if (FALSE == startSomeBuffers(4)) {
//
// ug, clean up the mess here
//
(void) DeviceIoControl(theApp.hookDriver.handle(),
STOP_IP_HOOK, // operation control code
NULL, // input data buffer
0, // size of input data buffer
NULL, // output data buffer
0, // size of output data buffer
&bytesReturned, // byte count
NULL // overlapped information
);
} else {
//
// if that worked, then check and disable this menu item
//
m_Started = TRUE;
}
}
void CMainFrame::OnStopHook()
{
if (theApp.hookDriver.handle() == INVALID_HANDLE_VALUE) {
m_Started = FALSE;
m_Enabled = FALSE;
return;
}
if (!m_Started) {
return;
}
//
// send the stop ioctl
//
DWORD bytesReturned;
BOOL result =
DeviceIoControl(theApp.hookDriver.handle(),
STOP_IP_HOOK, // operation control code
NULL, // input data buffer
0, // size of input data buffer
NULL, // output data buffer
0, // size of output data buffer
&bytesReturned, // byte count
NULL // overlapped information
);
if (FALSE == result) {
AfxErrorMessageBox(CString(_T("DeviceIoControl STOP_IP_HOOK")), GetLastError());
} else {
//
// if that worked then check and disable this menu item
//
m_Started = FALSE;
//
// wait for our worker thread to terminate
//
DWORD waitResult = WaitForSingleObject(workerHandle, 5000);
if (waitResult != WAIT_OBJECT_0) {
//
// terminate the worker thread with prejudice
//
TerminateThread(workerHandle, 1);
}
CloseHandle(workerHandle);
}
}
void CMainFrame::OnUpdateStartHook(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_Enabled);
pCmdUI->SetCheck(m_Started);
}
void CMainFrame::OnUpdateStopHook(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_Enabled);
if (m_Enabled) {
pCmdUI->SetCheck(!m_Started);
} else {
pCmdUI->SetCheck(FALSE);
}
}
IoContext::IoContext(UINT nbuffs)
{
if (nbuffs < 1) {
AfxErrorMessageBox(CString(_T("nBuffers too small:")), nbuffs);
ExitProcess(1);
//
// not reached
//
}
nbuffers = nbuffs;
//
// create an event
//
event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (event == NULL) {
DWORD error = GetLastError();
AfxErrorMessageBox(CString(_T("CreateEvent:")), error);
ExitProcess(error);
//
// not reached
//
}
rawBuffer = NULL;
buffer = NULL;
}
IoContext::~IoContext()
{
if (rawBuffer) {
delete[] rawBuffer;
}
if (event) {
CloseHandle(event);
}
}
bool IoContext::init()
{
rawBuffer = new UCHAR[sizeof(IPHOOK_BUFFER) + (sizeof(IPHOOK_DATA) * (nbuffers -1))];
if (rawBuffer == NULL) {
AfxErrorMessageBox(CString(_T("nBuffers too small:")), nbuffers);
return false;
//
// not reached
//
}
buffer = (IPHOOK_BUFFER*) rawBuffer;
overlapped.hEvent = event;
buffer->entries=nbuffers;
buffer->valid = 0;
buffer->tag = IPHOOK_BUFFER_TAG;
memset(&buffer->buffer[0], 0, sizeof(IPHOOK_DATA) * nbuffers);
return true;
}
DWORD WINAPI workerStart(
LPVOID lpParameter // thread data
)
{
WIN_CONTEXT context = *((WIN_CONTEXT *) lpParameter); // ignored for now
//
// until we get asked to stop,
// produce numBufs, wait for completion or termination,
// repeat as needed.
//
// we actually need to keep the buffers ordered, or else we
// will have a re-ordering problem on the other side. The easiest
// way to do that is to use only two buffers.
//
IoContext buffArray[2] = { 100, 100 };
//
// prime the pump
//
DWORD bytesReturned;
while (*context.pStarted == TRUE) {
for (UINT index = 0; index < 2; index++) {
//
// init allocates an io buffer and sets up the
// contents of the buffer.
//
buffArray[index].init();
BOOL result =
DeviceIoControl(theApp.hookDriver.handle(),
HOOK_THIS, // operation control code
NULL, // input data buffer
0, // size of input data buffer
buffArray[index].IoBuffer(), // output data buffer
buffArray[index].IoSize(), // size of output data buffer
&bytesReturned, // byte count
buffArray[index].over() // overlapped information
);
if (!result) {
AfxErrorMessage(CString(_T("DeviceIoControl HOOK_THIS: ")), GetLastError());
}
}
//
// as each request completes, poat it to the widow thread
//
for (index = 0; index < 2; index++) {
BOOL result =
GetOverlappedResult(theApp.hookDriver.handle(),
buffArray[index].over(),
&bytesReturned,
TRUE);
if (!result) {
AfxErrorMessage(CString(_T("GetOverlappedResult: ")), GetLastError());
//
// we have to free the buffer?
//
buffArray[index].deleteBuffer();
} else {
//
// post this to the window side
//
buffArray[index].releaseBuffer();
PostMessage(context.mainwindow,
BUFFER_AVAILABLE_MSG, (UINT) buffArray[index].IoBuffer(), 0);
}
}
}
return 0;
}
BOOLEAN CMainFrame::startSomeBuffers(UINT numBufs)
{
//
// this function spawns a worker thread whose job it is
// to feed the driver with buffers and wait for those buffers
// to return.
//
m_context.numBufs = 2;
m_context.mainwindow = GetSafeHwnd();
m_context.pStarted = &m_Started;
ASSERT(m_context.mainwindow);
workerHandle = CreateThread(NULL, 0, workerStart, (PVOID) &m_context, 0, &workerId);
if (workerHandle == NULL) {
AfxErrorMessage(CString(_T("CreateThread: ")), GetLastError());
return FALSE;
}
return TRUE;
}
afx_msg LRESULT CMainFrame::OnBufferMessage(WPARAM wParam, LPARAM lParam)
{
//
// just hand this to the view
//
CView* view = GetActiveView( );
if (view) {
view->PostMessage(BUFFER_AVAILABLE_MSG, wParam, lParam);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// Change History Log
//
// $Log: /iphook/usr/IpMonitor/MainFrm.cpp $
//
// 3 1/27/00 10:35p Markr
// Prepare to release!
//
///////////////////////////////////////////////////////////////////////////////
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -