1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DTransHelper.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #ifndef _DTRANSHELPER_HXX_
33 #define _DTRANSHELPER_HXX_
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
40 #pragma warning(push,1)
46 #include "..\misc\WinClip.hxx"
48 //------------------------------------------------------------------------
50 //------------------------------------------------------------------------
52 #define AUTO_INIT TRUE
53 #define NO_AUTO_INIT FALSE
54 #define MEM_DESTROY_ON_RELEASE TRUE
55 #define NO_MEM_DESTROY_ON_RELEASE FALSE
57 //------------------------------------------------------------------------
59 //------------------------------------------------------------------------
62 //-------------------------------------------------------------------------
63 // a helper class to manage a global memory area, the clients can write
64 // into the global memory area and extract the handle to the global mem
65 // note: not thread-safe
66 //-------------------------------------------------------------------------
68 class CStgTransferHelper
71 // will be thrown in case of failures
72 class CStgTransferException
76 CStgTransferException( HRESULT hr
) : m_hr( hr
) {};
81 sal_Bool bAutoInit
= sal_False
,
83 sal_Bool bDelStgOnRelease
= sal_False
);
85 ~CStgTransferHelper( );
87 void SAL_CALL
write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
= NULL
);
88 void SAL_CALL
read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
= NULL
);
90 HGLOBAL SAL_CALL
getHGlobal( ) const;
91 void SAL_CALL
getIStream( LPSTREAM
* ppStream
);
95 sal_uInt32 uiFlags
= GHND
,
96 sal_Bool bDelStgOnRelease
= sal_False
);
100 sal_Bool bDelStgOnRelease
= sal_False
);
102 // returns the size of the managed memory
103 sal_uInt32 SAL_CALL
memSize( CLIPFORMAT cf
= CF_INVALID
) const;
105 // free the global memory and necessary
106 // release the internal stream pointer
107 void SAL_CALL
cleanup( );
111 sal_Bool m_bDelStgOnRelease
;
114 CStgTransferHelper( const CStgTransferHelper
& );
115 CStgTransferHelper
& operator=( const CStgTransferHelper
& );
118 //-------------------------------------------------------------------------
119 // something like an auto-pointer - allows access to the memory belonging
120 // to a HGLOBAL and automatically unlocks a global memory at destruction
122 //-------------------------------------------------------------------------
128 //---------------------------------------------
130 //---------------------------------------------
132 CRawHGlobalPtr( HGLOBAL hGlob
) :
134 m_bIsLocked( FALSE
),
140 //---------------------------------------------
142 //---------------------------------------------
144 CRawHGlobalPtr( const CStgTransferHelper
& theHGlobalHelper
) :
145 m_hGlob( theHGlobalHelper
.getHGlobal( ) ),
146 m_bIsLocked( FALSE
),
151 //---------------------------------------------
153 //---------------------------------------------
158 GlobalUnlock( m_hGlob
);
161 //---------------------------------------------
162 // lock the global memory (initializes a
163 // pointer to this memory)
164 //---------------------------------------------
168 if ( !m_bIsLocked
&& ( NULL
!= m_hGlob
) )
170 m_pGlobMem
= GlobalLock( m_hGlob
);
171 m_bIsLocked
= ( NULL
!= m_pGlobMem
);
177 //---------------------------------------------
178 // unlock the global memory (invalidates the
179 // pointer to this memory)
180 //---------------------------------------------
184 GlobalUnlock( m_hGlob
);
188 return ( NO_ERROR
== GetLastError( ) );
191 //---------------------------------------------
192 // locks the global memory and returns a
193 // pointer to this memory
194 //---------------------------------------------
202 //---------------------------------------------
203 // size of mem we point to
204 //---------------------------------------------
208 return GlobalSize( m_hGlob
);