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 #pragma once
21 
22 #if !defined WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN
24 #endif
25 #include <windows.h>
26 #include <objidl.h>
27 #include <WinClip.hxx>
28 
29 #define AUTO_INIT true
30 
31 // a helper class to manage a global memory area, the clients can write
32 // into the global memory area and extract the handle to the global mem
33 // note: not thread-safe
34 
36 {
37 public:
38  // will be thrown in case of failures
40  {
41  public:
42  HRESULT m_hr;
43  explicit CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
44  };
45 
46 public:
48  bool bAutoInit = false,
49  HGLOBAL hGlob = nullptr,
50  bool bDelStgOnRelease = false );
51 
53 
54  void write( const void* lpData, ULONG cb, ULONG* cbWritten = nullptr );
55  void read( LPVOID pv, ULONG cb, ULONG* pcbRead = nullptr );
56 
57  HGLOBAL getHGlobal( ) const;
58  void getIStream( LPSTREAM* ppStream );
59 
60  void init(
61  SIZE_T newSize,
62  sal_uInt32 uiFlags = GHND,
63  bool bDelStgOnRelease = false );
64 
65  void init(
66  HGLOBAL hGlob,
67  bool bDelStgOnRelease = false );
68 
69  // returns the size of the managed memory
70  sal_uInt32 memSize( CLIPFORMAT cf = CF_INVALID ) const;
71 
72  // free the global memory and necessary
73  // release the internal stream pointer
74  void cleanup( );
75 
76 private:
77  LPSTREAM m_lpStream;
79 
80 private:
83 };
84 
85 // something like an auto-pointer - allows access to the memory belonging
86 // to a HGLOBAL and automatically unlocks a global memory at destruction
87 // time
88 
90 {
91 public:
92 
93  // ctor
94 
95  explicit CRawHGlobalPtr( HGLOBAL hGlob ) :
96  m_hGlob( hGlob ),
97  m_bIsLocked( false ),
98  m_pGlobMem( nullptr )
99  {
100  }
101 
102  // ctor
103 
104  explicit CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
105  m_hGlob( theHGlobalHelper.getHGlobal( ) ),
106  m_bIsLocked( false ),
107  m_pGlobMem( nullptr )
108  {
109  }
110 
111  // dtor
112 
114  {
115  if ( m_bIsLocked )
116  GlobalUnlock( m_hGlob );
117  }
118 
119  // lock the global memory (initializes a
120  // pointer to this memory)
121 
123  {
124  if ( !m_bIsLocked && ( nullptr != m_hGlob ) )
125  {
126  m_pGlobMem = GlobalLock( m_hGlob );
127  m_bIsLocked = ( nullptr != m_pGlobMem );
128  }
129 
130  return m_bIsLocked;
131  }
132 
133  // unlock the global memory (invalidates the
134  // pointer to this memory)
135 
137  {
138  GlobalUnlock( m_hGlob );
139  m_bIsLocked = false;
140  m_pGlobMem = nullptr;
141 
142  return ( NO_ERROR == GetLastError( ) );
143  }
144 
145  // locks the global memory and returns a
146  // pointer to this memory
147 
148  LPVOID GetMemPtr( )
149  {
150  Lock( );
151  return m_pGlobMem;
152  }
153 
154  // size of mem we point to
155 
156  int MemSize( ) const
157  {
158  return GlobalSize( m_hGlob );
159  }
160 
161 private:
162  HGLOBAL m_hGlob;
164  LPVOID m_pGlobMem;
165 };
166 
167 /* 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:24