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.

Old Project – 3D Level Editor (2010)

Today I stumbled upon some old screenshots in my backup folder and I thought maybe I should post it here to remind myself how passionate I was.

This is a 3D level editor I did for my hobby game project back in 2010. The level editor was made using Qt 4 and Irrlicht engine. Some of the screenshots below are showing the editor which uses Irrlicht’s native GUI. Irrlicht’s GUI system was quite limited in term of types of widgets and functionality, which later on led me to switching all the GUI over to Qt.

Some of the features supported by the “game engine” I did back then include:
– Basic fixed function 3D rendering
– Basic collision (box collider and trigger)
– Spawn points
– Player camera and animated camera (for in-game cinematic)
– 3D sound (using irrKlang)
– Simple path finding

I made a simple game demo using the game engine and level editor I made. Unfortunately, the game wasn’t finished and I moved on doing something else. Below is the video recording I did back in 2010 showing how the game demo look like, for a competition. Spore Motions was the name of our team back then.

Hopefully I will be back into game development very soon.

Minimal Qt Project

Today I was trying to experiment on how small can a Qt project get if I only use the essential modules and scrap away all the rest.

First I created a new Qt project by going to:
File -> New File or Project… -> Application -> Qt Widgets Application

When I build the new project I’m getting this empty window, which is expected:

Then I did the following:

1. Delete mainwindow.ui
2. Delete mainWindow.h and mainWindow.cpp
3. In main.cpp, remove QApplication and replace it with QGuiApplication instead.
4. Use QWindow instead of QMainWindow

The reason why I removed QApplication and QMainWindow is because they both rely on QtWidget module. This way we can reduce the size further by removing one more dependency. Now all I left are just 2 file: Myproject.pro and main.cpp, and they now look like this:

MyProject.pro

TARGET = MyProject
TEMPLATE = app
SOURCES += main.cpp

main.cpp

#include <QGuiApplication>
#include <QWindow>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QWindow window;
    window.setTitle("Hello World!");
    window.resize(640, 480);
    window.show();

    return app.exec();
}

Then I build the project again, which the result should look totally the same as before. However, we no longer need QtWidget.dll anymore which reduces the number of DLL files to just 5:

– libgcc_s_dw2-1.dll
– libstdc++-6.dll
– libwinpthread-1.dll
– Qt5Core.dll
– Qt5Gui.dll

The total size of entire executable folder, including all the DLL files mentioned above, is now only 11.3MB. Its still fairly large if compare to other application framework, but considering two things:

1. There are tons of functionalities in QtCore and QtGui alone, including all the helper classes and even OpenGL
2. All the DLL files in the entire Qt SDK is at the staggering size of 1.87GB in total

11.3MB is actually pretty damn impressive!

Exposing QML Object Pointer to C/C++

Sometimes, for whatever reason, we want to modify the properties of a QML object through C/C++ scripting. Qt’s QML engine actually supports this and it allows you to register your QML objects as C/C++ types, however, this is often not well-explained.

For example, I created a label in QML and I want to change its text occasionally. In order for me to expose the label object to C/C++, I created a C/C++ class called MyClass which extended from QObject class.

myclass.h:

class MyClass : public QObject
{
    Q_OBJECT
public:
    // Object pointer
    QObject* myObject;
    
    explicit MyClass(QObject *parent = 0);
    
    // Must call Q_INVOKABLE so that this function can be used in QML
    Q_INVOKABLE void SetMyObject(QObject* obj);
}

In myclass.cpp source file, I defined a function called SetMyObject() to save the object pointer. This function will later be called in QML.

myclass.cpp:

void MyClass::SetMyObject(QObject* obj)
{
     // Set the object pointer
     myObject = obj;
}

After that, in main.cpp, include MyClass header and register it to QML engine using the function qmlRegisterType():

main.cpp:

#include "myclass.h"

int main(int argc, char *argv[])
{
     // Register your class to QML
     qmlRegisterType<MyClass>("MyClassLib", 1, 0, "MyClass");
}

Notice there are 4 parameters you need to declare in qmlRegisterType(). Besides declaring your class name (i.e. MyClass), you also need to declare your library name (i.e. MyClassLib) and its version (i.e. version 1.0), which will be used for importing your class to QML later on.

Now that QML engine is fully aware of my custom class, I can then map it to my label object in QML. First off, in my QML file, I imported my class library by calling import MyClassLib 1.0. Notice that the library name and its version number have to be matching with the one you declared in main.cpp, otherwise it will throw you an error.

After declaring MyClass in QML (and set its id as myclass), I then call myclass.SetMyObject(myLabel) to expose the label’s pointer to C/C++ right after the label is being initialized:

import MyClassLib 1.0

ApplicationWindow
{
     id: mainWindow
     width: 480
     height: 640
     
     MyClass
     {
          id: myclass
     }

     Label
     {
          id: myLabel
          text: qsTr("Hello World!")
          Component.onCompleted:
          {
               myclass.SetMyObject(myLabel);
          }
     }
}

Please be aware that you need to wait until the label is fully initiated before exposing its pointer to C/C++, otherwise you may cause the program to crash. To make sure it’s fully initiated, call SetMyObject() within Component.onCompleted and not other places.

Now that the QML label is being exposed to C/C++, I can do whatever I want to the label. For instance, I can set its visibility to true and change its text to “Bye bye world!”:

// QVariant automatically detects your data type
myObject->setProperty("visible", QVariant(true));
myObject->setProperty("text", QVariant("Bye bye world!"));

Besides changing the properties, I can also call its functions by calling QMetaObject::invokeMethod():

QVariant returnedValue;
QVariant message = "Hello world!";

QMetaObject::invokeMethod(myObject, "myQMLFunction",
    Q_RETURN_ARG(QVariant, returnedValue),
    Q_ARG(QVariant, message));

qDebug() << "QML function returned:" << returnedValue.toString();

…or simply:

QMetaObject::invokeMethod(myObject, "myQMLFunction");

That’s all for this tutorial. If you have any problem please feel free to leave a comment below. Happy coding!

Application Development with Qt Creator Second Edition

Application Development with Qt Creator Second Edition” is the 3rd book I reviewed for Packt Publishing. Although it’s the second edition, the contents of the book are greatly varied from its predecessor.

First off, it has more chapters compare to the first edition, bringing more contents to the readers especially topics related to Qt Quick. The book also included some pretty fun projects for readers to work on such as media player and simple paint program.

The book also covers some of the fundamental and important topics such as localization, software optimization, and debugging which are needed to develop industry-standard applications.

Let’s take a look at the chapter outline:

Chapter 1
Getting Started with Qt Creator, shows you how to download and install Qt Creator as well as edit simple applications to test your installation.

Chapter 2
Building Applications with Qt Creator, shows you how to compile, run, and debug your application using Qt Creator. You will learn how Qt Creator integrates with both the GNU debugger and the Microsoft console debugger to provide breakpoints, memory inspection, and other debugging help.

Chapter 3
Designing Your Application with Qt Designer, shows you how to use the drag-and-drop GUI designer that is a part of Qt Creator to build both Qt Widget-based applications and Qt Quick applications.

Chapter 4
Qt Foundations, takes you through the foundations of software development using Qt and also covers its support for platform-agnostic application development.

Chapter 5
Developing Applications with Qt Widgets, shows you how to build applications using Qt Widgets that look and act like native desktop applications on the platform of your choice.

Chapter 6
Drawing with Qt, shows the various ways you can move beyond the built-in controls in Qt and make your own drawing on the screen and other drawable entities such as image files in PNG or JPEG.

Chapter 7
Doing More with Qt Quick, expands on what you learned about Qt Quick in the introductory chapters.

Chapter 8
Multimedia and Qt Quick, introduces you to Qt Quick’s support for multimedia, such as audio and video playback as well as how to use a camera if it is connected.

Chapter 9
Sensors and Qt Quick, shows you how to use the various sensors on many of the devices available today using Qt Quick.

Chapter 10
Localizing Your Application with Qt Linguist, shows you how to manage resource strings for different locales, letting you build your application with different languages in different locales.

Chapter 11
Optimizing Performance with Qt Creator, shows you how to use Qt Creator to examine your Qt Quick application’s runtime performance, as well as how to perform the memory profiling of your application with Valgrind, an open source diagnostic tool.

Chapter 12
Developing Mobile Applications with Qt Creator, gives you a glimpse of the exciting arena of mobile software development and shows you how you can use what you’ve learned in this book about Qt and Qt Creator to write applications for platforms such as Google Android.

Chapter 13
Qt Tips and Tricks, is packed with tricks for using Qt and Qt Creator that will help you use the Qt framework and the Qt Creator IDE efficiently.

You can get the book from here.