Search code examples
cwindowscmdsystem32bcdedit

Why do windows commands that access C:\Windows\System32\... not work in a C program?


I am running a c program designed to automate a process that "repackages" a windows install. I am doing this for two reasons. First reason is to learn c programming, and the second reason is that I run windows off a MacBook pro that has no superdrive. I found a tutorial that explains how to install windows on vbox and then copy it over to another hard drive. I decided that I want to practice c programming and automate this tutorial so I wrote the code below. When I run the program below, I get errors that bcdedit is not an internal or external command and that unnattend.xml copies but when I check if it is there it is no where to be found. After experimenting a bit I found that the code I use in the System() functions runs perfectly fine if I run it straight from elevated command prompt. Although it gives me the errors I mentioned earlier when I run my program from elevated command prompt. It seems that an exe has no access to the System32 folder?? Please help! I'm beating my head against the wall here

    #include <stdio.h>

    void part1 (void);
    void part2 (void);

    void part1 (void)
    {
        FILE *fp;
        //Run Switcheroo
        if ((fp=fopen("log.txt", "r")) == NULL)
        {
            //Run part 1.
            system("DISKPART /s resources\\diskpart\\DskPrtAssgn.txt");
            system("TIMEOUT /T 3");
            system("reg unload HKLM\\BCD00000000");
            system("TIMEOUT /T 3");
            system("robocopy s:\\ c:\\ bootmgr");
            system("TIMEOUT /T 3");
            system("robocopy s:\\Boot c:\\Boot /s");
            system("TIMEOUT /T 3");
            system("bcdedit /store c:\\boot\\bcd /set {bootmgr} device partition=C:");
            system("TIMEOUT /T 3");
            system("DISKPART /s resources\\diskpart\\DskPrtActv.txt");
            system("TIMEOUT /T 3");
            system("schtasks /create /tn 'Switcheroo' /tr %userprofile%\\Desktop\\Switcheroo\\Switcheroo.exe /sc onlogon");

            //Set up the log file that the computer will check upon reboot.
            char buffer[2] = {'0'};
            fp = fopen("log.txt", "wb");
            fwrite (buffer , 1 , sizeof(buffer) , fp );

            //Reboot.
            system("shutdown -r");
        }
        else if (fp = fopen("log.txt", "rt"))
        {
            part2();
        }
    }

    void part2 (void)
    {
        FILE *fp;

        //Read the log file from part1. 
        if (fp = fopen("log.txt", "rt"))
        {
            //Run part 2.
            system("DISKPART /s resources\\diskpart\\DskPrtRmv.txt");
            system("TIMEOUT /T 3");
            system("cd resources\\sysprep");
            system("copy unattend.xml C:\Windows\System32\Sysprep");
            system("TIMEOUT /T 3");     
            system("runas /user:%username% %userprofile%\\Desktop\\Switcheroo\\resources\\sysprep\\ sysprep.bat");
        }

        //If part one did not finish then print error.
        else if ((fp=fopen("log.txt", "r")) == NULL)
        {
            printf("Error.");
        }
    }

    int main ()
    {
        part1();
        return(0);
    }

Solution

  • You should not be writing system administration scripts in C. Do it in batch, or in python, or in PowerShell. C is absolutely not the right choice for the program you are writing.