1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #ifndef _DTRANSHELPER_HXX_
22 #define _DTRANSHELPER_HXX_
25 #pragma warning(push,1)
31 #include "../misc/WinClip.hxx"
33 #define AUTO_INIT TRUE
34 #define NO_AUTO_INIT FALSE
35 #define MEM_DESTROY_ON_RELEASE TRUE
36 #define NO_MEM_DESTROY_ON_RELEASE FALSE
38 //-------------------------------------------------------------------------
39 // a helper class to manage a global memory area, the clients can write
40 // into the global memory area and extract the handle to the global mem
41 // note: not thread-safe
42 //-------------------------------------------------------------------------
44 class CStgTransferHelper
47 // will be thrown in case of failures
48 class CStgTransferException
52 CStgTransferException( HRESULT hr
) : m_hr( hr
) {};
57 sal_Bool bAutoInit
= sal_False
,
59 sal_Bool bDelStgOnRelease
= sal_False
);
61 ~CStgTransferHelper( );
63 void SAL_CALL
write( const void* lpData
, ULONG cb
, ULONG
* cbWritten
= NULL
);
64 void SAL_CALL
read( LPVOID pv
, ULONG cb
, ULONG
* pcbRead
= NULL
);
66 HGLOBAL SAL_CALL
getHGlobal( ) const;
67 void SAL_CALL
getIStream( LPSTREAM
* ppStream
);
71 sal_uInt32 uiFlags
= GHND
,
72 sal_Bool bDelStgOnRelease
= sal_False
);
76 sal_Bool bDelStgOnRelease
= sal_False
);
78 // returns the size of the managed memory
79 sal_uInt32 SAL_CALL
memSize( CLIPFORMAT cf
= CF_INVALID
) const;
81 // free the global memory and necessary
82 // release the internal stream pointer
83 void SAL_CALL
cleanup( );
87 sal_Bool m_bDelStgOnRelease
;
90 CStgTransferHelper( const CStgTransferHelper
& );
91 CStgTransferHelper
& operator=( const CStgTransferHelper
& );
94 //-------------------------------------------------------------------------
95 // something like an auto-pointer - allows access to the memory belonging
96 // to a HGLOBAL and automatically unlocks a global memory at destruction
98 //-------------------------------------------------------------------------
104 //---------------------------------------------
106 //---------------------------------------------
108 CRawHGlobalPtr( HGLOBAL hGlob
) :
110 m_bIsLocked( FALSE
),
116 //---------------------------------------------
118 //---------------------------------------------
120 CRawHGlobalPtr( const CStgTransferHelper
& theHGlobalHelper
) :
121 m_hGlob( theHGlobalHelper
.getHGlobal( ) ),
122 m_bIsLocked( FALSE
),
127 //---------------------------------------------
129 //---------------------------------------------
134 GlobalUnlock( m_hGlob
);
137 //---------------------------------------------
138 // lock the global memory (initializes a
139 // pointer to this memory)
140 //---------------------------------------------
144 if ( !m_bIsLocked
&& ( NULL
!= m_hGlob
) )
146 m_pGlobMem
= GlobalLock( m_hGlob
);
147 m_bIsLocked
= ( NULL
!= m_pGlobMem
);
153 //---------------------------------------------
154 // unlock the global memory (invalidates the
155 // pointer to this memory)
156 //---------------------------------------------
160 GlobalUnlock( m_hGlob
);
164 return ( NO_ERROR
== GetLastError( ) );
167 //---------------------------------------------
168 // locks the global memory and returns a
169 // pointer to this memory
170 //---------------------------------------------
178 //---------------------------------------------
179 // size of mem we point to
180 //---------------------------------------------
184 return GlobalSize( m_hGlob
);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */