Trying to create replicant code.
Greetings, recently at school we've studied how to use the command shellExecute() to launch a simple program, like the calculator, from inside our program.
However, i'm always the overly ambitious guy and i've set the same path of the exe itself instead of the calculator, thus the program kept launching itself endlessy.
Nothing really exciting just yet, however, i've managed to be able to discover the full path the executable is and that is, in my opinion, an interesting feature.
Now, what i'm trying to do is being able to create a file automatically, however this isn't a "hello world" text file, but a compiled .exe instead.
Let's say i have a project on visual studio about a program typing "hello" in the chat once, then waiting for input and closing once it's provided and i do have that project compiled too.
What i planned to do is to create a new file using std::ofstream x("Name.exe"); then opening the compiled exe with notepad, copying the insane mess that lies inside and pasting it using x<<"-insane mess here-"; .
However i was also thinking about including the compiled file in the same folder then copying it with the CopyFile() function.
The issue with the first method, is that i'm really unsure if it'd work and i wouldn't be able to edit the output executable quickly if needed.
However using the second method would require me to have multiple files in the same folder and (most important!) the outputted executables wouldn't be able to output other files easily .
The final idea could either be to make "evil" programs copying theirselves to all the directories within' their own directory, then copying theirselves in the precedent directory as well and running all the copies, then closing itself. Up to the point where every directory contains a copy of the program.
Or maybe something useful like choosing to have a program being auto run on startup, thus having it copy itself to the auto run folder.
Long story short:
I have a program that finds its own full path and runs itself.
I'd need that program to be able to replicate itself or any file (mostly .exe) possibly without using any other file other than theirselves.
Thus the deal would be:
Program_Alpha.exe
-Creates Program_beta.exe in the same folder
OR in any specified folder knowing the full path
-opens Program_beta.exe knowing the full path
-closes itself
Here's the actual code of the program:
-important-
I've had to change the visual studio's project settings from using unicode chars to multibyte chars. Otherwise while i could've fixed "open" using LPCWSTR x=L"open"; i couldn't have done that with basePath as it's a variable.
Code:
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int _tmain(int argc, char* argv[])
{
char basePath[255]="";
_fullpath(basePath,argv[0],sizeof(basePath));
ShellExecute(NULL, "open",basePath, NULL, NULL, SW_SHOWNORMAL);
getchar();
return 0;
}
Hope anybody can help me out! :)
*edit*
I tried to perform the first option, but since there are quite some ' " ' characters, it doesn't work well i'm afraid. Unless there's another way around of course.