Get Demo
  • Windows
  • MacOS
  • Linux

Step 2.1: Creating a new protected application

At the first stage, we created several simple apps to test the API of the licensing system. Now, in the second stage, we will create just one application. It will also be a console app with the foo() function working only in the registered version. Here is the code of our test application:

#include <windows.h>
#include <stdio.h>
#include "VMProtectSDK.h"
#define PRINT_HELPER(state, flag) if (state & flag) printf("%s ", #flag)
void print_state(INT state)
{
    if (state == 0)
    {
        printf("state = 0\n");
        return;
    }
    printf("state = ");
    PRINT_HELPER(state, SERIAL_STATE_FLAG_CORRUPTED);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_INVALID);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_BLACKLISTED);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_DATE_EXPIRED);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_RUNNING_TIME_OVER);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_BAD_HWID);
    PRINT_HELPER(state, SERIAL_STATE_FLAG_MAX_BUILD_EXPIRED);
    printf("\n");
}
char *read_serial(const char *fname)
{
    FILE *f;
    if (0 != fopen_s(&f, fname, "rb")) return NULL;
    fseek(f, 0, SEEK_END);
    int s = ftell(f);
    fseek(f, 0, SEEK_SET);
    char *buf = new char[s + 1];
    fread(buf, s, 1, f);
    buf[s] = 0;
    fclose(f);
    return buf;
}
// The foo() method is very short, but we need it to be a separate function,
// so we ask the compiler not to inline it
__declspec(noinline) void foo()
{
    printf("I'm foo!\n");
}
int main(int argc, char **argv)
{
    char *serial = read_serial("serial.txt");
    int res = VMProtectSetSerialNumber(serial);
    delete [] serial;
    if (res)
    {
        printf("Serial number is invalid\n");
        print_state(res);
        return 0;
    }
    printf("Serial number is correct, calling foo()\n");
    foo();
    printf("Done\n");
    return 0;
}

Compile the program without debug information, but in the linker settings enable creation of a MAP file — we will need it to work with VMProtect. After running the program, we should see the following output:

Serial number is invalid
state = SERIAL_STATE_FLAG_INVALID

Currently, the licensing system still works in test mode because the file has not been processed by VMProtect and does not contain a licensing module.

Last updated 11 days ago