How to change the name of .exe on runtime - c++

Persay, if I wanna change the .exe on runtime of my program (Like its original name would be: someexe.exe and after you have closed it will change to something random or similiar)
I've seen it happen before and I'm very much interested in implementing it just for fun!
Thanks :)

I'm not 100% sure, but the regular rename function should work. However, I'm not sure if it's possible to rename a file that is currently held open by the OS (and I have no Windows system that I can test on right now).
If you can't do it on "your own executable", then you'd have to rename the file by running another program [or batchfile] and then resuming your own program [e.g. let the rename program start your program again, and it recognising somehow that it's now the new name and can continue.

Possible code
#include <iostream>
#include <windows.h>
int main(int argc, char** argv)
{
system("title a.exe");
std::cout<<"a.exe is the current name of your .exe file\n";
//do your stuff
std::cout<<"a.exe is changing to b.exe\n";
system("RENAME C:\\path\\a.exe b.exe");
system("title b.exe");
//do your stuff
std::cout<<"now b.exe is changing to a.exe\n";
system("title a.exe");
system("RENAME C:\\path\\b.exe a.exe");
//do you stuff
return 0;
}
But using systemcommand is considered to be bad practise therefore you should use rename command ,, code :
#include <stdio.h>
int main(int argc , char** argv)
{
rename("a.exe","bii.exe");
//do your stuff
rename("bii.exe","a.exe");
//do your stuff
return 0;
}

You have to make a copy of the executable file. Then you can rename that file and when you close your current program file you need to transfer control to another program preferably a batch file which deleted the original executable.
in windows system() should be able to do these stuff.
you also need to write your own batch file with code to delete thr program that calls it.

Related

So...is there a way to stop files from clearing automatically? (c++)

So i'm making an extremely simple guessing console game and i want to store data permanently in a file (highscore). However everytime i compile the file i'm using empties itself. Is there anyway to stop that?
I've tried a lot of thing which didn't work and i honestly don't know where the problem is. I'm guessing it has to do with the fin and fout but for others it seemed to work
#include <iostream>
#include <fstream>
#include <time.h>
#include <conio.h>
int hs;
//this would be the play_game() function, unrelated to the subject
int main()
{
std::ofstream fout;
fout.open("HS.txt");
std::ifstream fin;
fin.open("HS.txt");
srand(time(NULL));
//menu with 4 options, play, quit, help and highscore (which i'm working on)
fin.close();
fout.close();
}
Don't open your file twice in parallel with two streams. Also, a simple open-for-writing will not truncate your file, but will place you at the start of the file, so you'll be overwriting existing data; see this question. You have to open files with the write mode.
You need to either:
Open your file for both input and output - and without truncating it; see: What does it mean to open an output file as both input and output? , or
Open your file for reading only when your app starts, and open it for writing, and write to it, when it exists (or every once-in-a-while for better resiliency).

How to embed font and other image files when using graphic libraries like SFML

Many of you have probably wondered- how can I get rid of a font or any other single file from the drive when using a library like SFML, which needs to load a font from a file path.
So, how do I embed that data into the executable, so the resulting executable does not depend on those resource files anymore?
First of all, we have to get our resource. I have downloaded "BalooBhaijaan-Regular.ttf" font from google fonts.
Then, one should get the binary data of the given font. The easiest way to achieve this in my opinion is to use the linux "xxd" command with -i parameter which outputs in a C-style array.
Let's redirect the output to a file because It is usually going to be long if we are talking about true type fonts or larger images:
xxd -i BalooBhaijaan-Regular.ttf > font_data.txt
Create an empty C/C++ header or put the font data into an already existing file. I prefer using new header files as the output is going to be really long.
Of course, after pasting into your IDE you can change the array type to const as the content of a font usually doesn't change.
This is how it looks in my IDE:
You might of course wonder why is this a char array - simply because in a char array each "field" represents one byte.
As you might have noticed, xxd also creates another variable for us - the last variable in the font_data.txt is an unsigned int which informs us about the length of the array. We will need this later. Name of the "length-informing" integer is same as name of the array with "_len" suffix
Now, there are two ways to proceed:
1. load font from the memory using a builtin method (some libraries support it, SFML does)
2. create a "fake" file and load it
Lets talk about both cases
1.
This one is fairly simple, sfml supports loading file from memory given it's address and size, so we can just do this:
#include "BalooBhaijaanFont.hpp"
#include <SFML/Graphics.hpp>
int main(int argc, char** argv) {
sf::RenderWindow mainWindow(sf::VideoMode(200,100), L"TEST");
sf::Font fromMem;
fromMem.loadFromMemory(&BalooBhaijaan_Regular_ttf, BalooBhaijaan_Regular_ttf_len);
sf::Text text("WORKS!", fromMem);
while(mainWindow.isOpen()){
mainWindow.draw(text);
mainWindow.display();
}
return 0;
}
As you can see, loading it with a builtin function is really easy.
2.
Now it's time for a temporary file approach which i really do NOT recommend - most libraries support loading from memory and if you are making your own library you are going to end with having a memory load function anyway.
Whilst it is still possible to create a file just to read it to a font class and then remove it, I do not see any sense of using this method unless you are extremely annoyed by additional files in your folders.
Just for reference:
#include "BalooBhaijaanFont.hpp"
#include <SFML/Graphics.hpp>
int main(int argc, char** argv) {
sf::RenderWindow mainWindow(sf::VideoMode(200,100), L"TEST");
sf::Font fromFile;
{
FILE * tempFile = fopen("tmpfont.ttf", "wb");
fwrite( BalooBhaijaan_Regular_ttf, sizeof(char), BalooBhaijaan_Regular_ttf_len, tempFile );
fclose(tempFile);
fromFile.loadFromFile("tmpfont.ttf");
std::remove("tmpfont.ttf");
}
sf::Text text("WORKS!", fromFile);
while(mainWindow.isOpen()){
mainWindow.draw(text);
mainWindow.display();
}
return 0;
}
For Windows you can do xxd -i Roboto-Bold.ttf > font_data.txt in git bash. This gives you a file with data that can be imported straight into your project.

Finding where a process was run from

So i have an issue that i have an application that gets started. Then through a test i need to turn it off and start it again. But it needs t obe done without hard coding.
So is there a way of finding where a process was run from? I can find a list of all processes running but dont know if this is even possible.
EDIT: Its on a windows 7 OS.
QueryFullProcessImageName() will provide the path to the executable image for a process:
#include <windows.h>
#include <iostream>
int main()
{
char exe_path[MAX_PATH];
DWORD exe_path_size = MAX_PATH;
if (QueryFullProcessImageName(GetCurrentProcess(),
0,
exe_path,
&exe_path_size))
{
std::cout << exe_path << "\n";
}
return 0;
}
Easy and portable way would be using argv[0].
It returns the full .exe file path which is all you need
First, what do you mean by "find where the process is run from"? I'm assuming you mean the parent's process id, but it could mean current working directory, ip of remote call, etc...
To find the parent's process id, look into getppid().
Edit: this assumes that you (like any sane programmer) are using a unix-like machine.
Edit #2: You're on Windows, so I have no idea.
Edit #3: Since you're looking for the path to the program you are executing, use argv[0]. The first command line arg to int main(int argc, char* argv[]) is always the path to the binary.

Manually getting the output instead of output redirection in cmd line

I have a C++ program which has the prototype of the main function as follows:
int main(int argc, char * argv[])
The code hasn't been written by me, but this is a single C file available here.
When I compile this code, and through the command line run it as:
someexe in.txt > out.txt
This gives me an output out.txt which is generated in the same directory by operating on some input from in.txt.
someexe in.txt out.txt
This gives me an output on the command line itself. (without using > operator)
However, instead of passing the command line argument and without using the output redirection > operator, I have been trying to call the main function from another function and passing the parameters myself. If I pass an array of char* {fileDirName, in.txt}, I am not sure how to go about generating an out.txt (since I think > output redirection is an operating system level function available in command line).
Any suggestions would be greatly appreciated
The program in the link is readily available as copy paste and can be tried (main function is written at the last in the above program)
Assuming the aim is to mimic the output redirection feature (> out.txt) of the shell you can do something like:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <iostream>
int main() {
int fd = open("out.txt", O_WRONLY|O_CREAT|O_TRUNC, 0660);
assert(fd >= 0);
const int ret = dup2(fd, 1);
assert(ret >= 0);
std::cout << "Hello redirected world!" << std::endl;
close(fd);
}
You can do similar for stdin also, to mimic the input redirection (< in.txt). These will be preserved across calls to exec() too.
Of course it would be simpler to modify the program to write to the place you wanted given you have the source available.
Note though that dup2(), which "swap" the stdout fd for the one we just opened is non-portable. IIRC open() (as opposed to fopen()) is UNIX specific also)
You can't call another main() from inside the source another program - main() is special.
If you want to reuse this source code as a library you need to rename main() to something else.
However if it is handling input from either a pipe or a file (eg myprog < input.txt or myprog input.txt) in the normal Unix way then that's a little trickier to handle transparently.
The best way would be to call the compiled program as a separate process from within your new program, passing the correct commandline parameters - see the exec() family of calls

C++ - Accessing a file that was "dropped" on the executable

I have used a number of programs where I am able to use the programs functions by simply dragging and dropping a file onto the executable. For example, if there is a program that formats text files, simply dragging a text file onto the executable will make it run and use the text file as the target.
What does the main function look like for a program that allows this?
Dropped files are usually just given as command line parameters to the program:
int main(int argc, char** argv)
{
if (argc > 1)
{
// do sth. with argv[1] == first dropped file name
}
}