So i am trying to create a self replicating .exe in c++ using code::blocks. I have written the code and on my local machine it works flawlessly. After loading it on a Windows 7 Virtual machine and 2 other PCs, i realized it does not work. It comes up with the error
“Can not copy file, access denied”
My PC probably has different permissions set i am assuming which is why it works on mine and not others. Here is the section of code:
ifstream check( "C:\\newfile.exe" );
if( !check.is_open() )
{
char bufferf[MAX_PATH];
GetModuleFileName( NULL, bufferf, MAX_PATH );
std::stringstream str;
str << "copy " << bufferf << " C:\\newfile.exe";
system( str.str().c_str() );
system( "start c:\\newfile.exe" );
cout << __argv[0] << endl;
return 1;
}
else
{
return 0;
}
Remember i am very new to c++, long time java, php and c# developer but i figured it is time to learn a lower level language. If you have any suggestions on making this a more efficient function then please let me know.show any code you can since im not 100% familiar with all the libraries yet. long story short i want to copy the current exe to the cdrive, run that copied file and close the original. here is another way i tried
ifstream check( "C:\\newFile.exe" );
if( !check.is_open() )
{
char bufferf[MAX_PATH];
GetModuleFileName( NULL, bufferf, MAX_PATH );
char * original_file = bufferf;
char * new_file = ( char * )"c:\\newFile.exe";
( void ) CopyFile( ( LPCTSTR )original_file, ( LPCTSTR )new_file, FALSE );
system( "start c:\\newFile.exe" );
cout << __argv[0];
return 1;
}
else
{
return 0;
}
Still no success. but both work flawlessly on my development machine (from what i can see) i heard people say you can not copy running exe files in windows but that is not true, i feel it is a permissions issue but i may be wrong.
Any tips on self replication are welcome, i’m open to new methods.
please know that, Windows Vista and Windows 7 have UAC enabled which doesn’t allow you to “write” to certain folders. You might have to run the program in Administrator mode or disable UAC altogether for your program to copy/create file from one location to the protected special folder.
To request the user to allow a program to be run in administrative mode, you must define and link a manifest file to your application: This tells you how to do it.