The License Code Generation Example
................... #include <stdio.h> /* For scanf(), sprintf() etc.*/ #include "lscgen.h" /* For the code generator API.*/ /* The fixed feature name of licenses generated by this example program. */ #define VLS_CGENXMPL_FEATURE_NAME "CGENXMPL" /*Mnemonic used for setting code structure for long codes.*/ #define VLS_LONG_CODE_TYPE_STR "1" /* Utility function to print code generator API errors to stderr. It also calls the code generator library cleanup function on the handle if necessary. */ static int VLSPrintErrors (VLScg_HANDLE *iHandle, int retCode) { if (*iHandle != VLScg_INVALID_HANDLE) { (void) VLScgPrintError(*iHandle, stderr); (void) VLScgCleanup(iHandle); } return retCode; } /* VLSPrintErrors() */ /* A simple example to illustrate the use of the code generation API to generate license strings. This is a command line utility that generates license codes for a fixed feature name, "CGENXMPL". It prompts the user for the expiration date and then calls the code generator API functions to generate an appropriate license for CGENXMPL. To build this example, compile and then link with the appropriate code generator API library - lscgen32.lib/lscgen64.lib */ int main () { /* Code generator library handle. */ VLScg_HANDLE iHandle; /* Code generator standalone license code structure. */ codeT licCode; /* Expiration date information: acquired from user. */ int expMonthInt, expDayInt, expYearInt; /* String versions of above for calling code generator API functions.*/ char expMonth[10], expDay[10], expYear[10]; /* For license string to be returned by code generator API.*/ char *licStr = (char *) NULL; /* For return codes from code generator API functions. */ int retCode; /* Initialize the code generator library. */ if((retCode=VLScgInitialize(&iHandle))!= VLScg_SUCCESS){ (void) VLSPrintErrors(&iHandle, retCode); fprintf(stderr, "\nERROR: Code generator library initialization failed.\n"); return retCode; } /* if (!VLScgInitialize()) */ /* Initialize the license code structure. */ if((retCode=VLScgReset(iHandle,&licCode))!= VLScg_SUCCESS) return VLSPrintErrors(&iHandle, retCode); /* Specify that we want to generate a long code. */ if ((retCode = VLScgSetCodeLength(iHandle, &licCode, VLS_LONG_CODE_TYPE_STR)) != VLScg_SUCCESS) return VLSPrintErrors(&iHandle, retCode); /* Set the feature name. */ if (VLScgAllowFeatureName(iHandle, &licCode) == 0) return VLSPrintErrors(&iHandle, VLScg_FAIL); if ((retCode = VLScgSetFeatureName(iHandle, &licCode, VLS_CGENXMPL_FEATURE_NAME)) != VLScg_SUCCESS) return VLSPrintErrors(&iHandle, retCode); /* * Prompt for and acquire the expiration date from the user.*/ printf("License Expiration Month [1-12] : "); scanf("%d", &expMonthInt); printf("License Expiration Day [1-31] : "); scanf("%d", &expDayInt); printf("License Expiration Year : "); scanf("%d", &expYearInt); /* Convert expiration date information to strings. */ sprintf(expMonth, "%d", expMonthInt); sprintf(expDay, "%d", expDayInt); sprintf(expYear, "%d", expYearInt); /* Set the expiration date. */ if (VLScgAllowLicExpiration(iHandle, &licCode) == 0) return VLSPrintErrors(&iHandle, VLScg_FAIL); if (((retCode = VLScgSetLicExpirationMonth(iHandle, &licCode,expMonth)) != VLScg_SUCCESS) || ((retCode = VLScgSetLicExpirationDay(iHandle, &licCode,expDay)) != VLScg_SUCCESS) || ((retCode = VLScgSetLicExpirationYear(iHandle, &licCode,expYear)) != VLScg_SUCCESS)) return VLSPrintErrors(&iHandle, retCode); /* Generate the license: memory for license string is allocated by library. */ if ((retCode = VLScgGenerateLicense(iHandle, &licCode,&licStr))!= VLScg_SUCCESS) return VLSPrintErrors(&iHandle, retCode); /* Print out the license string. */ (void) fprintf(stdout, "%s\n", licStr); /* Free the license string, which was allocated by VLScgGenerateLicense() */ VLScgFreeLicenseString(licStr); /* Terminate use of code generation library cleanly. */ (void) VLScgCleanup(&iHandle); return 0; } /* main() */