C語(yǔ)言輔導(dǎo):用vc設(shè)計(jì)系統(tǒng)援救程序

字號(hào):

一.引言
    Windows的system.dat和user.dat中存儲(chǔ)著windows注冊(cè)表,win.ini和system.ini中也存儲(chǔ)著一些重要的初始化信息,對(duì)它們進(jìn)行備份可以避免一些重裝系統(tǒng)之類(lèi)的繁瑣.雖然windos自帶一些備份和恢復(fù)系統(tǒng)信息的功能,但是使用起來(lái)很不方便.經(jīng)過(guò)一些研究,我發(fā)現(xiàn)你完全可以非常容易地設(shè)計(jì)出自己的系統(tǒng)援救程序.
    二.關(guān)鍵問(wèn)題分析
    1.在程序中調(diào)用系統(tǒng)現(xiàn)有的程序.
    使用WinExec(),ShellExecute(),CreateProcess()調(diào)用其它應(yīng)用程序的這三種方法基本已經(jīng)人所共知.但是有一些命令和程序它們不能調(diào)用,如DOS中的內(nèi)部命令和sys.com等一些外部命令.這時(shí),我們可以首先建立一個(gè).bat的批處理文件,然后再用這三種方法之一采用隱藏的方式來(lái)調(diào)用這個(gè)批處理文件.這樣我們可以在用戶(hù)不知不覺(jué)中,來(lái)使用一些系統(tǒng)現(xiàn)成的功能.
    2.如何備份系統(tǒng)信息
    可以調(diào)用windows目錄下command\目錄中的scanreg.exe來(lái)備份系統(tǒng)信息,用/backup參數(shù)可以使前面介紹的文件被壓縮成一個(gè)cab文件存儲(chǔ)于windows目錄下的sysbckup\目錄中,文件名自動(dòng)為rb000,rb001等.此命令不能直接調(diào)用,必須通過(guò)前面介紹的方法來(lái)使用它.如果你不想使用scanreg.exe,也可以使用同一個(gè)目錄中的extract.exe來(lái)生成cab文件,這樣雖然麻煩一些,但是靈活性較強(qiáng).
    注意,為了程序的通用性, windows目錄不應(yīng)根據(jù)你的計(jì)算機(jī)上的目錄直接指定,而應(yīng)使用GetWindowsDirectory()來(lái)獲得.
    3.如何恢復(fù)系統(tǒng)信息
    注冊(cè)表文件的恢復(fù)必須在純DOS方式下,因此你的程序應(yīng)讓系統(tǒng)重啟(使用ExitWindowsEx()),在沒(méi)進(jìn)入windows之前恢復(fù)系統(tǒng)信息.這可以通過(guò)修改autoexec.bat來(lái)實(shí)現(xiàn).如果在備份時(shí)你使用的是scanreg /backup,那么在恢復(fù)時(shí)你可以在autoexec.bat中加入scanreg /restore.如果你備份時(shí)使用的是extract 那么你可以將形式如下的一條語(yǔ)句加入到autoexec.bat中:
    extract /Y /L C:\windows myBackedFile.cab *.*
    另外除特殊情況外,在純DOS方式下一般不支持長(zhǎng)路徑名.因此在程序中將語(yǔ)句寫(xiě)入autoexec.bat時(shí),要先用GetShortPathName()來(lái)轉(zhuǎn)化為短路徑名.
    4.援救盤(pán)的創(chuàng)建
    恢復(fù)系統(tǒng)可分兩種情況.一種是用戶(hù)想把系統(tǒng)信息恢復(fù)成以前某次備份時(shí)的狀態(tài),此時(shí)可使用戶(hù)在程序中選擇要恢復(fù)的備份,然后程序控制重啟并在autoexec.bat中恢復(fù)即可.另一種情況則是用戶(hù)由于誤操作或其它原因是系統(tǒng)出錯(cuò)而不能進(jìn)入windows,因此要建立援救軟盤(pán),以使用戶(hù)能夠恢復(fù)系統(tǒng).援救盤(pán)的目的一個(gè)是啟動(dòng)系統(tǒng),可以通過(guò)windows目錄下command\目錄中的sys.com來(lái)實(shí)現(xiàn)(如sys c: a:).另外軟盤(pán)重要記住備份存放的位置,以便通過(guò)命令來(lái)恢復(fù)系統(tǒng).
    三.程序?qū)崿F(xiàn)
    1.在頭文件中加入:
    CString m_strWinDir(’ ’,_MAX_DIR);
    2.在構(gòu)造函數(shù)中加入:
    GetWindowsDirectory(m_strWinDir.GetBuffer(0),_MAX_DIR);
    m_strWinDir.ReleaseBuffer();
    3.加入一個(gè)進(jìn)展條,并用ClassWazid生成一個(gè)控制型變量m_progress.
    4.加入兩個(gè)函數(shù):
    CString CRescueSysDlg::getMyDir()//用來(lái)得到程序的當(dāng)前目錄.
    {
    TCHAR sFilename[_MAX_PATH];
    TCHAR sDrive[_MAX_DRIVE];
    TCHAR sDir[_MAX_DIR];
    TCHAR sFname[_MAX_FNAME];//不帶擴(kuò)展名
    TCHAR sExt[_MAX_EXT]; //擴(kuò)展名,前面有"."
    GetModuleFileName(AfxGetInstanceHandle(), sFilename, _MAX_PATH);
    _tsplitpath(sFilename, sDrive, sDir, sFname, sExt);
    CString homeDir(CString(sDrive) + CString(sDir));
    int nLen=homeDir.GetLength();
    if (homeDir.GetAt(nLen-1) != _T(’\\’))
    homeDir+=_T("\\");
    return homeDir;
    }
    CString CRescueSysDlg::toShortPath(CString m_inDir) //用來(lái)將長(zhǎng)路徑名轉(zhuǎn)化為短路徑名.
    {
    char strBuffer[_MAX_PATH];
    GetShortPathName(m_inDir,strBuffer,_MAX_PATH);
    CString m_toDir(strBuffer);
    return m_toDir;
    }
    5.為"開(kāi)始備份"按鈕或菜單項(xiàng)生成一個(gè)響應(yīng)函數(shù):
    void CRescueSysDlg::OnBeginBkup()
    {
    CInputName m_inNameDlg;
    // CinputName是一個(gè)對(duì)話框類(lèi),用來(lái)將用戶(hù)輸入的備份名稱(chēng)保存在inNameDlg .m_strInputEdit中.
    if(IDCANCEL==m_inNameDlg.DoModal())
    return;
    CFile f1;
    CString m_allFileName[20];
    int m_savedNum=0;
    if(f1.Open(getMyDir()+"backedCab.mqy",CFile::modeRead))
    //讀出用戶(hù)以前所有備份的名稱(chēng).
    {
    CArchive ar1(&f1,CArchive::load);
    ar1>>m_savedNum;
    for(int i=0;i
    ar1>>m_allFileName[i];
    ar1.Close();
    f1.Close();
    }
    CTime m_curTime=CTime::GetCurrentTime();
    CString strInputAdded,strIsFileName;
    strInputAdded.Format("_%d_%d_%d_%d_%d",m_curTime.GetYear(),m_curTime.GetMonth(),
    m_curTime.GetDay(),m_curTime.GetHour(),m_curTime.GetMinute());
    strIsFileName=m_inNameDlg.m_strInputEdit+strInputAdded;
    CFile f2;
    f2.Open(getMyDir()+"backedCab.mqy",CFile::modeCreate|CFile::modeWrite);
    CArchive ar2(&f2,CArchive::store);
    ar2<
    for(int j=0;j
    ar2<
    ar2<
    ar2.Close();
    f2.Close();
    m_savedNum++;
    DeleteFile(toShortPath(m_strWinDir)+"\\sysbckup\\rb000.cab");
    CStdioFile f;//建立包含系統(tǒng)備份命令的批處理文件并執(zhí)行.
    f.Open(getMyDir()+"myTemp.bat", CFile::modeCreate|CFile::modeWrite,NULL);
    CString m_strCommand=toShortPath(m_strWinDir)+"\\command\\scanreg.exe/backup\n";  f.WriteString(m_strCommand);
    f.Close();
    WinExec(getMyDir()+"myTemp.bat",SW_HIDE);
    CString m_toRbName;
    m_toRbName.Format("rb0%d.cab", m_savedNum); //格式化存儲(chǔ)文件名.
    CTime m_beginTime=CTime::GetCurrentTime();
    CTimeSpan m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
    SYSTEM_INFO sysInfo;
    GetSystemInfo(&sysInfo);
    int delayTime=150/sysInfo.wProcessorLevel; //根據(jù)計(jì)算機(jī)的速度算出大致的完成時(shí)間.
    while(!CopyFile(m_strWinDir+"\\sysbckup\\rb000.cab",getMyDir()+m_toRbName,0))
    {
    MSG msg;
    if(::PeekMessage(&msg,m_hWnd,0,0,PM_REMOVE))
    {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
    }
    if(!(m_progress.GetPos()>=100))
    m_progress.SetPos(100*m_timeSpan.GetSeconds()/delayTime);
    m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
    }
    m_progress.SetPos(100);
    AfxMessageBox("已經(jīng)成功的備份了系統(tǒng)文件");
    DeleteFile(getMyDir()+"myTemp.bat");
    }
    6.為"開(kāi)始恢復(fù)"按鈕或菜單項(xiàng)生成一個(gè)響應(yīng)函數(shù):
    void CRescueSysDlg::OnBeginRestore()
    {
    CRestoreDlg m_restoreDlg;
    // CRestoreDlg是一個(gè)對(duì)話框類(lèi),用來(lái)將用戶(hù)輸入的恢復(fù)名稱(chēng)保存在m_restoreDlg.m_strSeled中.
    if(IDCANCEL==m_restoreDlg.DoModal())
    return;
    CStdioFile f; //建立Autoexec.bat和rescueS.bat,將恢復(fù)系統(tǒng)的命令寫(xiě)入.
    f.Open(getMyDir()+"Autoexec.bat", CFile::modeCreate|CFile::modeWrite,NULL);
    CString m_strCommand=(CString)"echo off \n"+"cls \n"+
    toShortPath(getMyDir())+"rescueS.bat \n";
    f.WriteString(m_strCommand);
    f.Close();
    f.Open(getMyDir()+"rescueS.bat", CFile::modeCreate|CFile::modeWrite,NULL);
    m_strCommand=(CString)"echo off \n"+"cls \n"+
    "del "+toShortPath(m_strWinDir)+"\\sysbckup\\*.cab\n"+
    "copy "+toShortPath(getMyDir())+m_restoreDlg.m_strSeled+" "+toShortPath(m_strWinDir) +"\\sysbckup\\rb000.cab \n"+
    "copy "+toShortPath(getMyDir())+"Autoexec.bak c:\\Autoexec.bat /Y \n"+
    toShortPath(m_strWinDir)+"\\command\\scanreg.exe/restore";
    f.WriteString(m_strCommand);
    f.Close();
    CopyFile(getMyDir()+"Autoexec.bak","c:\\Autoexec.bat",0);
    CopyFile("c:\\Autoexec.bat",getMyDir()+"Autoexec.bak",0);
    CopyFile(getMyDir()+"Autoexec.bat","c:\\Autoexec.bat",0);
    if(IDCANCEL==AfxMessageBox("必須重啟才能生效,你想現(xiàn)在重啟嗎?",MB_OKCANCEL))
    return;
    else
    ExitWindowsEx(EWX_REBOOT,NULL);
    }
    7.為"創(chuàng)建援救盤(pán)"按鈕或菜單項(xiàng)生成一個(gè)響應(yīng)函數(shù):
    void CRescueSysDlg::OnCreatea()
    {
    if(IDCANCEL==AfxMessageBox("請(qǐng)插入一張軟盤(pán),然后按確定鍵.\n\n注意:軟盤(pán)上的所有內(nèi)容將被刪除.",MB_OKCANCEL))
    return;
    CStdioFile f; //建立包含建立啟動(dòng)盤(pán)的命令的批處理文件并執(zhí)行.
    f.Open(getMyDir()+"myTemp.bat", CFile::modeCreate|CFile::modeWrite,NULL);
    CString m_strCommand=toShortPath(m_strWinDir)+"\\command\\deltree /Y a:\\ \n"+
    toShortPath(m_strWinDir)+"\\command\\sys c: a:\n"+
    "dir "+getMyDir()+"rescueSys.exe >> "+getMyDir()+"abcdefgh.txt";
    f.WriteString(m_strCommand);
    f.Close();
    WinExec(getMyDir()+"myTemp.bat",SW_HIDE);
    CTime m_beginTime=CTime::GetCurrentTime();
    CTimeSpan m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
    CFileFind finder;
    while(!finder.FindFile(getMyDir()+"abcdefgh.txt"))
    //如果找到了abcdefgh.txt則說(shuō)明批處理已經(jīng)執(zhí)行完畢.
    {
    MSG msg;
    if(::PeekMessage(&msg,m_hWnd,0,0,PM_REMOVE))
    {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
    }
    if(!(m_progress.GetPos()>=100))
    m_progress.SetPos(100*m_timeSpan.GetSeconds()/30);
    m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
    }
    m_progress.SetPos(100);
    f.Open("a:\\autoexec.bat", CFile::modeCreate|CFile::modeWrite,NULL);
    m_strCommand=(CString)"echo off \n"+"cls \n"+
    "copy "+toShortPath(getMyDir())+"*.cab "+toShortPath(m_strWinDir)+"\\sysbckup /Y \n"+
    toShortPath(m_strWinDir)+"\\command\\scanreg.exe/restore \n";
    f.WriteString(m_strCommand);
    f.Close();
    AfxMessageBox("已經(jīng)成功的創(chuàng)建了援救盤(pán).");
    DeleteFile(getMyDir()+"myTemp.bat");
    DeleteFile(getMyDir()+"abcdefgh.txt");
    }