xref: /trunk/main/dtrans/source/win32/workbench/testmarshal.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dtrans.hxx"
30 
31 
32 //_________________________________________________________________________________________________________________________
33 //  interface includes
34 //_________________________________________________________________________________________________________________________
35 
36 //_________________________________________________________________________________________________________________________
37 //  other includes
38 //_________________________________________________________________________________________________________________________
39 #include <rtl/ustring.hxx>
40 #include <sal/types.h>
41 #include <osl/diagnose.h>
42 
43 #include <stdio.h>
44 #if defined _MSC_VER
45 #pragma warning(push,1)
46 #endif
47 #include <windows.h>
48 #include <objbase.h>
49 #if defined _MSC_VER
50 #pragma warning(pop)
51 #endif
52 
53 #include <memory>
54 
55 #include <process.h>
56 #include "XTDo.hxx"
57 
58 //-------------------------------------------------------------
59 // my defines
60 //-------------------------------------------------------------
61 
62 #define WRITE_CB
63 #define EVT_MANUAL_RESET     TRUE
64 #define EVT_INIT_NONSIGNALED FALSE
65 #define EVT_NONAME           ""
66 #define WAIT_MSGLOOP
67 #define RAW_MARSHALING
68 
69 //------------------------------------------------------------
70 //  namesapces
71 //------------------------------------------------------------
72 
73 using namespace ::rtl;
74 using namespace ::std;
75 
76 //------------------------------------------------------------
77 //  globales
78 //------------------------------------------------------------
79 
80 HANDLE  g_hEvtThreadWakeup;
81 
82 #ifdef RAW_MARSHALING
83     HGLOBAL g_hGlob;
84 #else
85     IStream* g_pStm;
86 #endif
87 
88 //################################################################
89 // a thread in another apartment to test apartment transparency
90 
91 unsigned int _stdcall ThreadProc(LPVOID pParam)
92 {
93     // setup another apartment
94     HRESULT hr = OleInitialize( NULL );
95 
96     WaitForSingleObject( g_hEvtThreadWakeup, INFINITE );
97 
98     IDataObject* pIDo;
99 
100 #ifdef RAW_MARSHALING
101 
102     IStream* pStm = NULL;
103     hr = CreateStreamOnHGlobal( g_hGlob, FALSE, &pStm );
104     if ( SUCCEEDED( hr ) )
105     {
106         hr = CoUnmarshalInterface(
107                 pStm,
108                 __uuidof( IDataObject ),
109                 (void**)&pIDo );
110 
111         hr = pStm->Release( );
112     }
113 
114 #else
115 
116     hr = CoGetInterfaceAndReleaseStream(
117         g_pStm,
118         __uuidof( IDataObject ),
119         (void**)&pIDo
120         );
121 
122 #endif
123 
124     IEnumFORMATETC* pIEEtc;
125     hr = pIDo->EnumFormatEtc( DATADIR_GET, &pIEEtc );
126 
127     hr = OleIsCurrentClipboard( pIDo );
128 
129     hr = OleFlushClipboard( );
130 
131     OleUninitialize( );
132 
133     return 0;
134 }
135 
136 //################################################################
137 
138 //----------------------------------------------------------------
139 //  main
140 //----------------------------------------------------------------
141 
142 int SAL_CALL main( int nArgc, char* Argv[] )
143 {
144     HRESULT hr = OleInitialize( NULL );
145 
146     g_hEvtThreadWakeup = CreateEvent( 0,
147                                       EVT_MANUAL_RESET,
148                                       EVT_INIT_NONSIGNALED,
149                                       EVT_NONAME );
150 
151     unsigned uThreadId;
152     HANDLE   hThread;
153 
154     // create a thread in another apartment
155     hThread = (void*)_beginthreadex( NULL, 0, ThreadProc, NULL, 0, &uThreadId );
156 
157     IDataObject* pIDo = new CXTDataObject( );
158 
159     hr = OleSetClipboard( pIDo );
160     hr = E_FAIL;
161 
162     hr = OleIsCurrentClipboard( pIDo );
163 
164     //hr = OleGetClipboard( &pIDo );
165     if ( SUCCEEDED( hr ) )
166     {
167 #ifdef RAW_MARSHALING
168 
169         IStream* pStm = NULL;
170 
171         hr = CreateStreamOnHGlobal( 0, FALSE, &pStm );
172         if ( SUCCEEDED( hr ) )
173         {
174             hr = CoMarshalInterface(
175                 pStm,
176                 __uuidof( IDataObject ),
177                 pIDo,
178                 MSHCTX_INPROC,
179                 0,
180                 MSHLFLAGS_NORMAL );
181             if ( SUCCEEDED( hr ) )
182                 hr = GetHGlobalFromStream( pStm, &g_hGlob );
183 
184             hr = pStm->Release( );
185         }
186 
187 #else
188 
189         hr = CoMarshalInterThreadInterfaceInStream(
190                 __uuidof( IDataObject ),
191                 pIDo,
192                 &g_pStm );
193 
194 #endif
195 
196         if ( SUCCEEDED( hr ) )
197         {
198             // wakeup the thread and waiting util it ends
199             SetEvent( g_hEvtThreadWakeup );
200 
201 #ifdef WAIT_MSGLOOP
202 
203             BOOL bContinue = TRUE;
204 
205             while( bContinue )
206             {
207                 DWORD dwResult = WaitForMultipleObjects(
208                     1,
209                     &hThread,
210                     TRUE,
211                     0 );
212 
213                 if ( WAIT_OBJECT_0 == dwResult )
214                 {
215                     bContinue = FALSE;
216                 }
217                 else
218                 {
219                     MSG msg;
220                     while( PeekMessage(
221                             &msg,
222                             NULL,
223                             0,
224                             0,
225                             PM_REMOVE ) )
226                     {
227                         TranslateMessage(&msg);
228                         DispatchMessage(&msg);
229                     }
230                 }
231             } // while
232 
233 #endif
234 
235         } // if
236     } // if
237 
238     OleFlushClipboard( );
239 
240     OleUninitialize( );
241 
242     return 0;
243 }
244