Article ID: 152533
Article Last Modified on 11/21/2006
119591 How to Obtain Microsoft Support Files from Online Services
//////////////////////////////////////////////////////////////////
// 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: kbdownload kbcode kbdatabase kbfile kbsample KB152533