十一、2008年3月18日
作者:青青子衿
email:[email protected]
1、 分析Cbot的成員函數 bool CBot :: HandleCommand ( CMessage * pMsg ) 時,遇到關於 CInstaller 類的障礙,於是先分析 CInstaller 類
class CInstaller
{
public :
bool CopyToSysDir ( CString & sFilename );
bool Install ();
bool Uninstall ();
#ifdef WIN32
bool RegStartAdd ( CString & sValuename , CString & sFilename );
bool RegStartDel ( CString & sValuename );
#endif
CString m_sSysDir ;
};
該類有5個成員函數, 和一個CString類型的成員變量
2、 int GetCopies ( CString & sFilename ) 函數
/////////////////////////////////////////////////////////////////
//
//函數功能:得到系統中公有幾個相同的bot在運行
//參數: CString &sFilename BOT 的文件名稱
//返回值: 返回bot進程的數目
//
/////////////////////////////////////////////////////////////////
int GetCopies ( CString & sFilename )
{
#ifdef WIN32
char cFilename [ MAX_PATH ];
GetModuleFileName ( GetModuleHandle ( NULL ), cFilename , sizeof ( cFilename ));
//查找在系統中總共有幾個該程序備份在運行
if ( fCreateToolhelp32Snapshot && fProcess32First && fProcess32Next )
{
psnap = fCreateToolhelp32Snapshot (2, 0);
if ( psnap != INVALID_HANDLE_VALUE )
{
int copies =0;
pe32 . dwSize = sizeof ( PROCESSENTRY32 );
if ( fProcess32First ( psnap , & pe32 ))
{
do { if (( strncmp ( cFilename +( strlen ( cFilename )- strlen ( pe32 . szExeFile )),
pe32 . szExeFile , strlen ( pe32 . szExeFile ))==0) ||
( strncmp ( sFilename , pe32 . szExeFile , strlen ( pe32 . szExeFile ))==0))
copies ++;
} while ( fProcess32Next ( psnap , & pe32 ));
}
CloseHandle ( psnap );
return copies ; //返回系統中運行的bot的數目
}
else return 0;
}
else return 0;
//Linux平臺下,
#else
//在linux平臺只能判斷有沒有,不能判斷,具體的數目。
char szCmdBuf [4096];
sprintf ( szCmdBuf , "ps ax | grep %s | grep -v grep > psaxtemp" , sFilename . CStr ());
system ( szCmdBuf ); //執行系統命令,將當前系統中的進程名稱包含該文件名sFilename的進程,寫入psaxtemp文件。
FILE * fp = fopen ( "psaxtemp" , "r" ); //打開保存有進程信息的文件
if (! fp )
{
//如果文件打開失敗,返回0
return 0;
}
//獲得文件的長度
fseek ( fp , 0, SEEK_END );
long lFileSize = ftell ( fp );
fseek ( fp , 0, SEEK_SET );
if ( lFileSize >0) //如果文件大於0返回1。
{
return 1;
}
fclose ( fp ); system ( "rm -f psaxtemp" ); return 0;
#endif // WIN32
}