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

字號:

一.引言
    Windows的system.dat和user.dat中存儲著windows注冊表,win.ini和system.ini中也存儲著一些重要的初始化信息,對它們進行備份可以避免一些重裝系統(tǒng)之類的繁瑣.雖然windos自帶一些備份和恢復(fù)系統(tǒng)信息的功能,但是使用起來很不方便.經(jīng)過一些研究,我發(fā)現(xiàn)你完全可以非常容易地設(shè)計出自己的系統(tǒng)援救程序.
    二.關(guān)鍵問題分析
    1.在程序中調(diào)用系統(tǒng)現(xiàn)有的程序.
    使用WinExec(),ShellExecute(),CreateProcess()調(diào)用其它應(yīng)用程序的這三種方法基本已經(jīng)人所共知.但是有一些命令和程序它們不能調(diào)用,如DOS中的內(nèi)部命令和sys.com等一些外部命令.這時,我們可以首先建立一個.bat的批處理文件,然后再用這三種方法之一采用隱藏的方式來調(diào)用這個批處理文件.這樣我們可以在用戶不知不覺中,來使用一些系統(tǒng)現(xiàn)成的功能.
    2.如何備份系統(tǒng)信息
    可以調(diào)用windows目錄下command\目錄中的scanreg.exe來備份系統(tǒng)信息,用/backup參數(shù)可以使前面介紹的文件被壓縮成一個cab文件存儲于windows目錄下的sysbckup\目錄中,文件名自動為rb000,rb001等.此命令不能直接調(diào)用,必須通過前面介紹的方法來使用它.如果你不想使用scanreg.exe,也可以使用同一個目錄中的extract.exe來生成cab文件,這樣雖然麻煩一些,但是靈活性較強.
    注意,為了程序的通用性, windows目錄不應(yīng)根據(jù)你的計算機上的目錄直接指定,而應(yīng)使用GetWindowsDirectory()來獲得.
    3.如何恢復(fù)系統(tǒng)信息
    注冊表文件的恢復(fù)必須在純DOS方式下,因此你的程序應(yīng)讓系統(tǒng)重啟(使用ExitWindowsEx()),在沒進入windows之前恢復(fù)系統(tǒng)信息.這可以通過修改autoexec.bat來實現(xiàn).如果在備份時你使用的是scanreg /backup,那么在恢復(fù)時你可以在autoexec.bat中加入scanreg /restore.如果你備份時使用的是extract 那么你可以將形式如下的一條語句加入到autoexec.bat中:
    extract /Y /L C:\windows myBackedFile.cab *.*
    另外除特殊情況外,在純DOS方式下一般不支持長路徑名.因此在程序中將語句寫入autoexec.bat時,要先用GetShortPathName()來轉(zhuǎn)化為短路徑名.
    4.援救盤的創(chuàng)建
    恢復(fù)系統(tǒng)可分兩種情況.一種是用戶想把系統(tǒng)信息恢復(fù)成以前某次備份時的狀態(tài),此時可使用戶在程序中選擇要恢復(fù)的備份,然后程序控制重啟并在autoexec.bat中恢復(fù)即可.另一種情況則是用戶由于誤操作或其它原因是系統(tǒng)出錯而不能進入windows,因此要建立援救軟盤,以使用戶能夠恢復(fù)系統(tǒng).援救盤的目的一個是啟動系統(tǒng),可以通過windows目錄下command\目錄中的sys.com來實現(xiàn)(如sys c: a:).另外軟盤重要記住備份存放的位置,以便通過命令來恢復(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.加入一個進展條,并用ClassWazid生成一個控制型變量m_progress.
    4.加入兩個函數(shù):
    CString CRescueSysDlg::getMyDir()//用來得到程序的當(dāng)前目錄.
    {
    TCHAR sFilename[_MAX_PATH];
    TCHAR sDrive[_MAX_DRIVE];
    TCHAR sDir[_MAX_DIR];
    TCHAR sFname[_MAX_FNAME];//不帶擴展名
    TCHAR sExt[_MAX_EXT]; //擴展名,前面有"."
    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) //用來將長路徑名轉(zhuǎn)化為短路徑名.
    {
    char strBuffer[_MAX_PATH];
    GetShortPathName(m_inDir,strBuffer,_MAX_PATH);
    CString m_toDir(strBuffer);
    return m_toDir;
    }
    5.為"開始備份"按鈕或菜單項生成一個響應(yīng)函數(shù):
    void CRescueSysDlg::OnBeginBkup()
    {
    CInputName m_inNameDlg;
    // CinputName是一個對話框類,用來將用戶輸入的備份名稱保存在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))
    //讀出用戶以前所有備份的名稱.
    {
    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); //格式化存儲文件名.
    CTime m_beginTime=CTime::GetCurrentTime();
    CTimeSpan m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
    SYSTEM_INFO sysInfo;
    GetSystemInfo(&sysInfo);
    int delayTime=150/sysInfo.wProcessorLevel; //根據(jù)計算機的速度算出大致的完成時間.
    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.為"開始恢復(fù)"按鈕或菜單項生成一個響應(yīng)函數(shù):
    void CRescueSysDlg::OnBeginRestore()
    {
    CRestoreDlg m_restoreDlg;
    // CRestoreDlg是一個對話框類,用來將用戶輸入的恢復(fù)名稱保存在m_restoreDlg.m_strSeled中.
    if(IDCANCEL==m_restoreDlg.DoModal())
    return;
    CStdioFile f; //建立Autoexec.bat和rescueS.bat,將恢復(fù)系統(tǒng)的命令寫入.
    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)建援救盤"按鈕或菜單項生成一個響應(yīng)函數(shù):
    void CRescueSysDlg::OnCreatea()
    {
    if(IDCANCEL==AfxMessageBox("請插入一張軟盤,然后按確定鍵.\n\n注意:軟盤上的所有內(nèi)容將被刪除.",MB_OKCANCEL))
    return;
    CStdioFile f; //建立包含建立啟動盤的命令的批處理文件并執(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則說明批處理已經(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)建了援救盤.");
    DeleteFile(getMyDir()+"myTemp.bat");
    DeleteFile(getMyDir()+"abcdefgh.txt");
    }