I have a batch file "install.bat" stored in location "
c:\Users\abc\xyz
".
I want to execute this batch file in administrative mode from
C:\Windows\System32
folder using a System() API. Can anyone kindly lemme know how do I achieve this VC++ programatically.
My code snippet::
int ret = System("c:\Users\abc\xyz\install.bat");
If I give this command, batch file is of course executing from "c:\Users\abc\xyz" folder. But I want to run this batch file from System32 folder?
Thanks in advance.
One option is to put a cd command as the first line of your .bat file. You could change the working directory of the calling process, but that's using a hammer to crack a nut.
If you move away from the system function you can call CreateProcess. That allows you to specify all the gory details you need when creating a new process. You need to run the command interpreter (find that by reading the COMSPEC environment variable). You can specify the working directory for the new process as one of the parameters to CreateProcess.
CreateProcess is rather hard to call though. And it won't help you with requesting elevation to admin rights. Instead you can use ShellExecute. Call that passing "runas" for the verb, which will result in elevation.
ShellExecute(0, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL,
"C:\\Windows\\System32", SW_SHOW);
You need to use ShellExecute.
Something like the following
ShellExecute(hwnd, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL, "c:\\windows\\system", SW_SHOWNORMAL );
Without using System(), you can just cd to the location containing the .bat file and just invoke the batch file
Related
I have a batch program that I would like to add to one of my c++ programs. The batch program tests if a file exists and exits the script if it doesn't. I do not want to add any more libraries to my code. The problem that I have is that I am not sure how I am able to use batch in c++. I am able to figure everything else out on my own I just need to know this one thing.
You can use the system(" ") command to use batch.
Assuming you are on Windows,
you have two options available to run batch files on Windows from C/C++.
You can use system (or _wsystem for wide characters).
"The system function passes command to the command interpreter,
which executes the string as an operating-system command. system
refers to the COMSPEC and PATH environment variables that locate the
command-interpreter file (the file named CMD.EXE in Windows 2000 and
later)."
Or
You can use CreateProcess directly.
Note that for batch files:
"To run a batch file, you must start the command interpreter; set
lpApplicationName to cmd.exe and set lpCommandLine to the following
arguments: /c plus the name of the batch file."
I have create a project in which I am trying to generate a .txt file and then calling OS specific commands (I'm using unix so using system() function to trigger application) to open it in default application.
My code works perfect.
But the problem starts when I try to run my application after setting it in environment path. I am doing that for global visibility. File gets generated correctly by while opening the .txt using system() seems like it is trying to look at it at different directory.
I want to some how convey to the system() function to look at the specific location.
How can I solve this problem?
It sounds like you are actually just in a different working directory. Try showing the result of getcwd.
system( "file path" ) helped me to open a excel application, but then, I lost control from the MFC exe as the cmd.exe holds the control ( with the excel openend). Can anyone please help me in closing the cmd exe alone, and getting the control back to my MFC application. I would also want the openend excel to stay, displayed
Instead of opening the cmd.exe and launching the excel application along with the file path, you can use the ShellExecute API to does it easier.
Here is the example:
ShellExecute(NULL, L"open", L"excel", L"your_file_path", NULL, SW_SHOWNORMAL);
As noted in a commendt, you can use system("start excel \"file path\") that doesn't let the command interpreter to wait for excel to return.
But you can better not use system, and refer to a more congruent process control API, like CreateProcess to call excel directly from your app.
This will prevent some malware to place an excel.cmd file in you path to do what he wants with your application.
This is simple to do in languages like Python, but I'm not sure how to do it in C++. I want to move to a specific folder, say "C:\tests" and run some command line call from that folder. Thanks
You can start a process in a specific directory using the CreateProcess() call. In particular, look at the lpCurrentDirectory argument:
lpCurrentDirectory [in, optional]
The full path to the current directory for the process. The string can also specify
a UNC path. If this parameter is NULL, the new process will have the same current drive
and directory as the calling process. (This feature is provided primarily for shells that
need to start an application and specify its initial drive and working directory.)
This function is used internally by Python's subprocess.Popen's constructor.
You want SetCurrentDirectory for changing directories and system for executing a command asynchronously. system is the simple way to do it. You can use CreateProcess if you need flexibility.
Create a bat file and put cd command there to set required path and then write your command to execute. Ex in your bat file write
line1 "cd c:/tests/"
line2 "your command to be executed"
After that you could use system("*.bat") to call your bat file. See more info related to that here http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044654269&id=1043284392 Hope this helps
I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.