Share via


Creating an Issuance License from Scratch

The following steps identify how to create an issuance license by using only the functions contained in the AD RMS SDK. For an example, see the OnlinePublishing or OfflinePublishing samples included with the SDK.

The following example shows how to create an issuance license.

int CreateIssuanceLicense(void)
{

  // Retrieve the system time to define the validity period
  // for the unsigned issuance license.
 
  GetSystemTime( &stTimeFrom );
  GetSystemTime( &stTimeUntil );
  stTimeUntil.wYear++;

  // Create an issuance license, user, and right. Add the 
  // right and user pair to the issuance license.
 
  hr = DRMCreateIssuanceLicense( 
          &stTimeFrom,       // starting validity time
          &stTimeUntil,      // ending validity time
          NULL,              // display name for referral URL
          NULL,              // referral URL
          NULL,              // optional owner handle
          NULL,              // issuance license template string
          NULL,              // bound license handle
          &hIssuanceLicense  // pointer to issuance license
          );
  
  if( FAILED( hr ) )
       goto e_Exit;

  // Create a user.

  hr = DRMCreateUser( 
          wszUserId,         // user name
          NULL,              // user ID
          L"Unspecified",    // type of user ID
          &hUser             // user object handle 
          );     

  if( FAILED( hr ) )
       goto e_Exit;

  // Create a right for the user.

  hr = DRMCreateRight( 
          L"EDIT",            // name of the right
          &stTimeFrom,        // beginning validity time
          &stTimeUntil,       // ending validity time
          0,                  // count of extended elements
          NULL,               // extended information name
          NULL,               // extended information value
          &hRight             // rights object handle
          );

  if( FAILED( hr ) )
       goto e_Exit;

  // Add the right and user to the license.

  hr = DRMAddRightWithUser( 
          hIssuanceLicense,   // issuance license handle
          hRight,             // handle of right to add
          hUser );            // handle of user to add

  if( FAILED( hr ) )
       goto e_Exit;

  // Add metadata to the license.
  // Create a GUID to use as a unique content ID.

  hr = CoCreateGuid( &guid );

  if( FAILED( hr ) )
       goto e_Exit;

  wszGUID = new WCHAR[ GUID_STRING_LENGTH ];
  if( NULL == wszGUID )
       goto e_Exit;

  uiGUIDLength = StringFromGUID2( 
          guid, 
          wszGUID, 
          GUID_STRING_LENGTH );

  if( 0 == uiGUIDLength )
  {
     hr = E_FAIL;
     goto e_Exit;
  }

  // Set the content ID in the issuance license.

  hr = DRMSetMetaData( 
          hIssuanceLicense,   // issuance license handle
          wszGUID,            // content ID
          L"MS-GUID",         // type of content ID
          NULL,               // optional SKU ID
          NULL,               // SKU ID type
          NULL,               // content type
          NULL                // content name
          );
  if( FAILED( hr ) )
       goto e_Exit;

e_Exit:

  return hr;

}

To specify an owner for the issuance license, call the DRMCreateUser function before calling the DRMCreateIssuanceLicense function as shown in the following example.

Note  The owner specified by hOwner in is the Owner principal, not the OWNER right. Merely specifying someone as the hOwner does not grant them the OWNER right; granting yourself (or anyone else) the OWNER right must be done explicitly by using DRMAddRightWithUser. Zero, one, or more users can be granted the OWNER right, which never expires. For more information, see the hOwner parameter of DRMCreateIssuanceLicense.

// Create the owner.

hr = DRMCreateUser( 
          L"someone@example.com", // user name
          NULL,                   // user ID
          L"WINDOWS",             // type of user ID
          &hOwner                 // owner handle
          );

if( FAILED( hr ) )
     goto e_Exit;

// Create the empty issuance license.

hr = DRMCreateIssuanceLicense( 
          &stTimeFrom,       // starting validity time
          &stTimeUntil,      // ending validity time
          NULL,              // display name for referral URL
          NULL,              // referral URL
          hOwner,            // optional owner handle
          NULL,              // issuance license template string
          NULL,              // bound license handle
          &hIssuanceLicense  // pointer to issuance license
          );

if( FAILED( hr ) )
     goto e_Exit;

See Also

Creating an Issuance License
Creating an Issuance License From a Template
Licenses and Certificates
Reusing Issuance License Data

Send comments about this topic to Microsoft

Build date: 3/13/2008