1*fbcf0fe9SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*fbcf0fe9SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*fbcf0fe9SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*fbcf0fe9SAndrew Rist * distributed with this work for additional information 6*fbcf0fe9SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*fbcf0fe9SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*fbcf0fe9SAndrew Rist * "License"); you may not use this file except in compliance 9*fbcf0fe9SAndrew Rist * with the License. You may obtain a copy of the License at 10*fbcf0fe9SAndrew Rist * 11*fbcf0fe9SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*fbcf0fe9SAndrew Rist * 13*fbcf0fe9SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*fbcf0fe9SAndrew Rist * software distributed under the License is distributed on an 15*fbcf0fe9SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*fbcf0fe9SAndrew Rist * KIND, either express or implied. See the License for the 17*fbcf0fe9SAndrew Rist * specific language governing permissions and limitations 18*fbcf0fe9SAndrew Rist * under the License. 19*fbcf0fe9SAndrew Rist * 20*fbcf0fe9SAndrew Rist *************************************************************/ 21*fbcf0fe9SAndrew Rist 22*fbcf0fe9SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir #ifndef _DTRANSHELPER_HXX_ 26cdf0e10cSrcweir #define _DTRANSHELPER_HXX_ 27cdf0e10cSrcweir 28cdf0e10cSrcweir //------------------------------------------------------------------------ 29cdf0e10cSrcweir // includes 30cdf0e10cSrcweir //------------------------------------------------------------------------ 31cdf0e10cSrcweir 32cdf0e10cSrcweir #if defined _MSC_VER 33cdf0e10cSrcweir #pragma warning(push,1) 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir #include <windows.h> 36cdf0e10cSrcweir #if defined _MSC_VER 37cdf0e10cSrcweir #pragma warning(pop) 38cdf0e10cSrcweir #endif 39cdf0e10cSrcweir #include "..\misc\WinClip.hxx" 40cdf0e10cSrcweir 41cdf0e10cSrcweir //------------------------------------------------------------------------ 42cdf0e10cSrcweir // defines 43cdf0e10cSrcweir //------------------------------------------------------------------------ 44cdf0e10cSrcweir 45cdf0e10cSrcweir #define AUTO_INIT TRUE 46cdf0e10cSrcweir #define NO_AUTO_INIT FALSE 47cdf0e10cSrcweir #define MEM_DESTROY_ON_RELEASE TRUE 48cdf0e10cSrcweir #define NO_MEM_DESTROY_ON_RELEASE FALSE 49cdf0e10cSrcweir 50cdf0e10cSrcweir //------------------------------------------------------------------------ 51cdf0e10cSrcweir // deklarations 52cdf0e10cSrcweir //------------------------------------------------------------------------ 53cdf0e10cSrcweir 54cdf0e10cSrcweir 55cdf0e10cSrcweir //------------------------------------------------------------------------- 56cdf0e10cSrcweir // a helper class to manage a global memory area, the clients can write 57cdf0e10cSrcweir // into the global memory area and extract the handle to the global mem 58cdf0e10cSrcweir // note: not thread-safe 59cdf0e10cSrcweir //------------------------------------------------------------------------- 60cdf0e10cSrcweir 61cdf0e10cSrcweir class CStgTransferHelper 62cdf0e10cSrcweir { 63cdf0e10cSrcweir public: 64cdf0e10cSrcweir // will be thrown in case of failures 65cdf0e10cSrcweir class CStgTransferException 66cdf0e10cSrcweir { 67cdf0e10cSrcweir public: 68cdf0e10cSrcweir HRESULT m_hr; CStgTransferException(HRESULT hr)69cdf0e10cSrcweir CStgTransferException( HRESULT hr ) : m_hr( hr ) {}; 70cdf0e10cSrcweir }; 71cdf0e10cSrcweir 72cdf0e10cSrcweir public: 73cdf0e10cSrcweir CStgTransferHelper( 74cdf0e10cSrcweir sal_Bool bAutoInit = sal_False, 75cdf0e10cSrcweir HGLOBAL hGlob = NULL, 76cdf0e10cSrcweir sal_Bool bDelStgOnRelease = sal_False ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir ~CStgTransferHelper( ); 79cdf0e10cSrcweir 80cdf0e10cSrcweir void SAL_CALL write( const void* lpData, ULONG cb, ULONG* cbWritten = NULL ); 81cdf0e10cSrcweir void SAL_CALL read( LPVOID pv, ULONG cb, ULONG* pcbRead = NULL ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir HGLOBAL SAL_CALL getHGlobal( ) const; 84cdf0e10cSrcweir void SAL_CALL getIStream( LPSTREAM* ppStream ); 85cdf0e10cSrcweir 86cdf0e10cSrcweir void SAL_CALL init( 87cdf0e10cSrcweir SIZE_T newSize, 88cdf0e10cSrcweir sal_uInt32 uiFlags = GHND, 89cdf0e10cSrcweir sal_Bool bDelStgOnRelease = sal_False ); 90cdf0e10cSrcweir 91cdf0e10cSrcweir void SAL_CALL init( 92cdf0e10cSrcweir HGLOBAL hGlob, 93cdf0e10cSrcweir sal_Bool bDelStgOnRelease = sal_False ); 94cdf0e10cSrcweir 95cdf0e10cSrcweir // returns the size of the managed memory 96cdf0e10cSrcweir sal_uInt32 SAL_CALL memSize( CLIPFORMAT cf = CF_INVALID ) const; 97cdf0e10cSrcweir 98cdf0e10cSrcweir // free the global memory and necessary 99cdf0e10cSrcweir // release the internal stream pointer 100cdf0e10cSrcweir void SAL_CALL cleanup( ); 101cdf0e10cSrcweir 102cdf0e10cSrcweir private: 103cdf0e10cSrcweir LPSTREAM m_lpStream; 104cdf0e10cSrcweir sal_Bool m_bDelStgOnRelease; 105cdf0e10cSrcweir 106cdf0e10cSrcweir private: 107cdf0e10cSrcweir CStgTransferHelper( const CStgTransferHelper& ); 108cdf0e10cSrcweir CStgTransferHelper& operator=( const CStgTransferHelper& ); 109cdf0e10cSrcweir }; 110cdf0e10cSrcweir 111cdf0e10cSrcweir //------------------------------------------------------------------------- 112cdf0e10cSrcweir // something like an auto-pointer - allows access to the memory belonging 113cdf0e10cSrcweir // to a HGLOBAL and automatically unlocks a global memory at destruction 114cdf0e10cSrcweir // time 115cdf0e10cSrcweir //------------------------------------------------------------------------- 116cdf0e10cSrcweir 117cdf0e10cSrcweir class CRawHGlobalPtr 118cdf0e10cSrcweir { 119cdf0e10cSrcweir public: 120cdf0e10cSrcweir 121cdf0e10cSrcweir //--------------------------------------------- 122cdf0e10cSrcweir // ctor 123cdf0e10cSrcweir //--------------------------------------------- 124cdf0e10cSrcweir CRawHGlobalPtr(HGLOBAL hGlob)125cdf0e10cSrcweir CRawHGlobalPtr( HGLOBAL hGlob ) : 126cdf0e10cSrcweir m_hGlob( hGlob ), 127cdf0e10cSrcweir m_bIsLocked( FALSE ), 128cdf0e10cSrcweir m_pGlobMem( NULL ) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir 133cdf0e10cSrcweir //--------------------------------------------- 134cdf0e10cSrcweir // ctor 135cdf0e10cSrcweir //--------------------------------------------- 136cdf0e10cSrcweir CRawHGlobalPtr(const CStgTransferHelper & theHGlobalHelper)137cdf0e10cSrcweir CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) : 138cdf0e10cSrcweir m_hGlob( theHGlobalHelper.getHGlobal( ) ), 139cdf0e10cSrcweir m_bIsLocked( FALSE ), 140cdf0e10cSrcweir m_pGlobMem( NULL ) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir //--------------------------------------------- 145cdf0e10cSrcweir // dtor 146cdf0e10cSrcweir //--------------------------------------------- 147cdf0e10cSrcweir ~CRawHGlobalPtr()148cdf0e10cSrcweir ~CRawHGlobalPtr( ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir if ( m_bIsLocked ) 151cdf0e10cSrcweir GlobalUnlock( m_hGlob ); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir //--------------------------------------------- 155cdf0e10cSrcweir // lock the global memory (initializes a 156cdf0e10cSrcweir // pointer to this memory) 157cdf0e10cSrcweir //--------------------------------------------- 158cdf0e10cSrcweir Lock()159cdf0e10cSrcweir BOOL Lock( ) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir if ( !m_bIsLocked && ( NULL != m_hGlob ) ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir m_pGlobMem = GlobalLock( m_hGlob ); 164cdf0e10cSrcweir m_bIsLocked = ( NULL != m_pGlobMem ); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir 167cdf0e10cSrcweir return m_bIsLocked; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir 170cdf0e10cSrcweir //--------------------------------------------- 171cdf0e10cSrcweir // unlock the global memory (invalidates the 172cdf0e10cSrcweir // pointer to this memory) 173cdf0e10cSrcweir //--------------------------------------------- 174cdf0e10cSrcweir Unlock()175cdf0e10cSrcweir BOOL Unlock( ) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir GlobalUnlock( m_hGlob ); 178cdf0e10cSrcweir m_bIsLocked = FALSE; 179cdf0e10cSrcweir m_pGlobMem = NULL; 180cdf0e10cSrcweir 181cdf0e10cSrcweir return ( NO_ERROR == GetLastError( ) ); 182cdf0e10cSrcweir } 183cdf0e10cSrcweir 184cdf0e10cSrcweir //--------------------------------------------- 185cdf0e10cSrcweir // locks the global memory and returns a 186cdf0e10cSrcweir // pointer to this memory 187cdf0e10cSrcweir //--------------------------------------------- 188cdf0e10cSrcweir GetMemPtr()189cdf0e10cSrcweir LPVOID GetMemPtr( ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir Lock( ); 192cdf0e10cSrcweir return m_pGlobMem; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir 195cdf0e10cSrcweir //--------------------------------------------- 196cdf0e10cSrcweir // size of mem we point to 197cdf0e10cSrcweir //--------------------------------------------- 198cdf0e10cSrcweir MemSize() const199cdf0e10cSrcweir int MemSize( ) const 200cdf0e10cSrcweir { 201cdf0e10cSrcweir return GlobalSize( m_hGlob ); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir private: 205cdf0e10cSrcweir HGLOBAL m_hGlob; 206cdf0e10cSrcweir BOOL m_bIsLocked; 207cdf0e10cSrcweir LPVOID m_pGlobMem; 208cdf0e10cSrcweir }; 209cdf0e10cSrcweir 210cdf0e10cSrcweir #endif 211