home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / lsapi / simple.c < prev   
C/C++ Source or Header  |  1997-10-08  |  5KB  |  161 lines

  1. /*
  2.  *  This is a part of the Microsoft Source Code Samples.
  3.  *  Copyright 1996-1997 Microsoft Corporation.
  4.  *  All rights reserved.
  5.  *
  6.  *  The following example uses the minimum amount of code required to
  7.  *  utilize any licensing system.  This example is provided on the SDK
  8.  *  and can be used to familiarize oneself with a licensing system or
  9.  *  as a template for an application.  Note that the code exits if a
  10.  *  grant cannot be obtained.  Recall that this is strictly an
  11.  *  application's decision and is not dictated by any licensing system
  12.  *  which adheres to the LSAPI standard.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "windows.h"
  18.  
  19. /*
  20.  * Include the LSAPI header file.
  21.  */
  22. #include "lsapi.h"
  23.  
  24. /*
  25.  * Define the product name, product version, and publisher name for
  26.  * use with the licensing calls.
  27.  */
  28. #define MYAPP_PRODUCT_NAME    "sample_product"
  29. #define MYAPP_PRODUCT_VERSION "1.0"
  30. #define MYAPP_PUBLISHER_NAME  "sample_publishers"
  31.  
  32. /*
  33.  * Define the strings used to log a comment with the license system.
  34.  */
  35. #define MYAPP_REQUEST_LOG_COMMENT "Comment for the LSRequest call"
  36. #define MYAPP_RELEASE_LOG_COMMENT "Comment for the LSRelease call"
  37.  
  38. /*
  39.  * Forward declarations
  40.  */
  41. void PrintErrors( LS_HANDLE handle, LS_STATUS_CODE errorCode );
  42.  
  43.  
  44. __cdecl main()
  45. {
  46.    /*
  47.     * LSAPI Variables
  48.     */
  49.    LS_ULONG        unitsGranted;
  50.    LS_HANDLE       licenseHandle;
  51.    LS_STATUS_CODE  status;
  52.  
  53.    /************ the following code is for the LSAPI beta only! ************/
  54.    char            szProviderPath[ MAX_PATH ];
  55.    UINT            nChars;
  56.  
  57.    /* install if necessary */
  58.    nChars = GetSystemDirectory( szProviderPath, sizeof( szProviderPath ) );
  59.    if ( 0 == nChars )
  60.    {
  61.       printf( "Can't get system directory, error %d.\n", GetLastError() );
  62.    }
  63.  
  64.    lstrcat( szProviderPath, "\\mslsp32.dll" );
  65.    status = LSInstall( szProviderPath );
  66.    if ( LS_SUCCESS != status )
  67.    {
  68.       printf( "Cannot install LSAPI, error 0x%08lx.\n", status );
  69.    }
  70.  
  71.    /* add licenses for our product */
  72.    status = LSLicenseUnitsSet( LS_ANY,
  73.                                MYAPP_PUBLISHER_NAME,
  74.                                MYAPP_PRODUCT_NAME,
  75.                                MYAPP_PRODUCT_VERSION,
  76.                                LS_LICENSE_TYPE_NODE,
  77.                                LS_NULL,
  78.                                1,
  79.                                0,
  80.                                NULL );
  81.    if ( LS_SUCCESS != status )
  82.    {
  83.       printf( "Cannot install licenses, error 0x%lx.\n", status );
  84.    }
  85.    /************ the above code is for the LSAPI beta only! ************/
  86.  
  87.    /*
  88.     * Make the call to request a grant
  89.     */
  90.    status = LSRequest(
  91.          LS_ANY,                               /* Use any licensing system */
  92.          (LS_STR FAR *)MYAPP_PUBLISHER_NAME,   /* Publisher name           */
  93.          (LS_STR FAR *)MYAPP_PRODUCT_NAME,     /* Product name             */
  94.          (LS_STR FAR *)MYAPP_PRODUCT_VERSION,  /* Version number           */
  95.          LS_DEFAULT_UNITS,                     /* Let license figure units */
  96.          (LS_STR FAR *)MYAPP_REQUEST_LOG_COMMENT, /* Log comment         */
  97.          0,                                    /* No Challenge             */
  98.          &unitsGranted,                        /* # units granted          */
  99.          &licenseHandle );                     /* license context          */
  100.  
  101.    /*
  102.     * Check whether we got a successful grant.  If not, then call a routine
  103.     * which prints out the error message, free the license handle, and do
  104.     * not continue running the application.
  105.     */
  106.    if ( LS_SUCCESS != status )
  107.       {
  108.       PrintErrors( licenseHandle, LS_USE_LAST );
  109.       LSFreeHandle( licenseHandle );
  110.       return(1);
  111.       }
  112.  
  113.    /*
  114.     * Continue with the application.
  115.     */
  116.    printf("Hello World\n");
  117.  
  118.  
  119.    /*
  120.     * We are now done with the application, so we must make a call
  121.     * to release the grant.
  122.     */
  123.    status = LSRelease(
  124.          licenseHandle,                        /* License context          */
  125.          LS_DEFAULT_UNITS,                     /* Let license figure units */
  126.          (LS_STR FAR *)MYAPP_RELEASE_LOG_COMMENT);/* Log comment         */
  127.    if ( LS_SUCCESS != status )
  128.       {
  129.       PrintErrors( licenseHandle, status );
  130.       LSFreeHandle( licenseHandle );
  131.       return( 1 );
  132.       }
  133.  
  134.    /*
  135.     * Free the license handle.
  136.     */
  137.    LSFreeHandle( licenseHandle );
  138.  
  139.  
  140.    exit( 0 );
  141. }
  142.  
  143.  
  144. void PrintErrors( LS_HANDLE handle, LS_STATUS_CODE errorCode )
  145. {
  146.    LS_STATUS_CODE   status;
  147.    char             errorText[200];
  148.  
  149.  
  150.    errorText[0] = 0;
  151.  
  152.    status = LSGetMessage( handle, errorCode, (LS_STR FAR *)errorText, 200);
  153.    if ( LS_TEXT_UNAVAILABLE == status )
  154.       printf("Error: No message catalog available.\n");
  155.    else
  156.       if ( LS_UNKNOWN_STATUS == status )
  157.          printf("Error: Unknown error code was used.\n");
  158.       else
  159.          printf("Error: %s\n", errorText);
  160. }
  161.