ExclamationHelpTrying to create replicant code.

Posts 1–3 of 3 · Page 1 of 1
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.
Update:
I've changed the program a little.
I've tried the second option, thus to copy an already existing copy of the program (as i believe it can't copy itself as it's running...) first to the same directory (as a test) and then to the next subdirectory (thus for example: if the file is inside a\b\c\d\ the copy will be done inside a\b\c\).
Then it is easily able to run the copy which then does the same up to a dead end (i guess? as the program won't at some point find an existing directory and simply fail to launch the copy).

However there's still one issue: the program has to do those copies into the folders which are contained in the program's folder.

This means that if in a\b\c\d there are two folders called \e and \f, the program has to copy the file to those as well as \c.
Right now, i'm planning to make a matrix of char vectors (as i've been forced to use char vectors instead of strings) then planning to copy the file to no more than 20-ish folders as then it'd become a little too heavy i'm afraid.

Issue: How do i detect how many folders are there in the same folder of the executable and their respective name?
Also, is it possible to ideally have a list of all folders, then choosing them one by one? Thus not using a matrix?

for example:
folder is called tommy, the executable is here.
inside tommy, there are abc,def,ghi and zzz.
The program has int X=0.
program checks X's value, it's zero, thus picks the first folder by alphabetical order, which is abc.
calculates file path
copies
launches the copy
increases X by one
checks X's value, it's one, thus picks the second folder etc etc.

That'd be a fine way to do it as it'd support unlimited folders using less variables.
Actually... i solved by my own.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?