📄 cleardiskdlg.cpp
字号:
*/
BOOL CClearDiskDlg::SendAlarmMsg( )
{
CListBox *pList;
CString strIp;
int nCount = 0;
int nIdx = 0;
DWORD dwStatus = 0;
wchar_t wcServer[10];
wchar_t wcMsgname[20];
wchar_t wcFrom[128];
wchar_t wcBuf[60];
char cServer[10];
char cMsgname[20];
char cFrom[128];
char cBuf[60];
pList = (CListBox *)GetDlgItem( IDC_IPLIST );
if (!pList)
{
return FALSE;
}
nCount = pList->GetCount();
if (nCount < 1)
{
AfxMessageBox( "没有设置工作站地址" );
return FALSE;
}
memset( cServer, 0, sizeof( cServer ) );
memset( cMsgname, 0, sizeof( cMsgname ) );
memset( cFrom, 0, sizeof( cFrom ) );
memset( cBuf, 0, sizeof( cBuf ) );
memset( wcServer, 0, sizeof( wcServer ) );
memset( wcMsgname, 0, sizeof( wcMsgname ) );
memset( wcFrom, 0, sizeof( wcFrom ) );
memset( wcBuf, 0, sizeof( wcBuf ) );
gethostname( cFrom, sizeof( cFrom ) );
strcpy( cBuf, "服务器硬盘空间不足,将删除部分历史文件,释放空间!" );
MultiByteToWideChar( NULL, NULL, cFrom, sizeof(cFrom), wcFrom, sizeof(wcFrom) );
MultiByteToWideChar( NULL, NULL, cBuf, sizeof(cBuf), wcBuf, sizeof(wcBuf) );
for (nIdx = 0; nIdx < nCount; nIdx ++ )
{
//取得信息发送的目标地址
pList->GetText( nIdx, strIp );
//发送警告信息
memset( cMsgname, 0, sizeof( cMsgname ) );
memset( wcMsgname, 0, sizeof( wcMsgname ) );
strcpy( cMsgname, strIp.GetBuffer(strIp.GetLength()) );
MultiByteToWideChar( NULL, NULL, cMsgname, sizeof(cMsgname), wcMsgname, sizeof(wcMsgname) );
dwStatus = NetMessageBufferSend( NULL, wcMsgname, wcFrom, (unsigned char *)wcBuf, wcslen( wcBuf )*2 );
if( NERR_Success == dwStatus )
{
//AfxMessageBox( "succeed" );
}
else
{
//AfxMessageBox( "failed" );
}
}
return TRUE;
}
/*
== ============================================================
== Function : SetButnStatus
== Description : 设置按钮的可操作状态
== Argument : bzStatus :TRUE 运行按钮可用
== FALSE 运行按钮不可用
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CClearDiskDlg::SetButnStatus( BOOL bzStatus )
{
CButton *pButn = NULL;
pButn = (CButton *)GetDlgItem( IDC_STOP );
pButn->EnableWindow( !bzStatus );
pButn = (CButton *)GetDlgItem( IDC_RUN );
pButn->EnableWindow( bzStatus );
pButn = (CButton *)GetDlgItem( IDCANCEL );
pButn->EnableWindow( bzStatus );
}
/*
== ============================================================
== Function : OnRun
== Description : 运行磁盘检测程序
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CClearDiskDlg::OnRun()
{
//运行前保存参数
WriteProfile( );
//每个目录启动一个操作线程
int nIdx = 0;
int nCount = 0;
CString strBuf;
CListBox *pLzPath = NULL;
pLzPath = (CListBox *)GetDlgItem( IDC_CLEAR );
if (!pLzPath)
{
return;
}
if( m_dwFreeSpace < 100 )
{
AfxMessageBox( "请设置剩余空间的值,当硬盘空间不足该值时,才会执行文件清理流程!\r\n建议该值不要设置太小,最好在500以上!" );
return;
}
nIdx = 0;
nCount = pLzPath->GetCount();
//没有目录可监测
if( nCount < 1 )
{
AfxMessageBox( "请添加目录!!!" );
return;
}
//改变按钮状态
SetButnStatus( FALSE );
for( nIdx = 0; nIdx < nCount; nIdx ++ )
{
//从路径列表中读取文件存放目录
pLzPath->GetText( nIdx, strBuf );
//创建磁盘操作实例
CSpace *pSpace = new CSpace;
pSpace->SetMsgWnd( m_hWnd );
pSpace->SetFreeSpace( m_dwFreeSpace );
pSpace->SetPath( strBuf );
m_lzSpace.AddTail( (CObject *)pSpace );
//启动处理线程
AfxBeginThread( threadFunc , (LPVOID)pSpace );
}
}
/*
== ============================================================
== Function : OnStop
== Description : 停止磁盘检测程序
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CClearDiskDlg::OnStop()
{
// TODO: Add your control notification handler code here
SetButnStatus( TRUE );
//停止线程
POSITION pos = NULL;
POSITION posbuf = NULL;
CSpace *pSpace = NULL;
for( pos = m_lzSpace.GetHeadPosition(); pos; )
{
posbuf = pos;
pSpace = (CSpace *)m_lzSpace.GetNext( pos );
if( pSpace->m_dwThreadId )
{
PostThreadMessage( pSpace->m_dwThreadId, WM_CLOSE, 0, 0 );
}
else
{
//线程已退出,则释放内存
delete pSpace;
pSpace = NULL;
m_lzSpace.RemoveAt( posbuf );
}
}
//释放线程占用的内存空间
// ......
DWORD dwStart = 0;
dwStart = ::GetTickCount();
while( m_lzSpace.GetCount() )
{
Sleep( 1000 );
//等待线程退出
for( pos = m_lzSpace.GetHeadPosition(); pos; )
{
posbuf = pos;
pSpace = (CSpace *)m_lzSpace.GetNext( pos );
if( !pSpace->m_dwThreadId )
{
//线程已退出,则释放内存
delete pSpace;
pSpace = NULL;
m_lzSpace.RemoveAt( posbuf );
}
Sleep( 1 );
}
//超时强制终止线程
if( ::GetTickCount() - dwStart > 10 * 1000 )
{
DWORD dwRet = 0;
for( pos = m_lzSpace.GetHeadPosition(); pos; )
{
posbuf = pos;
pSpace = (CSpace *)m_lzSpace.GetNext( pos );
if( !pSpace->m_dwThreadId )
{
//线程已退出,则释放内存
delete pSpace;
pSpace = NULL;
m_lzSpace.RemoveAt( posbuf );
}
else
{
::TerminateThread( (HANDLE)pSpace->m_dwThreadId, dwRet );
Sleep( 1 );
delete pSpace;
pSpace = NULL;
m_lzSpace.RemoveAt( posbuf );
}
Sleep( 1 );
}
}
}
}
void CClearDiskDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
/*
== ============================================================
== Function : CheckProfile
== Description : 检测配置文件是否存在,不存在则创建
== Argument :
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
void CClearDiskDlg::CheckProfile( )
{
CFileFind proffind;
CFile profile;
CString strPath;
CString strBuf;
int nIdx = 0;
int nCount = 10;
strPath.Format( "%s\\%s",m_strSysPath, m_strProfile );
if( proffind.FindFile( strPath ) == FALSE )
{
profile.Open( strPath, CFile::modeWrite | CFile::modeCreate );
//空间参数
strBuf.Format( "[SPACE]\r\nFREE=\r\n\r\n" );
profile.Write( strBuf, strBuf.GetLength() );
//路径参数
strBuf.Format( "[PATH]\r\n" );
profile.Write( strBuf, strBuf.GetLength() );
for( nIdx = 0; nIdx < nCount; nIdx ++ )
{
strBuf.Format( "P%d=\r\n", nIdx );
profile.Write( strBuf, strBuf.GetLength() );
}
strBuf.Format( "\r\n" );
profile.Write( strBuf, strBuf.GetLength() );
//IP地址参数
strBuf.Format( "[STATION]\r\n" );
profile.Write( strBuf, strBuf.GetLength() );
for( nIdx = 0; nIdx < nCount; nIdx ++ )
{
strBuf.Format( "IP%d=\r\n", nIdx );
profile.Write( strBuf, strBuf.GetLength() );
}
profile.Close( );
return;
}
}
/*
== ============================================================
== Function : WriteProfile
== Description : 参数写入配置文件
== Argument :
==
== Return : TRUE :写入成功
== FALSE :写入失败
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CClearDiskDlg::WriteProfile( )
{
CheckProfile( );
int nIdx = 0;
int nCount = 0;
CString strBuf;
CString strKeyName;
CListBox *pLzPath = NULL;
CListBox *pLzIp = NULL;
pLzPath = (CListBox *)GetDlgItem( IDC_CLEAR );
if (!pLzPath)
{
return FALSE;
}
pLzIp = (CListBox *)GetDlgItem( IDC_IPLIST );
if (!pLzIp)
{
return FALSE;
}
//保存可用空间值
m_dwFreeSpace = GetDlgItemInt( IDC_LIMITSPACE );
strBuf.Format( "%d", m_dwFreeSpace );
::WritePrivateProfileString( "SPACE", "FREE", strBuf, m_strSysPath+"\\"+m_strProfile );
//保存文件存放路径
nIdx = 0;
nCount = pLzPath->GetCount();
for( nIdx = 0; nIdx < nCount; nIdx ++ )
{
pLzPath->GetText( nIdx, strBuf );
strKeyName.Format( "P%d", nIdx );
::WritePrivateProfileString( "PATH", strKeyName, strBuf, m_strSysPath+"\\"+m_strProfile );
}
//重置空值
strBuf.Empty();
for( ; nIdx < 10; nIdx ++ )
{
strKeyName.Format( "P%d", nIdx );
::WritePrivateProfileString( "PATH", strKeyName, strBuf, m_strSysPath+"\\"+m_strProfile );
}
//保存工作站IP地址
nIdx = 0;
nCount = pLzIp->GetCount();
for( nIdx = 0; nIdx < nCount; nIdx ++ )
{
pLzIp->GetText( nIdx, strBuf );
strKeyName.Format( "IP%d", nIdx );
::WritePrivateProfileString( "STATION", strKeyName, strBuf, m_strSysPath+"\\"+m_strProfile );
}
//重置空值
strBuf.Empty();
for( ; nIdx < 10; nIdx ++ )
{
strKeyName.Format( "IP%d", nIdx );
::WritePrivateProfileString( "STATION", strKeyName, strBuf, m_strSysPath+"\\"+m_strProfile );
}
return TRUE;
}
/*
== ============================================================
== Function : ReadProfile
== Description : 从配置文件中读取参数写入界面控件中
== Argument :
==
== Return : TRUE :读取成功
== FALSE :读取失败
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
BOOL CClearDiskDlg::ReadProfile( )
{
char szBuf[MAXPATH];
int nIdx = 0;
DWORD dwRet = 0;
CString strKeyName;
CListBox *pLzPath = NULL;
CListBox *pLzIp = NULL;
pLzPath = (CListBox *)GetDlgItem( IDC_CLEAR );
if (!pLzPath)
{
return FALSE;
}
pLzIp = (CListBox *)GetDlgItem( IDC_IPLIST );
if (!pLzIp)
{
return FALSE;
}
//读取剩余空间值
m_dwFreeSpace = GetPrivateProfileInt( "SPACE", "FREE", 0, m_strSysPath+"\\"+m_strProfile );
SetDlgItemInt( IDC_LIMITSPACE, m_dwFreeSpace );
//读取文件存放路径
nIdx = 0;
while( TRUE )
{
dwRet = 0;
strKeyName.Format( "P%d", nIdx );
memset( szBuf, 0, MAXPATH );
dwRet = GetPrivateProfileString( "PATH", strKeyName, "", szBuf, MAXPATH, m_strSysPath+"\\"+m_strProfile );
if (dwRet > 0)
{
pLzPath->AddString( szBuf );
}
else
{
break;
}
nIdx ++;
}
//读取工作站IP地址
nIdx = 0;
while( TRUE )
{
dwRet = 0;
strKeyName.Format( "IP%d", nIdx );
memset( szBuf, 0, MAXPATH );
dwRet = GetPrivateProfileString( "STATION", strKeyName, "", szBuf, MAXPATH, m_strSysPath+"\\"+m_strProfile );
if (dwRet > 0)
{
pLzIp->AddString( szBuf );
}
else
{
break;
}
nIdx ++;
}
return TRUE;
}
void CClearDiskDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
void CClearDiskDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
//程序退出时保存参数
WriteProfile( );
}
/*
== ============================================================
== Function : threadFunc
== Description : 空间检测线程
== Argument : LPVOID p 传入线程的内存地址
==
== Return :
==
== Modification : 2006-08-15
== Writer : 金杰
== ============================================================
*/
UINT threadFunc( LPVOID p )
{
CSpace *pSpace = ( CSpace * )p;
DWORD dwStartTime = 0;
DWORD dwEnd = 0;
DWORD dwThreadId = 0;
MSG msg;
if( !pSpace )
{
return -1;
}
pSpace->m_dwThreadId = GetCurrentThreadId();
dwStartTime = ::GetTickCount();
while( TRUE )
{
if( PeekMessage( &msg , 0 , WM_CLOSE,WM_CLOSE,PM_NOREMOVE ) )
{
pSpace->m_dwThreadId = 0;
break ;
}
//半小时执行一次空间检测
dwEnd = ::GetTickCount();
dwEnd -= dwStartTime;
if( ::GetTickCount() - dwStartTime > 30 * 60 * 1000 )
{
dwStartTime = ::GetTickCount();
if( pSpace->IsFull() )
{
pSpace->Clear();
}
}
Sleep( 10 );
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -