Prevent Direct Execution of EXE

Back in early 2000s was the time when MMOs were very very very poppular in Asian countries. That was also the time when I was crazy about Ragnarok Online, one of the most famous korean MMORPGs of all time. Besides playing the game (which cost around US$10 per month, a large sum for a middle school student like me), I also went into modding the game and ran it on an emulator (private server running locally).

One thing that I noticed was the existence of a mysterious EXE file in its installation directory called “Ragexe.exe” which can never be executed; it will show you an error message whenever you double click on it.

Years later when I started to learn C/C++ programming I finally understood what the hell was that EXE file for; in-fact, it’s the actual game executable and not the other EXE that has the game icon on it, the other EXE is actually the updater client, nothing else.

So finally, the mystery has been solved. However, the question is: How can we re-create this for our own programs/games? What if we also want our users to run the updater first and not directly execute the main program? It turns out, it’s pretty easy to achieve. In the following example I will be using C/C++ and Qt but it should be pretty similar in other programming languages and platforms as well.

The sample code here is pretty simple:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	if (argc <= 1)
	{
		QMessageBox::critical(0, "Application Error", "Please run MyGame.exe instead.");
		return 0;
	}
	else
	{
		if (argv[1] != "yourpassword")
		{
			QMessageBox::critical(0, "Application Error", "Please run MyGame.exe instead.");
			return 0;
		}
	}

	MainWindow w;
	w.show();

	return a.exec();
}

In the example above, your EXE will simply pop out an error message that says “Please run MyGame.exe instead.” if you double click on it directly. This is because the argc variable is either 0 or 1 (depending on platform, which means no additional input argument during launch) if you run it directly.

However, if there is one or more arguments being dumped to the program during launch, check (within the argv array) whether the second argument (the first argument is usually the program’s name) matches your secret phrase or password before allowing the program to launch. Usually there will be input arguments if the user drag one or more files to the EXE to make it launch, as this is very useful for programs like text editors or image editors. That’s why we must check if the argument matches your password or not.

Now that your program can no longer be run directly, what about the updater? How to ask the updater to execute your main program with an input argument? It’s actually pretty easy as well. In the following example I will be using C/C++ and Qt, but it should be similar across different programming languages and platforms:

QProcess *process = new QProcess(this);
process->setNativeArguments("yourpassword");
process->startDetached("MyGame.exe");
delete process;

exit(EXIT_SUCCESS);

In the code above, I simply declared a QProcess object and set the native argument before starting the main program and close the updater client.

That’s all, it’s very simple to achieve. The tutorial above is by no mean professional: Technical names and phrases are not necessarily accurate, and the method used is not necessarily the standard way. I’m just trying to share what I know and what I did with my own projects. Peace.

3 thoughts on “Prevent Direct Execution of EXE

  1. Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog? My blog site is in the exact same niche as yours and my users would really benefit from a lot of the information you provide here. Please let me know if this alright with you. Thanks!

  2. Fantastic site you have here but I was wondering if you knew of any user discussion forums that cover the same topics discussed in this article? I’d really like to be a part of group where I can get responses from other experienced people that share the same interest. If you have any suggestions, please let me know. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *