📄 pfwredundlg.cpp
字号:
for( int i = 0 ; i < m_aptr.GetSize() ; i++ )
{
pcdwarr = (CDWordArray *)m_aptr.GetAt(i) ;
pcdwarrContext = (CDWordArray *)m_adaptercontext.GetAt(i) ;
for( int ii = 0 ; ii < pcdwarr->GetSize() ; ii++ , index++ )
{
Address = pcdwarr->GetAt(ii) ;
USHORT nJustAttached = 2 ;
USHORT& nAttached = nJustAttached ;
m_map.Lookup( &pcdwarr[ index ] , nAttached ) ;
if( 0 == nJustAttached )
{
bRes = ping.Ping( Address ) ;
if( !bRes )
{
if( m_bTreadContinue )
{
nJustAttached = 1 ; // TRUE ;
m_map.SetAt( &pcdwarr[ index ] , nJustAttached ) ;
// Do real attachment here : m_adapterindex.GetAt(i) = Index of Adapter
ULONG NTEContext , NTEInstance ;
if( AddIPAddress( Address, // IP address to add
m_mask, // subnet mask for IP address
m_adapterindex.GetAt(i), // index of adapter
&NTEContext, // Net Table Entry context
&NTEInstance // Net Table Entry Instance
) == NO_ERROR )
{
pcdwarrContext->SetAt( ii , NTEContext ) ;
m_bGreen++ ;
}
else
{
CString str ;
str.LoadString( IDS_ERRORINADDIPADDRESS ) ;
AfxMessageBox( str ) ;
}
}
else
return TRUE ;
}
}
if( !bRes && !bGreenTemp && m_bTreadContinue )
{ // Let's take this IP to our adapter and change the icon if need
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.hWnd = AfxGetMainWnd()->m_hWnd ;
nid.uID = IDR_MAINFRAME;
Shell_NotifyIcon(NIM_DELETE, &nid);
nid.uID = IDI_ICON1;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP ;
nid.uCallbackMessage = WM_MY_TRAY_NOTIFICATION;
nid.hIcon = ::LoadIcon(AfxGetInstanceHandle() , MAKEINTRESOURCE(IDI_ICON1));
lstrcpyn(nid.szTip, m_strToolTip , sizeof(nid.szTip) / sizeof(nid.szTip[0]));
BOOL bRess = Shell_NotifyIcon(NIM_ADD, &nid);
}
}
}
return bRet ;
}
int CPfwredunDlg::GetIndexforAdapter( char *szAdapter , DWORD *pdwIndex )
{
int iRet = 0 ;
DWORD dwSize = 0 ;
CString str ;
int iMacAddr [ 6 ] = {0,0,0,0,0,0} ;
char szAdapterWork[ MAXLEN_IFDESCR + 20 + 2 ] ;
strcpy( szAdapterWork , szAdapter ) ;
MIB_IFTABLE *IfTable = NULL ;
GetIfTable( IfTable , &dwSize , FALSE ) ; // Ask the size
IfTable = ( MIB_IFTABLE *)new BYTE [ dwSize ] ;
if( IfTable )
{
char *pDesc = strchr( szAdapterWork ,'[' ) ;
if( pDesc )
{
*pDesc = 0 ;
sscanf( szAdapterWork , "%02X %02X %02X %02X %02X %02X" , &iMacAddr ,
&iMacAddr[1] , &iMacAddr[2] , &iMacAddr[3] ,&iMacAddr[4] , &iMacAddr[5] ) ;
if( GetIfTable( IfTable , &dwSize , FALSE ) == NO_ERROR )
{
int ii = IfTable->dwNumEntries ;
for( int i =0 ; i < ii ; i++ )
{
if( IfTable->table[i].dwPhysAddrLen &&
((CPfwredunApp *)AfxGetApp())->check_mac_address_exist( IfTable , i ) )
{
if( IfTable->table[i].bPhysAddr[0] == iMacAddr[0] &&
IfTable->table[i].bPhysAddr[1] == iMacAddr[1] &&
IfTable->table[i].bPhysAddr[2] == iMacAddr[2] &&
IfTable->table[i].bPhysAddr[3] == iMacAddr[3] &&
IfTable->table[i].bPhysAddr[4] == iMacAddr[4] &&
IfTable->table[i].bPhysAddr[5] == iMacAddr[5] )
{
*pdwIndex = IfTable->table[i].dwIndex ;
iRet = 1 ; // found
break ;
}
}
}
}
}
else
{
str.LoadString( IDS_WRONGCHANGESININI ) ;
AfxMessageBox( str ) ;
iRet = -1 ; // Message done
}
delete [ ] IfTable ;
}
else
{
str.LoadString( IDS_NO_MEMORY ) ;
AfxMessageBox( str ) ;
iRet = -1 ; // Message done
}
return iRet ;
}
UINT check_masterIPs_thread( LPVOID pthisdlg )
{
CPfwredunDlg *pDlg = (CPfwredunDlg *)pthisdlg ;
int iSleep = pDlg->m_Time / 10 ;
int iCount = 10 ;
while( pDlg->m_bTreadContinue )
{
pDlg->check_masters_IPs() ;
while( iCount-- )
{ // Sleep interrupted to check maybe thread have to be finished
Sleep(iSleep);
if( FALSE == pDlg->m_bTreadContinue )
break ;
}
iCount = 10 ;
}
pDlg->m_pcthread = NULL ;
SetEvent( pDlg->m_hEventThreadFinished ) ;
return 0 ;
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CString strOfAdapter ;
CPfwredunDlg *predundlg = ((CPfwredunApp *)AfxGetApp())->m_predundlg ;
int iSize = predundlg->m_aptr.GetSize() ;
switch ( iSize )
{
case 0 :
break ;
case 1 :
m_line2.SetWindowText( "" ) ;
m_line3.SetWindowText( "" ) ;
m_line4.SetWindowText( "" ) ;
m_line5.SetWindowText( "" ) ;
m_line6.SetWindowText( "" ) ;
break ;
case 2 :
m_line3.SetWindowText( "" ) ;
m_line4.SetWindowText( "" ) ;
m_line5.SetWindowText( "" ) ;
m_line6.SetWindowText( "" ) ;
break ;
case 3 :
m_line4.SetWindowText( "" ) ;
m_line5.SetWindowText( "" ) ;
m_line6.SetWindowText( "" ) ;
break ;
case 4 :
m_line5.SetWindowText( "" ) ;
m_line6.SetWindowText( "" ) ;
break ;
case 5 :
m_line6.SetWindowText( "" ) ;
break ;
default :
m_line6.SetWindowText( " . . ." ) ;
break ;
}
// TODO: Add extra initialization here
int iRes = 1 ;
char szIPs [ MAX_PATH ] , szIPsSign[ MAX_PATH ] ;
char *pAdapters = new char [ SHRT_MAX ] ;
GetPrivateProfileString( ADAPTERS , NULL , "" , pAdapters , SHRT_MAX , predundlg->m_szRedunFile ) ;
if( pAdapters && iSize )
{
char *pWork = pAdapters ;
int index = 0 ;
while ( *pWork && iRes <= iSize )
{
GetPrivateProfileString( ADAPTERS , pWork , "" , szIPs , MAX_PATH , predundlg->m_szRedunFile ) ;
if( strlen( szIPs ) )
{
// Here put before every IP with is attached a Attached Sign ( * )
// if exist attachment
CDWordArray *pcdwarr ;
char *szIPWork = szIPsSign ;
strcpy( szIPsSign , "" );
char *ptoken ;
char szSep [ 2 ] = {","} ;
ptoken = strtok( szIPs, szSep );
pcdwarr = (CDWordArray *)predundlg->m_aptr.GetAt(iRes-1) ;
USHORT nJustAttached = 2 ;
USHORT& nAttached = nJustAttached ;
while( ptoken )
{
predundlg->m_map.Lookup( &pcdwarr[ index ] , nAttached ) ;
if( 1 == nJustAttached )
strcat( szIPWork , "*") ;
strcat( szIPWork , ptoken ) ;
index++ ;
ptoken = strtok( NULL, szSep );
if( ptoken )
strcat( szIPWork, "," ) ;
}
strOfAdapter.Format("%s=%s" , pWork , szIPsSign ) ;
switch( iRes )
{
case 1 :
m_line1.SetWindowText( strOfAdapter ) ;
break ;
case 2 :
m_line2.SetWindowText( strOfAdapter ) ;
break ;
case 3 :
m_line3.SetWindowText( strOfAdapter ) ;
break ;
case 4 :
m_line4.SetWindowText( strOfAdapter ) ;
break ;
case 5 :
m_line5.SetWindowText( strOfAdapter ) ;
break ;
}
}
iRes++ ;
pWork += strlen( pWork ) + 1 ;
}
}
delete [] pAdapters ;
if( predundlg->m_bGreen )
{
// Check what first adapter and which IP ( number ) was added to it
CDWordArray *pcdwarr , *pcdwarrContext;
IPAddr Address ;
int index = 0 ;
BOOL bFound = FALSE ;
int i , ii , iiSize ;
USHORT nJustAttached = 2 ;
USHORT& nAttached = nJustAttached ;
for( i = 0 ; i < iSize ; i++ )
{
pcdwarr = (CDWordArray *)predundlg->m_aptr.GetAt(i) ;
pcdwarrContext = (CDWordArray *)predundlg->m_adaptercontext.GetAt(i) ;
iiSize = pcdwarr->GetSize() ;
for( ii = 0 ; ii < iiSize ; ii++ , index++ )
{
Address = pcdwarr->GetAt(ii) ;
predundlg->m_map.Lookup( &pcdwarr[ index ] , nAttached ) ;
if( 1 == nJustAttached )
{
bFound = TRUE ;
break ;
}
}
if( bFound )
break ;
}
m_static7.ShowWindow(SW_SHOW);
m_static8.ShowWindow(SW_SHOW);
if( predundlg->m_FirstPress == FALSE )
{
strOfAdapter.LoadString( IDS_DELETEWITHOUTPOLLING ) ;
m_button1.SetWindowText(strOfAdapter);
if( bFound )
{
strOfAdapter.Format("%d", i+1 ) ;
m_edit2.SetWindowText(strOfAdapter) ;
if( iSize == i+1 )
m_edit2.EnableWindow(FALSE) ;
strOfAdapter.Format("%d", ii+1 ) ;
m_edit3.SetWindowText(strOfAdapter) ;
if( iiSize == ii+1 )
m_edit3.EnableWindow(FALSE) ;
}
else
{
m_edit2.SetWindowText("" ) ;
m_edit3.SetWindowText("" ) ;
}
}
else
{
strOfAdapter.LoadString(IDS_PRESSAFTERMASTERARIZE) ;
m_button1.SetWindowText(strOfAdapter);
strOfAdapter.Format("%d", predundlg->m_iAdapter ) ;
m_edit2.SetWindowText(strOfAdapter) ;
if( iSize == predundlg->m_iAdapter )
m_edit2.EnableWindow(FALSE) ;
strOfAdapter.Format("%d", predundlg->m_iIP ) ;
m_edit3.SetWindowText(strOfAdapter) ;
if( iiSize == predundlg->m_iIP )
m_edit3.EnableWindow(FALSE) ;
}
m_edit2.ShowWindow(SW_SHOW);
m_edit3.ShowWindow(SW_SHOW);
m_button1.ShowWindow(SW_SHOW);
}
else
{
m_static7.ShowWindow(SW_HIDE);
m_static8.ShowWindow(SW_HIDE);
m_edit2.SetWindowText("" ) ;
m_edit3.SetWindowText("" ) ;
m_edit2.ShowWindow(SW_HIDE);
m_edit3.ShowWindow(SW_HIDE);
m_button1.ShowWindow(SW_HIDE);
}
m_edit1.SetLimitText( 3 ) ; // max 999 sec possible to enter
strOfAdapter.Format( "%d" , predundlg->m_Time / 1000 ) ;
m_edit1.SetWindowText(strOfAdapter) ;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAboutDlg::OnButton1()
{
// TODO: Add your control notification handler code here
// Read Adapter & IP and delete it , but only next Delete on these numbers will be
// start polling again ( press it after you initialize master )
int iAdapter , iIP ;
CString str , strOfAdapter ;
CPfwredunDlg *predundlg = ((CPfwredunApp *)AfxGetApp())->m_predundlg ;
int iSize = predundlg->m_aptr.GetSize() ;
CDWordArray *pcdwarr ;
m_edit2.GetWindowText(strOfAdapter) ;
sscanf( strOfAdapter , "%d" , &iAdapter ) ;
m_edit3.GetWindowText(strOfAdapter) ;
sscanf( strOfAdapter , "%d" , &iIP ) ;
if( ( iAdapter < 1 ) || ( iAdapter > iSize ) )
{
strOfAdapter.LoadString(IDS_ADAPTERLIMITS) ;
str.Format(strOfAdapter,iSize ) ;
AfxMessageBox(str);
return ;
}
pcdwarr = (CDWordArray *)predundlg->m_aptr.GetAt(iAdapter-1) ;
int iiSize = pcdwarr->GetSize() ;
if( ( iIP < 1 ) || ( iIP > iiSize ) )
{
strOfAdapter.LoadString(IDS_IPLIMITS) ;
str.Format(strOfAdapter,iiSize ) ;
AfxMessageBox(str);
return ;
}
// Now let's find index of nIP ( index is continuous number through all adapters
if( predundlg->m_FirstPress == FALSE )
{
predundlg->m_FirstPress = TRUE ;
predundlg->m_iAdapter = iAdapter ;
predundlg->m_iIP = iIP ;
// Do Delete Here and set new button text
ULONG NTEContext = ((CDWordArray *)predundlg->m_adaptercontext.
GetAt(predundlg->m_iAdapter-1))->GetAt(predundlg->m_iIP-1) ;
if( DeleteIPAddress( NTEContext ) == NO_ERROR )
{
str.LoadString(IDS_PRESSAFTERMASTERARIZE) ;
m_button1.SetWindowText(str);
}
else
{
str.LoadString( IDS_DELETEIPNOTSUCCESS ) ;
AfxMessageBox( str ) ;
}
}
else
{ // NOT First press so check that it's the same Adapter & IP
if( ( predundlg->m_iAdapter == iAdapter ) && ( predundlg->m_iIP == iIP ) )
{ // Initialize polling after master rejuvination
// If it was single change the green to blue of icon and down the attach sign
if( NULL == --predundlg->m_bGreen )
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(nid);
nid.hWnd = AfxGetMainWnd()->m_hWnd ;
nid.uID = IDI_ICON1;
Shell_NotifyIcon(NIM_DELETE, &nid);
nid.uID = IDR_MAINFRAME;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP ;
nid.uCallbackMessage = WM_MY_TRAY_NOTIFICATION;
nid.hIcon = ::LoadIcon(AfxGetInstanceHandle() , MAKEINTRESOURCE(IDR_MAINFRAME));
lstrcpyn(nid.szTip, predundlg->m_strToolTip , sizeof(nid.szTip) / sizeof(nid.szTip[0]));
BOOL bRess = Shell_NotifyIcon(NIM_ADD, &nid);
}
int index = 0 ;
for( int i = 0 ; i < iAdapter ; i++ )
{
pcdwarr = (CDWordArray *)predundlg->m_aptr.GetAt(i) ;
for( int ii = 0 ; ii < pcdwarr->GetSize() ; ii++ , index++ )
{
if( ii == ( iIP - 1 ) )
break ;
}
}
predundlg->m_map.SetAt(
&((CDWordArray *)predundlg->m_aptr.GetAt(predundlg->m_iAdapter-1))[index] , 0 ) ;
predundlg->m_FirstPress = FALSE ;
predundlg->m_iAdapter = predundlg->m_iIP = 0 ;
EndDialog( FALSE ) ;
}
else
{
strOfAdapter.LoadString(IDS_ADAPTERORIPNOTTHESAME) ;
str.Format(strOfAdapter,predundlg->m_iAdapter,predundlg->m_iIP ) ;
AfxMessageBox(str);
}
}
}
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
CPfwredunDlg *predundlg = ((CPfwredunApp *)AfxGetApp())->m_predundlg ;
int iTime = GetDlgItemInt( IDC_EDIT1 ) ;
if( iTime > 0 && iTime <= 999 )
predundlg->m_Time = iTime *1000 ;
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -