Convert .bat to .exe without depending on cmd.exe - c++

I have a bat to execute all files in a folder every 15 seconds. I need to make it .exe to run it as a hidden application.
I have converted it to .exe with bat to exe converter and other programs, and I see that in all of them, when I execute the .exe, for example, Load.exe, automatically start a cmd.exe at the same time. If I kill Load.exe it still continues to run, until I kill cmd.exe.
So, can I make an .exe from a .bat without depending on a cmd.exe?

Short but correct answer: no, you can not.

I don't know if it depends on CMD or not (probably it doesn't), but you can use C++ function system to execute each line of your .bat file.
Do it something like :
#include <stdlib.h>
int main()
{
system("<batch command goes here>");
return 0;
}
Simultaneously, you can add each line of .bat file to system function and execute it as a CPP/C program.

Related

How to have a C++ program run a cmd command [duplicate]

I'm trying to use the system() function in a C program.
For example, I tried to create a directory on my desktop, using the system() function.My code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("cd c:\\Users\\USER\\Desktop");
system("mkdir test");
return 0;
}
When I run this code, a directory is created, but not on my desktop. It is created in my project directory.
Why is this happens?
Can I use the cd command in the system() function? If not, is there an replacement to the cd command that will work with system()?
I'm using Windows OS. I'm trying to use system() from a C program as I use cmd program.
I know that I can create the directory using WinAPI without any problem. I don't want to use WinAPI, my question is how can I make it work using system().
The changed directory only lasts for the duration of the system command. The command starts a separate program, which inherits its current directory from your program, but when that program exits its current directory dies with it.
You can use && to join the commands together, and it will work:
system("cd /D C:\\Users\\USER\\Desktop && mkdir test");
I also added the /D switch, or the CD command would not change drive letter if it were called from a different drive.
However, mkdir is perfectly capable of accepting a full path, so you could simply do:
system("mkdir C:\\Users\\USER\\Desktop\\test");
When you say system("some shell command");, the program spawns a shell to run the command. The shell has its own idea of the current directory, separate from your program's. The shell cds to the directory just as you asked it to, and then dies, leaving your process's CWD unaffected.
You could simply say _chdir("c:\\Users\\User\\Desktop"); to set the current directory before running the "mkdir" command. The shell that spawns to run it will then inherit your program's current directory and make the folder in the right place.
(For that matter, you could say _mkdir("test") as well, and stop using system unnecessarily. You should only reach for system when you're trying to do something that's worth running an external program / shell for.)
You have to perform both the commands on a single line like this,
system("cd c:\\Users\\USER\\Desktop && mkdir test");

Open CMD in the directory where my program is

I have a piece of software written in c++, that has to call command line and execute 2 simple commands. The problem is, they need to be executed in the main directory of my program (folder where exe file is). How can I make sure, that they will execute in this directory, if it can be different on PCs (for example "Program Files" or "Program Files(x86)").
On windows, you can use the GetModuleFileName WinAPI, which will return you the path of the exe that you can cd to and execute your commands.

(VS2010 C++) Execute a command every time the program is run?

The IDE I'm using is VS2010 for writing C++
I want to execute the command cmd C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt every time the program I'm coding is run from within the IDE. (This command should open a console to track changes made to the file Log.txt)
There are ways to make a command run every time the program is built, but I can't find a way to make a command run whenever the program itself is run, even if it's already built. Where or how might I be able to set that kind of thing up?
I've tried putting $(TargetPath) & C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt into the project's Properties->Debugging->Command (TargetPath is the full name of the debug executable) but VS reads the entire thing as a filename and gets confused.
You can create a file run.cmd for example next to the vcxproj file, which would contain:
%1
C:\utilities\unix\tail.exe -f -n15 %2Log.txt
And then in Properties->Debugging->Command you write:
$(ProjectDir)\run.cmd
and in Command Arguments you write:
"$(TargetPath)" "$(ProjectDir)"
I may have misspelled the macros, but you get the idea: it executes first your program and then whatever you want.
Edit: Unfortunately it works only if you start without debugging (Ctrl+F5), because otherwise the debugger tries to attach to run.cmd and complains that the format is unsupported.

Compiled exe won't run through my custom schemed cmd?

I have a custom color/font/size/etc. scheme for my cmd.
When I run my program through VS (with or without debugging) it runs my program in cmd using my scheme. (good)
But, when I run the exe file directly, it runs in the default cmd (without my scheme). (bad)
If I make a .bat file which executes my exe, then it runs in the schemed cmd. (good)
But if I make a shortcut of that .bat file, it runs in the default cmd! (bad)
How can I make it so that the exe itself runs through the schemed cmd?
If not possible, how can I make it so the shortcut to the exe/bat runs it through the schemed cmd?
If you want the software to run in schemed cmd, you should execute the program over command line, other way you can edit your program's font etc. in your program.
Compilers run programs by cmd.
And if you want it so much, you can check your program on task manager at beginning your code and if there isn't, you can run with system function and hide first one.
I don't know how you changed your cmd scheme, so, it is possible that this will not help you. Anyway...
The standard way of cmd customization is to include in HKCU\Console\ a subkey with the title of the console that will be customized, and inside this key, the values that define the console properties (more here)
So, let's say you have now a console scheme in the registry. When you start a console program via a shortcut, the window title of the started console is the text indicated as the shortcut name. Match shortcut name to console name defined in registry (or the reverse).

Compiling a .cpp file from the body of another .cpp file

I've been working on an application that compiles raw .cpp files and analyzes their outputs, using Microsoft Visual Studio 2010 Command Prompt. I'm having a lot of trouble, and there doesn't seem to be much material about this online. Here's the troublesome code:
#include <iostream>
using namespace std;
...
string name = "cl /EHsc ";
name += "example.cpp";
system("setupcppenv.bat"); // A short batch file I wrote to launch the VC++ cmd prompt without launching another instance of cmd
system(name.c_str());
When I execute (it attempts to compile example.cpp), I get an error:
fatal error C1043: iostream: no include path set
I'm not very experienced with batch files, or using the command prompt compiler. What am I doing wrong?!
Additionally, is there a different way to compile from inside an application?
Thanks!
Each system() call invokes a separate process, so any environment variables you set in your setupcppenv.bat file will be discarded once that process ends.
What you should do instead, is to add the environment variables you are setting in your .bat file to the system environment, or at least to the environment of the cmd instance from where you launch your application, so that they are inherited by the process started by the system() call.
I don't know what's in setupcppenv.bat I would guess that you're making changes to environment variables in that batch file. What happens is that when the batch script ends, those environment variable changes are being lost becuase they're confined to the batch script's process and any children of that process.
A way to set environment variables that will work is to use the setenv() or putenv() functions in your program.