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

1.8k views Asked by At

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);
    }
4

There are 4 answers

3
MK. On BEST ANSWER

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.

3
acraig5075 On

Are you perhaps compiling with all warnings switched off?

Because the line system("copy unattend.xml C:\Windows\System32\Sysprep"); need the backslashes to be escaped.

EDIT

This will be an issue with privileges where you're being denied access to that folder. It should work if you run the .exe as Administrator.

1
FatalError On

I can't say this is your issue for sure, but... it's suspicious to me that you have system() calls that just cd to a new directory. Since each one runs in a new shell, it will not affect subsequent calls to system(). Thus, if you tried to change to a directory where a file exists to operate on it, you will find that you aren't actually in that directory.

3
dbenham On

You have escaped all the back slashes in every line except the one that is failing. You just need to escape them there as well.

system("copy unattend.xml C:\\Windows\\System32\\Sysprep"); 

I have to agree that what you are doing is crazy for a C program. But it should work.

EDIT

It also looks like you have an unwanted space before sysprep.bat in your last System() call.

Also, I agree with FatalError that your cd system call doesn't do any good. Drop that line, and then prefix "unattend.xml" with the full path to the file in the following line.

If bcedit is not found within any of your default PATH folders, then you probably need to provide the absolute path to the file.