LibreOffice Module dtrans (master)  1
testmarshal.cxx
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 #include <rtl/ustring.hxx>
21 #include <sal/types.h>
22 
23 #include <stdio.h>
24 #if defined _MSC_VER
25 #pragma warning(push,1)
26 #endif
27 #include <windows.h>
28 #include <objbase.h>
29 #if defined _MSC_VER
30 #pragma warning(pop)
31 #endif
32 
33 #include <memory>
34 
35 #include <process.h>
36 #include "XTDo.hxx"
37 
38 // my defines
39 
40 #define WRITE_CB
41 #define EVT_MANUAL_RESET TRUE
42 #define EVT_INIT_NONSIGNALED FALSE
43 #define EVT_NONAME ""
44 #define WAIT_MSGLOOP
45 #define RAW_MARSHALING
46 
47 // namespaces
48 
49 using namespace ::std;
50 
51 // globales
52 
54 
55 #ifdef RAW_MARSHALING
56  HGLOBAL g_hGlob;
57 #else
58  IStream* g_pStm;
59 #endif
60 
61 // a thread in another apartment to test apartment transparency
62 
63 unsigned int _stdcall ThreadProc(LPVOID pParam)
64 {
65  // setup another apartment
66  HRESULT hr = OleInitialize( NULL );
67 
68  WaitForSingleObject( g_hEvtThreadWakeup, INFINITE );
69 
70  IDataObject* pIDo = NULL;
71 
72 #ifdef RAW_MARSHALING
73 
74  IStream* pStm = NULL;
75  hr = CreateStreamOnHGlobal( g_hGlob, FALSE, &pStm );
76  if ( SUCCEEDED( hr ) )
77  {
78  hr = CoUnmarshalInterface(
79  pStm,
80  __uuidof( IDataObject ),
81  (void**)&pIDo );
82 
83  hr = pStm->Release( );
84  }
85 
86 #else
87 
88  hr = CoGetInterfaceAndReleaseStream(
89  g_pStm,
90  __uuidof( IDataObject ),
91  (void**)&pIDo
92  );
93 
94 #endif
95 
96  IEnumFORMATETC* pIEEtc;
97  hr = pIDo->EnumFormatEtc( DATADIR_GET, &pIEEtc );
98 
99  hr = OleIsCurrentClipboard( pIDo );
100 
101  hr = OleFlushClipboard( );
102 
103  OleUninitialize( );
104 
105  return 0;
106 }
107 
108 // main
109 
110 int SAL_CALL main( int nArgc, char* Argv[] )
111 {
112  HRESULT hr = OleInitialize( NULL );
113 
114  g_hEvtThreadWakeup = CreateEvent( 0,
117  EVT_NONAME );
118 
119  unsigned uThreadId;
120  HANDLE hThread;
121 
122  // create a thread in another apartment
123  hThread = (void*)_beginthreadex( NULL, 0, ThreadProc, NULL, 0, &uThreadId );
124 
125  IDataObject* pIDo = new CXTDataObject( );
126 
127  hr = OleSetClipboard( pIDo );
128  hr = E_FAIL;
129 
130  hr = OleIsCurrentClipboard( pIDo );
131 
132  //hr = OleGetClipboard( &pIDo );
133  if ( SUCCEEDED( hr ) )
134  {
135 #ifdef RAW_MARSHALING
136 
137  IStream* pStm = NULL;
138 
139  hr = CreateStreamOnHGlobal( 0, FALSE, &pStm );
140  if ( SUCCEEDED( hr ) )
141  {
142  hr = CoMarshalInterface(
143  pStm,
144  __uuidof( IDataObject ),
145  pIDo,
146  MSHCTX_INPROC,
147  0,
148  MSHLFLAGS_NORMAL );
149  if ( SUCCEEDED( hr ) )
150  hr = GetHGlobalFromStream( pStm, &g_hGlob );
151 
152  hr = pStm->Release( );
153  }
154 
155 #else
156 
157  hr = CoMarshalInterThreadInterfaceInStream(
158  __uuidof( IDataObject ),
159  pIDo,
160  &g_pStm );
161 
162 #endif
163 
164  if ( SUCCEEDED( hr ) )
165  {
166  // wakeup the thread and waiting util it ends
167  SetEvent( g_hEvtThreadWakeup );
168 
169 #ifdef WAIT_MSGLOOP
170 
171  BOOL bContinue = TRUE;
172 
173  while( bContinue )
174  {
175  DWORD dwResult = WaitForMultipleObjects(
176  1,
177  &hThread,
178  TRUE,
179  0 );
180 
181  if ( WAIT_OBJECT_0 == dwResult )
182  {
183  bContinue = FALSE;
184  }
185  else
186  {
187  MSG msg;
188  while( PeekMessage(
189  &msg,
190  NULL,
191  0,
192  0,
193  PM_REMOVE ) )
194  {
195  TranslateMessage(&msg);
196  DispatchMessage(&msg);
197  }
198  }
199  } // while
200 
201 #endif
202 
203  } // if
204  } // if
205 
206  OleFlushClipboard( );
207 
208  OleUninitialize( );
209 
210  return 0;
211 }
212 
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
EXTERN_C BOOL BOOL const wchar_t *pProgramPath HRESULT hr
#define EVT_MANUAL_RESET
Definition: testmarshal.cxx:41
int SAL_CALL main(int nArgc, char *Argv[])
return NULL
const wchar_t *typedef BOOL
#define EVT_NONAME
Definition: testmarshal.cxx:43
#define TRUE
unsigned int _stdcall ThreadProc(LPVOID pParam)
Definition: testmarshal.cxx:63
#define EVT_INIT_NONSIGNALED
Definition: testmarshal.cxx:42
#define FALSE
HANDLE g_hEvtThreadWakeup
Definition: testmarshal.cxx:53
HGLOBAL g_hGlob
Definition: testmarshal.cxx:56