I am using the following code to start a batch file with a C++ console application:
WinExec("removeOneDrive.bat", SW_SHOWNORMAL);
However, it starts and opens up but the code that is run in the batch file does not do anything, it is supposed to remove some files and registry keys etc... But it does not do any of these, no errors are shown as well.
Furthermore, when I start the batch file myself without C++ starting it, the batch file works as expected.
All the batch file is supposed to do is kill some running tasks, delete a couple of files and then remove some registry keys. Any ideas? I have tried running as admin but still no luck.
I have also tried this way but still does not work:
It might be a problem with access rights too. Registry manipulation and file deletion are often blocked by UAC. You can solve this later as soon as you make sure that your bat file is executed.
The batch file does execute as the console window comes up and runs. Also I added the code as you did above and output the returncode to the console and got 0000002A.
I do run the program as admin and the console window which the batch file runs under is system32/cmd.exe so I thought admin rights are not the problem there?
Just a question, do you ned to use ShellExecute/WinExec fo this? In your case system("removeOneDrive.bat"); would do the same and be easier to read, write and debug.
I have always been told never to use system anything, because it is buggy and has a huge security hole. So I thought it would be better to learn a different way and do it the correct way.
I tried to use system but it does the same thing, the batch file runs but it does not do what it is supposed to do. It is really weird as I run the program as admin and even turn off UAC, but still not change and if I were to not let C++ start the batch file everything works as expected.
Yeah it was just when I first started learning C++ I used this forum to help me in some areas and they always said never use system and always try to use another method, as it was deemed as a better way.
Tried it and does not work. Thanks for trying thought. I really cannot see why this happens. It must be something to do with permissions when starting the batch file with C++.
Remove redirection of output, remove turning off echo lines. Ge the full info on what happens. All those operations are probably trying to tell why they are not able to work, but you silenced them.
I hate to say this but I tried what you said and it still does not work. It does not even give any error, just performs like it should when echo off is not there. But does not do any of the actions it is supposed to do.
Does anyone know the solution to this problem? Been experiencing multiple errors. Thank you!
Create a class named DentalAppointment. Include fields for a patient’s data (use the Person class),
the date (using the Date class), the time (use the Time class), and the duration of the appointment
in minutes. Also include a field that contains the ending time of the appointment; this field will be
calculated based on the start time and the duration using the Time class function that adds minutes
to a Time object. The DentalAppointment constructor requires a first and last name, and a month,
day, year, hour, and minute for the appointment. Allow a DentalAppointment to be constructed
with or without an additional argument for appointment duration, and force the duration to 30
minutes when no argument is supplied. The constructor does not allow any appointment over 240
minutes. The constructor calculates the appointment ending time based on the start time and the
duration. Also include a display function for the DentalAppointment class. Write a main()function
that loops at least three times, prompting the user for DentalAppointment data and displaying all
the information. (Note, if you use the Person class display function, the zip code will be “X”; this is acceptable.)
Note: We already made a file in notepad, ClassData.txt
So, am I going to use instream infile ofstream outfile? Thank you again
@SoftMOUNT: did you remove output redirection too? If you remove /Q from rd will it ask for confirmation? Will it work if you rename bat file to foo.bat?
What is exact output of that bat file execution? (with all echo commands and pause at the end)
When I remove the /Q it does not work but it makes the process longer with no confirmation.
Here is the output with all echo commands and pause at the end... Ignore the /Q, I did try that before but I put them back in, the output is still the same as below without or with the /Q:
Ohh so sorry, I did not know I needed to remove them as well.
Ok so what happened is, it says it cannot find the registry key. However when you run the batch file on its own and not with C++ it works and find and deletes the reg key after asking me. But it does not do this when I run it with C++.
Below is the output when I run the batch file with C++:
C:\Users\Base\Desktop>set x86="C:\Windows\System32\OneDriveSetup.exe"
C:\Users\Base\Desktop>set x64="C:\Windows\SysWOW64\OneDriveSetup.exe"
C:\Users\Base\Desktop>echo Closing OneDrive process.
Closing OneDrive process.
C:\Users\Base\Desktop>echo.
C:\Users\Base\Desktop>taskkill /f /im OneDrive.exe
ERROR: The process "OneDrive.exe" not found.
C:\Users\Base\Desktop>ping 127.0.0.1 -n 5
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
C:\Users\Base\Desktop>echo Uninstalling OneDrive.
Uninstalling OneDrive.
C:\Users\Base\Desktop>echo.
C:\Users\Base\Desktop>if exist "C:\Windows\SysWOW64\OneDriveSetup.exe" ("C:\Windows\SysWOW64\OneDriveSetup.exe" /uninstall ) else ("C:\Windows\System32\OneDriveSetup.exe" /uninstall )
C:\Users\Base\Desktop>ping 127.0.0.1 -n 5
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
C:\Users\Base\Desktop>echo Removing OneDrive leftovers.
Removing OneDrive leftovers.
C:\Users\Base\Desktop>rd "C:\Users\Base\OneDrive"
The system cannot find the file specified.
C:\Users\Base\Desktop>rd "C:\OneDriveTemp"
The system cannot find the file specified.
C:\Users\Base\Desktop>rd "C:\Users\Base\AppData\Local\Microsoft\OneDrive"
The directory is not empty.
C:\Users\Base\Desktop>rd "C:\ProgramData\Microsoft OneDrive"
The directory is not empty.
C:\Users\Base\Desktop>echo Removeing OneDrive from the Explorer Side Panel.
Removeing OneDrive from the Explorer Side Panel.
C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
Permanently delete the registry key HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} (Yes/No)? Yes
ERROR: The system was unable to find the specified registry key or value.
C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
Permanently delete the registry key HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} (Yes/No)? Yes
ERROR: The system was unable to find the specified registry key or value.
C:\Users\Base\Desktop>pause
Press any key to continue . . .