Share via


Obtaining the Existing Issuance License

The following example shows how to retrieve an existing issuance license from a compound document. It is a matter of navigating to the \006Primary stream, which contains the existing issuance license, reading the stream header, pulling the issuance license length from the header, then reading the issuance license from the stream.

HRESULT GetPublishingLicense(WCHAR* wszRmhFilePath, 
                             WCHAR** ppwszPubLicense)
{
   WCHAR StorageName_DataSpaces[12]    =    L"*DataSpaces";
   WCHAR StorageName_DataSpaceInfo[14] =    L"DataSpaceInfo";
   WCHAR StorageName_TransformInfo[14] =    L"TransformInfo";
   WCHAR StorageName_DRMTransform[14]  =    L"*DRMTransform";
   WCHAR StreamName_DRMViewerContent[18] =  L"*DRMViewerContent";
   WCHAR StreamName_Version[8]         =    L"Version";
   WCHAR StreamName_DataSpaceMap[13]   =    L"DataSpaceMap";
   WCHAR StreamName_DRMDataSpace[14]   =    L"*DRMDataSpace";
   WCHAR StreamName_Primary[9]         =    L"*Primary";

    HRESULT hResult = NULL;
    IStorage *pStorage = NULL;
    IStorage *pDataspaceStorage = NULL;
    IStorage *pTransformInfoStorage = NULL;
    IStorage *pDRMTransformStorage = NULL;
    IStream  *pStream = NULL;
    BYTE* pbBuffer = NULL;
    WCHAR *pwBuffer = NULL;

    // Copy nonprinting ASCII characters into the names.
    short char_nine=9;
    short char_six=6;
    memcpy(StreamName_DRMDataSpace, &char_nine,2);
    memcpy(StreamName_Primary, &char_six,2);
    memcpy(StorageName_DataSpaces, &char_six,2);

    // Open the output compound file.
    hResult = StgOpenStorageEx( 
                 wcsRmhFilePath,          // File name
                 STGM_READ|               // File access
                    STGM_SHARE_EXCLUSIVE, 
                 STGFMT_STORAGE,          // Compound file
                 0,                       // Required
                 NULL,                    // Required
                 0,                       // Required
                 IID_IStorage,            // IStorage IID
                 (void **)&pStorage);     // IStorage pointer


    // Navigate to \006DataSpaces storage.
    hResult = pStorage->OpenStorage(
                            StorageName_DataSpaces,
                            NULL,
                            STGM_READ|STGM_SHARE_EXCLUSIVE,
                            NULL,
                            0,
                            &pDataspaceStorage);

    // Navigate down to TransformInfo storage.
    hResult = pDataspaceStorage->OpenStorage( 
                         StorageName_TransformInfo,
                         NULL,
                         STGM_READ | STGM_SHARE_EXCLUSIVE,
                         NULL,
                         0,
                         &pTransformInfoStorage);

    // Navigate down to \009DRMTransform storage.
    memcpy(StorageName_DRMTransform,&char_nine,2);
    hResult = pTransformInfoStorage->OpenStorage(
                          StorageName_DRMTransform,
                          NULL,
                          STGM_READ|STGM_SHARE_EXCLUSIVE,
                          NULL,
                          0,
                          &pDRMTransformStorage);

    // Open the \006Primary stream holding existing SIL.
    hResult = pDRMTransformStorage->OpenStream(StreamName_Primary,
                                NULL,
                                STGM_READ|STGM_SHARE_EXCLUSIVE,
                                0,
                                &pStream);

    // Read the SIL.

    // Calculate the header, so you can skip it.
    LPWSTR ClassID = L"{C73DFACD-061F-43B0-8B64-0C620D2A8B50}";
    LPWSTR FeatureIdentifier = L"Microsoft.Metadata.DRMTransform";
    int ClassIDLength = (int)wcslen(ClassID)*sizeof(WCHAR);
    int FeatureIdentifierLength = (int)wcslen(FeatureIdentifier)
                                  *sizeof(WCHAR);

    // Read the header into a buffer. The read position 
    // moves past each block of data you read, so you
    // move past the header.
    int TotalHeaderLen = 38+ClassIDLength+FeatureIdentifierLength;
    pbBuffer = new BYTE[TotalHeaderLen];
    ULONG cbRead = 0;
    hResult = pStream->Read(pbBuffer, TotalHeaderLen, &cbRead);

    // Get the length of the SIL, which is the last 4 bytes 
    // of the header.
    int iPLLength = 0;
    memcpy(&iPLLength, &pbBuffer[TotalHeaderLen-4], 4);

    // Allocate the buffer and read the SIL.
    BYTE* pbRightsLabel = new BYTE[iPLLength];
    hResult = pStream->Read(pbRightsLabel, iPLLength, &cbRead);

    // Convert to Unicode.
    int cwRead = MultiByteToWideChar(
                          CP_UTF8, 
                          NULL, 
                          (LPCTSTR)pbRightsLabel,
                          cbRead,
                          NULL,
                          0);
    if (cwRead > 0)
        pwBuffer = new WCHAR[cbRead];
    cwRead = MultiByteToWideChar(
                          CP_UTF8, 
                          NULL, 
                          (LPCTSTR)pbRightsLabel,
                          cbRead,
                          pwBuffer, 
                          cwRead);

    *ppwszPubLicense = pwBuffer;

e_Exit:
    // Clean up.
    if (pbBuffer != NULL)
        delete[] pbBuffer;
    if (hResult != S_OK)
    {
        delete[] pwBuffer;
        *ppwszPubLicense = NULL;
    }
    if (pStorage != NULL)
        pStorage->Release();
    if (pDataspaceStorage != NULL)
        pDataspaceStorage->Release();
    if (pTransformInfoStorage != NULL)
        pTransformInfoStorage->Release();
    if (pDRMTransformStorage != NULL)
        pDRMTransformStorage->Release();
    if (pStream != NULL)
        pStream->Release();

    return hResult;
}

See Also

Updating the Issuance License

Send comments about this topic to Microsoft

Build date: 3/13/2008