SAMPLE: CLIBIN.EXE Converting COleClientItem into CLongBinary

Last reviewed: July 2, 1997
Article ID: Q152533
4.00 4.10 WINDOWS NT kbprg kbfile kbole kbcode

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with: Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1

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 (size: 61541 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : 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;


KBCategory: kbprg kbfile kbole kbcode
KBSubcategory: MfcDatabase dbDao kbsample
Additional reference words: 4.00 4.10
Keywords : dbDao kbsample MfcDatabase kbcode kbfile kbole kbprg
Technology : kbMfc
Version : 4.00 4.10
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.