⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccams.cpp

📁 一个网络监视的程序
💻 CPP
字号:
// CCAMS.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "CCAMS.h"

#include "MainFrm.h"
#include "CCAMSDoc.h"
#include "CCAMSView.h"
#include "ActivClient.h"
#include "MulThreadSever.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCCAMSApp

BEGIN_MESSAGE_MAP(CCCAMSApp, CWinApp)
	//{{AFX_MSG_MAP(CCCAMSApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCCAMSApp construction

CCCAMSApp::CCCAMSApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


/////////////////////////////////////////////////////////////////////////////
// CCCAMSApp initialization

//===============================================================
// 函 数 名:InitInstance
// 功能描述:初始化临界区资源, 数据源, 及其它初始化工作
//===============================================================
BOOL CCCAMSApp::InitInstance()
{
	// 初始化临界区资源
	extern CRITICAL_SECTION g_criClientAccess;
	extern CRITICAL_SECTION g_criCpuAccess;
	extern CRITICAL_SECTION g_criFileAccess;
	extern CRITICAL_SECTION g_criMailAccess;
	extern CRITICAL_SECTION g_criNetAccess;
	extern CRITICAL_SECTION g_criProcAccess;

	InitializeCriticalSection( &g_criClientAccess );
	InitializeCriticalSection( &g_criCpuAccess );
	InitializeCriticalSection( &g_criFileAccess );
	InitializeCriticalSection( &g_criMailAccess );
	InitializeCriticalSection( &g_criNetAccess );
	InitializeCriticalSection( &g_criProcAccess );

	extern CRITICAL_SECTION g_criActiveClientList;
	InitializeCriticalSection( &g_criActiveClientList );

	extern CRITICAL_SECTION g_criNewAccess;
	InitializeCriticalSection( &g_criNewAccess );

	// 自动配置ODBC数据源: StuInf, 退出时删除它
	// 删除原有的数据源: CCAMS
	SQLRemoveDSNFromIni( "CCAMS\0" );
	// 一定要放在函数 ProcessShellCommand 之前, 
	//展否则 LeftView::InitUpdate 会被调用, 出现错误

/*	// 查找数据文件是否存在
	char path[_MAX_PATH];
	if ( GetCurrentDirectory( sizeof(path), path ) == 0 )
	{
		AfxMessageBox("读取当前路径失败!");
		return false;
	}
	CString strCurPath( path );
	if ( strCurPath.Right(1) != '\\' )
		strCurPath += '\\';
	strCurPath += "CCAMS.mdb";
*/
	TCHAR exeFullPath[_MAX_PATH];
	CString T_CStr_FileName="";
	GetModuleFileName(NULL,exeFullPath,_MAX_PATH);
	for(int i=0;exeFullPath[i]!='\0';i++)//TCHAR to CString 
		T_CStr_FileName+=exeFullPath[i];
	
	int T_StrStart=T_CStr_FileName.ReverseFind('\\');//cat appName
	CString t_StrEXE=T_CStr_FileName.Mid(T_StrStart+1);
	
	// 文件的完整路径
	T_CStr_FileName.Replace(t_StrEXE,"CCAMS.mdb");//add create File name 

	WIN32_FIND_DATA	wfd;	//The WIN32_FIND_DATA structure describes a file found by 
	HANDLE hFile = ::FindFirstFile( T_CStr_FileName, &wfd );
	if ( hFile == INVALID_HANDLE_VALUE )
	{
		AfxMessageBox("读取数据库文件失败!\n"
			"请确认数据库文件名为CCAMS.mdb\n"
			"且与可执行文件有同一目录下");
		return false;
	}

	char dsn[] = "DSN=CCAMS\0";
	char des[] = "Description=Central Computer Activity Monitor System\0";
	char type[] = "FileType=Access\0";
	CString strSource("DBQ=");
	strSource += T_CStr_FileName;

	char pSet[500];
	char *p = pSet;
	memcpy( p, dsn, sizeof(dsn) );
	p += (sizeof(dsn)-1);
	memcpy( p, des, sizeof(des) );
	p += (sizeof(des)-1);
	memcpy( p, type, sizeof(type) );
	p += (sizeof(type)-1);
	memcpy( p, strSource.GetBuffer(0), strSource.GetLength() );
	p += strSource.GetLength();
	*p = '\0';	p++;
	*p = '\0';	p++;

	if ( !SQLConfigDataSource(
		NULL,
		ODBC_ADD_DSN,
		"Microsoft Access Driver (*.mdb)",
		pSet
		) )
	{
		AfxMessageBox( "配置数据源失败!" );
		return false;
	}

/*
	if ( !SQLConfigDataSource(
		NULL,
		ODBC_ADD_DSN,
		"Microsoft Access Driver (*.mdb)",
		"DSN=CCAMS\0"
		"Description=Central Computer Activity Monitor System\0"
        "FileType=Access\0"
        "DBQ=.\\CCAMS.mdb\0\0"
		) )
*/

	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}


	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CCCAMSDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CCCAMSView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CCCAMSApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CCCAMSApp message handlers


//===============================================================
// 函 数 名:ExitInstance
// 功能描述:程序退出时做的清理工作
//===============================================================
int CCCAMSApp::ExitInstance() 
{
	// 删除使用的临界区资源
	extern CRITICAL_SECTION g_criClientAccess;
	extern CRITICAL_SECTION g_criCpuAccess;
	extern CRITICAL_SECTION g_criFileAccess;
	extern CRITICAL_SECTION g_criMailAccess;
	extern CRITICAL_SECTION g_criNetAccess;
	extern CRITICAL_SECTION g_criProcAccess;

	DeleteCriticalSection( &g_criClientAccess );
	DeleteCriticalSection( &g_criCpuAccess );
	DeleteCriticalSection( &g_criFileAccess );
	DeleteCriticalSection( &g_criFileAccess );
	DeleteCriticalSection( &g_criMailAccess );
	DeleteCriticalSection( &g_criProcAccess );

	extern CRITICAL_SECTION g_criActiveClientList;
	DeleteCriticalSection( &g_criActiveClientList );

	extern CRITICAL_SECTION g_criNewAccess;
	DeleteCriticalSection( &g_criNewAccess );

	// 删除用过的数据源: CCAMS
	SQLRemoveDSNFromIni( "CCAMS\0" );
	
	return CWinApp::ExitInstance();
}

/////////////////////////////////////////////////////////////////////////////
// Gloable variables
// The one and only CCCAMSApp object

CCCAMSApp theApp;

// 全局变量, 维护当前活动主机列表
ActivClient theActiveClientList;
CRITICAL_SECTION g_criActiveClientList;

// 全局变量, 表示当前是否有新主机加入.
// History Diagram 主机列表根据此来判断是否需要重新载入活动列表
// 创建监听线程的时候, 初始化为 false
// 当有新主机加入时, 修改此变量的值为 true, 
// 主机列表更新后重设为 false
bool bNewAccess;
CRITICAL_SECTION g_criNewAccess;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -