LibreOffice Module dtrans (master)  1
DTransHelper.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_DTRANS_SOURCE_WIN32_DTOBJ_DTRANSHELPER_HXX
21 #define INCLUDED_DTRANS_SOURCE_WIN32_DTOBJ_DTRANSHELPER_HXX
22 
23 #if !defined WIN32_LEAN_AND_MEAN
24 # define WIN32_LEAN_AND_MEAN
25 #endif
26 #include <windows.h>
27 #include <objidl.h>
28 #include <WinClip.hxx>
29 
30 #define AUTO_INIT true
31 
32 // a helper class to manage a global memory area, the clients can write
33 // into the global memory area and extract the handle to the global mem
34 // note: not thread-safe
35 
37 {
38 public:
39  // will be thrown in case of failures
41  {
42  public:
43  HRESULT m_hr;
44  explicit CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
45  };
46 
47 public:
49  bool bAutoInit = false,
50  HGLOBAL hGlob = nullptr,
51  bool bDelStgOnRelease = false );
52 
54 
55  void write( const void* lpData, ULONG cb, ULONG* cbWritten = nullptr );
56  void read( LPVOID pv, ULONG cb, ULONG* pcbRead = nullptr );
57 
58  HGLOBAL getHGlobal( ) const;
59  void getIStream( LPSTREAM* ppStream );
60 
61  void init(
62  SIZE_T newSize,
63  sal_uInt32 uiFlags = GHND,
64  bool bDelStgOnRelease = false );
65 
66  void init(
67  HGLOBAL hGlob,
68  bool bDelStgOnRelease = false );
69 
70  // returns the size of the managed memory
71  sal_uInt32 memSize( CLIPFORMAT cf = CF_INVALID ) const;
72 
73  // free the global memory and necessary
74  // release the internal stream pointer
75  void cleanup( );
76 
77 private:
78  LPSTREAM m_lpStream;
80 
81 private:
84 };
85 
86 // something like an auto-pointer - allows access to the memory belonging
87 // to a HGLOBAL and automatically unlocks a global memory at destruction
88 // time
89 
91 {
92 public:
93 
94  // ctor
95 
96  explicit CRawHGlobalPtr( HGLOBAL hGlob ) :
97  m_hGlob( hGlob ),
98  m_bIsLocked( false ),
99  m_pGlobMem( nullptr )
100  {
101  }
102 
103  // ctor
104 
105  explicit CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
106  m_hGlob( theHGlobalHelper.getHGlobal( ) ),
107  m_bIsLocked( false ),
108  m_pGlobMem( nullptr )
109  {
110  }
111 
112  // dtor
113 
115  {
116  if ( m_bIsLocked )
117  GlobalUnlock( m_hGlob );
118  }
119 
120  // lock the global memory (initializes a
121  // pointer to this memory)
122 
124  {
125  if ( !m_bIsLocked && ( nullptr != m_hGlob ) )
126  {
127  m_pGlobMem = GlobalLock( m_hGlob );
128  m_bIsLocked = ( nullptr != m_pGlobMem );
129  }
130 
131  return m_bIsLocked;
132  }
133 
134  // unlock the global memory (invalidates the
135  // pointer to this memory)
136 
138  {
139  GlobalUnlock( m_hGlob );
140  m_bIsLocked = false;
141  m_pGlobMem = nullptr;
142 
143  return ( NO_ERROR == GetLastError( ) );
144  }
145 
146  // locks the global memory and returns a
147  // pointer to this memory
148 
149  LPVOID GetMemPtr( )
150  {
151  Lock( );
152  return m_pGlobMem;
153  }
154 
155  // size of mem we point to
156 
157  int MemSize( ) const
158  {
159  return GlobalSize( m_hGlob );
160  }
161 
162 private:
163  HGLOBAL m_hGlob;
165  LPVOID m_pGlobMem;
166 };
167 
168 #endif
169 
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NO_ERROR
CStgTransferHelper(bool bAutoInit=false, HGLOBAL hGlob=nullptr, bool bDelStgOnRelease=false)
sal_uInt32 memSize(CLIPFORMAT cf=CF_INVALID) const
HGLOBAL getHGlobal() const
CRawHGlobalPtr(HGLOBAL hGlob)
LPVOID GetMemPtr()
CStgTransferHelper & operator=(const CStgTransferHelper &)
const wchar_t *typedef BOOL
void getIStream(LPSTREAM *ppStream)
int MemSize() const
void write(const void *lpData, ULONG cb, ULONG *cbWritten=nullptr)
CRawHGlobalPtr(const CStgTransferHelper &theHGlobalHelper)
void init(SIZE_T newSize, sal_uInt32 uiFlags=GHND, bool bDelStgOnRelease=false)
void read(LPVOID pv, ULONG cb, ULONG *pcbRead=nullptr)
const sal_Int32 CF_INVALID
Definition: WinClip.hxx:25