0

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);
    }
arynhard
  • 473
  • 2
  • 8
  • 26
  • which version of windows are you using? – fduff May 31 '12 at 18:34
  • 2
    This reads an awful lot like a batch file... just saying ;). – FatalError May 31 '12 at 18:35
  • @fduff: Windows 8. @ FatalError: I know, but this is just to learn how a c program works. I am brand new to it. If you know of any other way I could make it more "C" like please feel free to show me. I was actually hoping I could find a way to make it less like a batch file. – arynhard May 31 '12 at 18:37
  • If you want it to look less like a batch file, you might think about including windows.h. I don't know the specifics as I have never used it, but it's a start. – Colin D May 31 '12 at 18:40
  • @ColinD: Yes I have heard of it but I can't find any good documentation on it. Do you know of any? Thanks. – arynhard May 31 '12 at 18:41
  • @AndrewRynhard: Just keep in mind that each `system()` call happens in a brand new shell. So, a call to `system()` that just changes directories won't be particularly helpful (as it won't affect the following calls). – FatalError May 31 '12 at 18:48
  • 1
    BTW, not everyone has Windows directory on a C: drive, so consider replacing C:\Windows\System32 with %windir%\system32 – milleniumbug May 31 '12 at 18:50

4 Answers4

1

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.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • I don't see how changing the compile options will help. The error does not occur until run-time. – dbenham May 31 '12 at 18:50
  • 1
    @dbenham the \ is the escape character inside C strings. so you got \w and \S. You don't want that. You want just a backslash. So you have to make it C:\\Windows\\System32\\Sysprep – nos May 31 '12 at 18:54
  • I realize that. I'm just saying that the compiler could never detect the problem. The code should compile fine, without any errors. It is a run-time error. – dbenham May 31 '12 at 19:00
1

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.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • 1
    @MK. I modified the c program to run a batch file with the bcdedit command. Same problems occur. I understand that c might not be the best choice, but for the sake of learning I want to find an answer to my problem. – arynhard May 31 '12 at 19:06
  • Oh, by all means try to figure it out. But I thought that it was worth mentioning that doing this in C is not the Right Thing (tm) to do. – MK. May 31 '12 at 19:12
  • Thank you. I will give powershell a shot. Appreciate the help. – arynhard May 31 '12 at 19:29
1

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
0

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390