SAMPLE: CLIBIN.EXE Converting COleClientItem into CLongBinary

ID: Q152533


The information in this article applies to:


SUMMARY

CLIBIN.EXE is a Microsoft Foundation Class sample that demonstrates how to write the data from an OLE Embedded Item to an OLE Object field in an Access 7.0 Database. This particular sample uses the MFC/ODBC classes.

The following file is available for download from the Microsoft Software Library:

Clibin.exe
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
NOTE: Use the -d option when running CLIBIN.EXE to decompress the file and recreate the proper directory structure.


MORE INFORMATION

This sample writes the OLE Embedded Item's data to an Access OLE Object field from within the Container App's OnInsertItem member of the CView class. There is an Access database that goes along with this sample located in the project's directory called "longbin.mdb." Remember to register this .mdb file with the ODBC Administrator.

The following code demonstrates how to accomplish this procedure:

Sample Code


    ////////////////////////////////////////////////////////////////// 
    // Extract COleClientItem data and write it out to an Access Field
    // m_pSet is the recordset object
    // The recordset's CLongBinary member is: m_longbin
    // pItem is the COleClientItem object ( embed item )

    /****************************************************************/ 
    //NOTE: This is a constant large enough for an average Winword
    // document. Increase this number if the item you will be
    // inserting is larger!!!!!!!!
    const int DOCSIZE = 100000;

    LPPERSISTSTORAGE pPersist = NULL;
    LPLOCKBYTES pLockBytes = NULL;
    LPSTORAGE pStorage = NULL;
    LPVOID pVoid = NULL;
    HRESULT hr = S_OK;

    // make sure the recordset is in Add mode
    m_pSet->AddNew( );

    // free the current memory of the CLongBinary
    GlobalFree( m_pSet->m_longbin.m_hData );

    // pItem ( the COleClientItem object ) has a contained IOleObject
    // interface called m_lpObject.
    // Call QI to get the OLE objects data.
    if( FAILED( pItem->m_lpObject->QueryInterface( IID_IPersistStorage,
                                               (LPVOID*)&pPersist ) ) )
    {
        AfxMessageBox( _T( "interface not found" ) );
        return;
    }

    // Alloc the CLongBinary
    if( ( m_pSet->m_longbin.m_hData =
                     GlobalAlloc( GMEM_MOVEABLE, DOCSIZE ) ) == NULL )
    {
        AfxMessageBox( _T( "memory error" ) );
        goto term1; // special error condition clean-up label
    }

    // Now lock it so we can write to it.
    if( ( pVoid = GlobalLock( m_pSet->m_longbin.m_hData ) ) == NULL )
    {
        AfxMessageBox( _T( "memory error" ) );
        goto term1; // special error condition clean-up label
    }

    // Create ILockBytes to be converted to an IStorage
    if( FAILED( CreateILockBytesOnHGlobal( m_pSet->m_longbin.m_hData,
                                            FALSE, &pLockBytes ) ) )
    {
        AfxMessageBox( _T( "interface not found" ) );
        goto term1; // special error condition clean-up label
    }

    // Convert the ILockBytes to an IStorage
    if( FAILED( StgCreateDocfileOnILockBytes( pLockBytes, STGM_DIRECT
               | STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
                                                        0, &pStorage ) ) )
    {
        AfxMessageBox( _T( "interface not found" ) );
        goto term2; // special error condition clean-up label
    }

    // Now we can write the item's persistent data into the storage
    // we've provided.
    if( FAILED( pPersist->Save( pStorage, FALSE ) ) )
    {
        AfxMessageBox( _T( "Save failed" ) );
        goto term3; // special error condition clean-up label
    }

    // manage the field state for CLongBinary RFX
    m_pSet->SetFieldDirty( &m_pSet->m_longbin, TRUE );
    m_pSet->SetFieldNull( &m_pSet->m_longbin, FALSE );

    // set the CLongBinary length
    m_pSet->m_longbin.m_dwDataLength =
                              GlobalSize(m_pSet->m_longbin.m_hData );

    // Update the datasource
    m_pSet->Update( );

    // clean-up
    GlobalFree( m_pSet->m_longbin.m_hData );
    pStorage->Release( );
    pLockBytes->Release( );
    pPersist->Release( );
    return; 

Additional query words: 4.00 4.10


Keywords          : kbcode kbsample kbDAO kbDatabase kbMFC kbODBC kbVC 
Version           : 4.00 4.10
Platform          : NT WINDOWS 
Issue type        : 

Last Reviewed: August 5, 1999