system("bcdedit /set safeboot") does not set windows to safeboot

684 views Asked by At

Here is the c++ code with command, command for run windows in safeboot with network mode and restart, This code run only in 32 bit operating system but in 64 bit only restart will happen but not in safeboot mode.

[NOTE] : I run application in administrative mode.

system("bcdedit /set safeboot network  > Out.txt");
system("shutdown -r -f -t 0");

and also with ShellExecute()

ShellExecute(0, L"open", L"cmd.exe", L"/C bcdedit /set safeboot network > Out.txt", 0, SW_HIDE);
ShellExecute(0, L"open", L"cmd.exe", L"/C shutdown -r -f -t 0 > Out.txt", 0, SW_HIDE);

I tried manually like Open CMD in administrative mode

c:\WINDOWS\system32>bcdedit /set safeboot network
The Operation completed successfully.
c:\WINDOWS\system32>shutdown -r -f -t 0

Manually it was working (run in safeboot) but when I tried through code Windows restarted, but not in safeboot.

How to make this work both 32 bit and 64 bit operating system ?

3

There are 3 answers

0
SoronelHaetir On

Do you run your program with the system() call as an administrator? I would not expect that call to succeed if you do not.

0
user7860670 On

> Out.txt are probably not valid arguments Note that system is not a command shell interpreter to redirect standard io. If you want to get output then it would be better idea to redirect them manually using CreateProcess or some wrapper such as boost.Process.

0
Krish On

Am executing the program on a 64 bit machine. The bcdedit.exe file exists in C:\Windows\System32 Although C:\Windows\System32 is in system path, in an x86 process are subject to the File System Redirector. Which means that C:\Windows\System32 actually resolves to C:\Windows\SysWOW64

There is no 32 bit version of bcdedit.exe in C:\Windows\SysWOW64

The solution is to change target AnyCPU or x64.