# Step 1.3: Retrieving serial number status flags

A handy function to print flags

First of all, we need a handy function to transform numeric values of flags to comprehensible statuses of a serial number. Here is the code of this function:

    #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");
    }

Despite the size, function is really simple - checks all bit flags one by one and print all that are present in the status variable. Replace printf in the code after checking the serial number to the call to print_state, and make changes to the serial number we pass to the licensing system:

    char *serial = "Xserialnumber1"; // we set the serial number directly in the code, for simplicity
    int res = VMProtectSetSerialNumber(serial);
    print_state(res);

Now, if we run this program, the following message will be printed to the console:

    state = SERIAL_STATE_FLAG_INVALID
    please register!

Now, we put the old key back by removing "1" and run the program again:

    state = 0
    We are registered.

Now, as we can see status flags of a serial number, let's move to retrieving flags and data from a serial number.

Retrieving serial number status

You can get the status of a serial number in three ways: by calling VMProtectSetSerialNumber(), by calling VMProtectGetSerialNumberState() or by calling VMProtectGetSerialNumberData() - status flags are put into one of fields of the structure. Each method is intended to use in specific time. The first check of the serial number is performed during installation. At this moment you should decline incorrect numbers, expired numbers, numbers in the black list and so on. Some limitations, for example, the maximum operation time of the program or serial number expiration date should also be checked in run-time. And the VMProtectGetSerialNumberState() method is the fastest and the most convenient way here. And if you need to receive complete information about the serial number, you can use the more powerful VMProtectGetSerialNumberData() function.

Next step

© 2006-2015 Copyright VMProtect Software