arynhard
arynhard

Reputation: 473

How do I run bcdedit from my c program?

I don't understand why the following code returns "'bcdedit' is not an internal or external command" when ran from a c program. bcdedit works perfectly fine on cmd line. How can I get this to work?

#include <stdio.h>

int main ()
{
    system("bcdedit");
    system("TIMEOUT /T 3");
    return(0);
}

Upvotes: 1

Views: 2282

Answers (3)

Math
Math

Reputation: 3396

It happens because when you run the Command Prompt via Start Menu or even the Execute window you are running the 64-bit cmd version, located at C:\Windows\System32\cmd.exe, however when call cmd from your c program it calls the 32-bit cmd version, located at C:\Windows\SySWOW64\cmd.exe. This happens because your C compiler generates a 32-bit application.

According to MSDN:

The %windir%\System32 directory is reserved for 64-bit applications. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.

In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64.

Source: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx

If you compare both cmds you will realize that they are identical, what differs are the dll's.

The problem is that Windows x64 provides a 64-bit bcdedit.exe in the System32 folder, but doesn't provide a 32-bit bcdedit.exe anywhere. So the 32-bit cmd can't run the 64-bit bcdedit, so it returns that this command is invalid.

Solution: You can both obtain a 32-bit bcdedit from a Windows x86 version or you can compile a 64-bit application.

Upvotes: 3

MYMNeo
MYMNeo

Reputation: 836

I think you have cut one command into two part.And I think you want to run "bcdedit.exe /timeout 3",but you give the argument of the system command two parts, one is "bcedit.exe", another is "/timeout 3". I think you should wrote this

system("bcdedit.exe /timeout 3");

to run the command you wanted.Hope this will help you

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881553

Most likely because it cannot find the executable. Either ensure your path is correct(a) or use the full path name:

system ("c:\\windows\\system32\\bcdedit.exe");

And, of course, this should go without saying: make sure you run it as an administrative user.


(a) You should be able to confirm this with something like:

system ("path");

Upvotes: 1

Related Questions