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

📄 pfwredun.cpp

📁 用C++编写的
💻 CPP
字号:
// pfwredun.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "pfwredun.h"
#include "pfwredunDlg.h"
#include "iphlpapi.h"
#include "Winsock2.h"
#include "limits.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPfwredunApp

BEGIN_MESSAGE_MAP(CPfwredunApp, CWinApp)
	//{{AFX_MSG_MAP(CPfwredunApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPfwredunApp construction

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

/////////////////////////////////////////////////////////////////////////////
// The one and only CPfwredunApp object

CPfwredunApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CPfwredunApp initialization

BOOL CPfwredunApp::InitInstance()
{

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	CString str ; 
  	if( cmdInfo.m_strFileName.IsEmpty() )
	{
		// Now check if in pfwredun.ini exist data after '='
	    char   szRedunFile  [ MAX_PATH ] ;
		char   szIPs [ MAX_PATH ] ;
		char   *pAdapters = new char [ SHRT_MAX ] ;
	    get_full_name( szRedunFile ) ;
		GetPrivateProfileString( ADAPTERS , NULL , "" , pAdapters , SHRT_MAX , szRedunFile ) ;
 		if( pAdapters )
			if( *pAdapters )
			{
				char *pWork = pAdapters ;
 				while ( *pWork )
				{
 		            GetPrivateProfileString( ADAPTERS , pWork , "" , szIPs , MAX_PATH , szRedunFile ) ;
 					if( !strlen( szIPs ) )
					{
 				        str.LoadString( IDS_INIFILE_NOTFILLEDIPS ) ;
				        AfxMessageBox( str ) ;
						delete [] pAdapters ;
						return FALSE ;
					}					
				    pWork += strlen( pWork ) + 1 ;
				}				
			}
			else
			{
				str.LoadString( IDS_INIFILE_NOTFILLED ) ;
				AfxMessageBox( str ) ;
				delete [] pAdapters ;
				return FALSE ;
			}
		else
		{
			str.LoadString( IDS_NO_MEMORY ) ;
			AfxMessageBox( str ) ;
			delete [] pAdapters ;
			return FALSE ;
		}
		delete [] pAdapters ;
	}
	else
	{
		cmdInfo.m_strFileName.MakeUpper() ;
		if( stricmp( cmdInfo.m_strFileName , INIT_STR ) )
		{
			if( stricmp( cmdInfo.m_strFileName , ATOM_STR ) )
			{
  	            if( ATOM atom = GlobalFindAtom( REDUNDACY_AGENT_STR ) )
                	GlobalDeleteAtom( atom ) ;
			}
			else
			{
			    str.LoadString( IDS_WRONG_INPUT_PAR ) ;
			    AfxMessageBox( str ) ;
			}
		}
		else
		{ // INIT . So make pfwredun.ini with [adapters] section 
		  //                                  MAC Address ( Description) = IP,IP,...
			if( create_ini_file() )
			    str.LoadString( IDS_INIFILE_DONE ) ;
			else
			    str.LoadString( IDS_INIFILE_NOTDONE ) ;
			AfxMessageBox( str ) ;
		}
		return FALSE ;
	}
	if( !GlobalFindAtom( REDUNDACY_AGENT_STR ) )
	{
		GlobalAddAtom( REDUNDACY_AGENT_STR ) ;
		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

		CPfwredunDlg dlg;
		m_pMainWnd = &dlg;
		int nResponse = dlg.DoModal();
		if (nResponse == IDOK)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with OK
		}
		else if (nResponse == IDCANCEL)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with Cancel
		}
	}
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


BOOL CPfwredunApp::create_ini_file( )
{
	BOOL   bRet = FALSE ;
	char   szRedunFile  [ MAX_PATH ] ;
	get_full_name( szRedunFile ) ;
	// Clean the [Adapters] section and start to create the keys
	if( WritePrivateProfileString( ADAPTERS , NULL , NULL , szRedunFile ) )
	{
		DWORD dwSize = 0 ;
		MIB_IFTABLE *IfTable = NULL ;
		GetIfTable( IfTable , &dwSize , FALSE ) ; // Ask the size
		IfTable = ( MIB_IFTABLE *)new BYTE [ dwSize ] ; 
		if( GetIfTable( IfTable , &dwSize , FALSE ) == NO_ERROR )
		{
			int ii = IfTable->dwNumEntries ;
			for( int i =0 ; i < ii ; i++ )
			{
				if( IfTable->table[i].dwPhysAddrLen  && check_mac_address_exist( IfTable , i ) )	
				{
					char szData   [ MAXLEN_IFDESCR ] ;
					CString cData ;
					cData.Format( "%02X %02X %02X %02X %02X %02X [" ,
						                                    IfTable->table[i].bPhysAddr[0],
															IfTable->table[i].bPhysAddr[1],
															IfTable->table[i].bPhysAddr[2],
															IfTable->table[i].bPhysAddr[3],
															IfTable->table[i].bPhysAddr[4],
															IfTable->table[i].bPhysAddr[5] ) ;
				    memcpy( szData , ( char *)IfTable->table[i].bDescr , IfTable->table[i].dwDescrLen  ) ;
				    szData[IfTable->table[i].dwDescrLen] = 0 ;
				    cData += szData ;
				    cData += "]" ;
	                if( WritePrivateProfileString( ADAPTERS , cData , "" , szRedunFile ) &&
						WritePrivateProfileString( SETTINGS , SUBNETMASK , SUBNETMASK_DEFAULT , szRedunFile ) )
						bRet = TRUE ;
				}
				else
					continue ;	
			}
			if( bRet )
				WritePrivateProfileString( SETTINGS , TIME , DEFAUL_TIME , szRedunFile ) ; 
		}
	    delete [ ] IfTable ;
	}
	return bRet ;
}

BOOL CPfwredunApp::check_mac_address_exist( MIB_IFTABLE *pmib_table , int Index )
{
	BOOL bRet   = FALSE ;
	for( DWORD i = 0 ; i < pmib_table->table[Index].dwPhysAddrLen ; i++ )
	{
		if( pmib_table->table[Index].bPhysAddr[i] )
		{
			bRet = TRUE ;
			break ;
		}	
	}
	return bRet ;
}

void get_full_name( char *szRedunFile )
{
	GetModuleFileName(NULL , szRedunFile , MAX_PATH ) ;
	char  *pbs = strrchr( szRedunFile , '\\' ) ;
	if( pbs )
		*(pbs + 1 ) = 0 ;
	strcat( szRedunFile , REDUNDACYINI ) ; //write redundancy file in the same directory as exe
}

int CPfwredunApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
  	if( ATOM atom = GlobalFindAtom( REDUNDACY_AGENT_STR ) )
        GlobalDeleteAtom( atom ) ;
	
	return CWinApp::ExitInstance();
}

⌨️ 快捷键说明

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