making a C++ program and need to input a bat code

Write your question here
I am writing a program to assist with password resets and need to input the code from a bat file into my code to start the next step here is my current code

Put the code you need help with here.
// lab1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
char response;
string mystring;
mystring = "Do you want to reset your SERVERADMIN Password? (Y/N)";

cout << mystring << "\n";
cin >> response;
if (response == 'y')
cout << ("please follow the next step") << "\n";
else if (response == 'n')
exit(-1);
system("pause");



return 0;
}

the bat file config is here and this is what needs to be input if the response is 'y'

@echo off
SET /p InstanceName="Enter SQL Instance: " %=%

@echo off
SET /p SA Password="Password will be changed to Timeclock1...press enter to agree: " %=%

@echo on
sqlcmd -S "%COMPUTERNAME%\%InstanceName%" -Q "[master].[sys].[xp_instance_regwrite] @rootkey = N'HKEY_LOCAL_MACHINE' ,@key = N'Software\Microsoft\MSSQLServer\MSSQLServer' ,@value_name = N'LoginMode' ,@type = N'REG_DWORD' ,@value = 2;"

sqlcmd -S "%COMPUTERNAME%\%InstanceName%" -Q "alter login sa with password='Timeclock1'"

sqlcmd -S "%COMPUTERNAME%\%InstanceName%" -Q "alter login sa enable"

pause

does anybody know how I can accomplish this? I am still pretty new to C++ and am running some test programs to teach myself
you can call a batch file directly from the code with system("name.bat") if it is in your path. But your batch code looks to have parameters. It may be easier to cook up 1 command at a time as a string, filling in the parameters directly, and calling system on that. System can be replaced with any of many better commands (spawn, shellexecute, etc) if you prefer.

calling system on a batch file is like a double security no-no if the machine is at risk. A hacker can make a batch file do literally anything (including running a compromise program) and your program will invoke it for them with your credentials (assuming you are at an admin level...). If you do this, be aware of this loophole. For that reason I recommend cooking up the commands in the code directly. System also has some flaws that can be exploited, so you can look at the alternatives to pick a safer one if you like.

For my personal machine or minor use, I have created any number of tiny programs in c++ that ARE glorified batch files. When doing OS level stuff, this can be very powerful with minimal coding effort.


Last edited on
ive been working in C++ here for like 2 days lol just learning the ropes and trying to write some basic programs that backdoor installs to reset certain things and this would save about 45 minutes total for each time this needs to happen
can you elaborate more on these commands that I could use other than system?
also is there a way I could execute the batch file from C++ and keep the commands hidden and at the end just have a label that says operation complete?
// lab1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
char response;
string mystring;
mystring = "Do you want to reset your SERVERADMIN Password? (Y/N)";

cout << mystring << "\n";
cin >> response;
if (response == 'y')
system("T:\Reset_SA_Password.bat");
else if (response == 'n')
exit(-1);
system("pause");



return 0;
}

this is my new code and it calls the batch file but when it is executing in the command prompt I can see the batch commands. what I am asking is there a way to have the commands running in a hidden format so it cant be seen?
should be easy enough to turn it off in the bat file.

look up echo off and redirecting output to null
missed the follow ups.

system can be replaced with, depending on what you are doing and which OS you are on, spawn family, shellexecute family, fork/ exec family, and a few others. I don't know them all.


you can hide the console in windows for sure, I forget which command it is, but google will know. It calls the batch but hides the console, which is nice in a gui program that does not need the user to see the under the hood OS commands (sloppy interface issue). Not sure what unix needs there. It might be tricky to do this in a console program, you probably need the batch file to execute in its own hidden window, which is doable but possibly not simple. Not sure, I have not had to do this so I am just saying its possible, and this is roughly how it would look, but the exacts I do not know. I did it in a gui program but it was long ago and I don't have that code now.

Another way to do it is code the batch file with echo-off so you can't see anything.

here is one example of doing it in windows:
https://stackoverflow.com/questions/1597289/hide-console-in-c-system-function-win
Last edited on
Topic archived. No new replies allowed.