xref: /trunk/main/dtrans/source/win32/workbench/testmarshal.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
148123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
348123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
448123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
548123e16SAndrew Rist  * distributed with this work for additional information
648123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
748123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
848123e16SAndrew Rist  * "License"); you may not use this file except in compliance
948123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1148123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1348123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1448123e16SAndrew Rist  * software distributed under the License is distributed on an
1548123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1648123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
1748123e16SAndrew Rist  * specific language governing permissions and limitations
1848123e16SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2048123e16SAndrew Rist  *************************************************************/
2148123e16SAndrew Rist 
2248123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_________________________________________________________________________________________________________________________
29cdf0e10cSrcweir //  interface includes
30cdf0e10cSrcweir //_________________________________________________________________________________________________________________________
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //_________________________________________________________________________________________________________________________
33cdf0e10cSrcweir //  other includes
34cdf0e10cSrcweir //_________________________________________________________________________________________________________________________
35cdf0e10cSrcweir #include <rtl/ustring.hxx>
36cdf0e10cSrcweir #include <sal/types.h>
37cdf0e10cSrcweir #include <osl/diagnose.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <stdio.h>
40cdf0e10cSrcweir #if defined _MSC_VER
41cdf0e10cSrcweir #pragma warning(push,1)
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <windows.h>
44cdf0e10cSrcweir #include <objbase.h>
45cdf0e10cSrcweir #if defined _MSC_VER
46cdf0e10cSrcweir #pragma warning(pop)
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <memory>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <process.h>
52cdf0e10cSrcweir #include "XTDo.hxx"
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //-------------------------------------------------------------
55cdf0e10cSrcweir // my defines
56cdf0e10cSrcweir //-------------------------------------------------------------
57cdf0e10cSrcweir 
58cdf0e10cSrcweir #define WRITE_CB
59cdf0e10cSrcweir #define EVT_MANUAL_RESET     TRUE
60cdf0e10cSrcweir #define EVT_INIT_NONSIGNALED FALSE
61cdf0e10cSrcweir #define EVT_NONAME           ""
62cdf0e10cSrcweir #define WAIT_MSGLOOP
63cdf0e10cSrcweir #define RAW_MARSHALING
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //------------------------------------------------------------
66*a8d869c0SJohn Bampton //  namespaces
67cdf0e10cSrcweir //------------------------------------------------------------
68cdf0e10cSrcweir 
69cdf0e10cSrcweir using namespace ::rtl;
70cdf0e10cSrcweir using namespace ::std;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir //------------------------------------------------------------
73cdf0e10cSrcweir //  globales
74cdf0e10cSrcweir //------------------------------------------------------------
75cdf0e10cSrcweir 
76cdf0e10cSrcweir HANDLE  g_hEvtThreadWakeup;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir #ifdef RAW_MARSHALING
79cdf0e10cSrcweir     HGLOBAL g_hGlob;
80cdf0e10cSrcweir #else
81cdf0e10cSrcweir     IStream* g_pStm;
82cdf0e10cSrcweir #endif
83cdf0e10cSrcweir 
84cdf0e10cSrcweir //################################################################
85cdf0e10cSrcweir // a thread in another apartment to test apartment transparency
86cdf0e10cSrcweir 
ThreadProc(LPVOID pParam)87cdf0e10cSrcweir unsigned int _stdcall ThreadProc(LPVOID pParam)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     // setup another apartment
90cdf0e10cSrcweir     HRESULT hr = OleInitialize( NULL );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     WaitForSingleObject( g_hEvtThreadWakeup, INFINITE );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     IDataObject* pIDo;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir #ifdef RAW_MARSHALING
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     IStream* pStm = NULL;
99cdf0e10cSrcweir     hr = CreateStreamOnHGlobal( g_hGlob, FALSE, &pStm );
100cdf0e10cSrcweir     if ( SUCCEEDED( hr ) )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         hr = CoUnmarshalInterface(
103cdf0e10cSrcweir                 pStm,
104cdf0e10cSrcweir                 __uuidof( IDataObject ),
105cdf0e10cSrcweir                 (void**)&pIDo );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         hr = pStm->Release( );
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir #else
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     hr = CoGetInterfaceAndReleaseStream(
113cdf0e10cSrcweir         g_pStm,
114cdf0e10cSrcweir         __uuidof( IDataObject ),
115cdf0e10cSrcweir         (void**)&pIDo
116cdf0e10cSrcweir         );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #endif
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     IEnumFORMATETC* pIEEtc;
121cdf0e10cSrcweir     hr = pIDo->EnumFormatEtc( DATADIR_GET, &pIEEtc );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     hr = OleIsCurrentClipboard( pIDo );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     hr = OleFlushClipboard( );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     OleUninitialize( );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     return 0;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir //################################################################
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //----------------------------------------------------------------
135cdf0e10cSrcweir //  main
136cdf0e10cSrcweir //----------------------------------------------------------------
137cdf0e10cSrcweir 
main(int nArgc,char * Argv[])138cdf0e10cSrcweir int SAL_CALL main( int nArgc, char* Argv[] )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     HRESULT hr = OleInitialize( NULL );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     g_hEvtThreadWakeup = CreateEvent( 0,
143cdf0e10cSrcweir                                       EVT_MANUAL_RESET,
144cdf0e10cSrcweir                                       EVT_INIT_NONSIGNALED,
145cdf0e10cSrcweir                                       EVT_NONAME );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     unsigned uThreadId;
148cdf0e10cSrcweir     HANDLE   hThread;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     // create a thread in another apartment
151cdf0e10cSrcweir     hThread = (void*)_beginthreadex( NULL, 0, ThreadProc, NULL, 0, &uThreadId );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     IDataObject* pIDo = new CXTDataObject( );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     hr = OleSetClipboard( pIDo );
156cdf0e10cSrcweir     hr = E_FAIL;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     hr = OleIsCurrentClipboard( pIDo );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     //hr = OleGetClipboard( &pIDo );
161cdf0e10cSrcweir     if ( SUCCEEDED( hr ) )
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir #ifdef RAW_MARSHALING
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         IStream* pStm = NULL;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         hr = CreateStreamOnHGlobal( 0, FALSE, &pStm );
168cdf0e10cSrcweir         if ( SUCCEEDED( hr ) )
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             hr = CoMarshalInterface(
171cdf0e10cSrcweir                 pStm,
172cdf0e10cSrcweir                 __uuidof( IDataObject ),
173cdf0e10cSrcweir                 pIDo,
174cdf0e10cSrcweir                 MSHCTX_INPROC,
175cdf0e10cSrcweir                 0,
176cdf0e10cSrcweir                 MSHLFLAGS_NORMAL );
177cdf0e10cSrcweir             if ( SUCCEEDED( hr ) )
178cdf0e10cSrcweir                 hr = GetHGlobalFromStream( pStm, &g_hGlob );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             hr = pStm->Release( );
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir #else
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         hr = CoMarshalInterThreadInterfaceInStream(
186cdf0e10cSrcweir                 __uuidof( IDataObject ),
187cdf0e10cSrcweir                 pIDo,
188cdf0e10cSrcweir                 &g_pStm );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir #endif
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         if ( SUCCEEDED( hr ) )
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             // wakeup the thread and waiting util it ends
195cdf0e10cSrcweir             SetEvent( g_hEvtThreadWakeup );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir #ifdef WAIT_MSGLOOP
198cdf0e10cSrcweir 
199cdf0e10cSrcweir             BOOL bContinue = TRUE;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             while( bContinue )
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir                 DWORD dwResult = WaitForMultipleObjects(
204cdf0e10cSrcweir                     1,
205cdf0e10cSrcweir                     &hThread,
206cdf0e10cSrcweir                     TRUE,
207cdf0e10cSrcweir                     0 );
208cdf0e10cSrcweir 
209cdf0e10cSrcweir                 if ( WAIT_OBJECT_0 == dwResult )
210cdf0e10cSrcweir                 {
211cdf0e10cSrcweir                     bContinue = FALSE;
212cdf0e10cSrcweir                 }
213cdf0e10cSrcweir                 else
214cdf0e10cSrcweir                 {
215cdf0e10cSrcweir                     MSG msg;
216cdf0e10cSrcweir                     while( PeekMessage(
217cdf0e10cSrcweir                             &msg,
218cdf0e10cSrcweir                             NULL,
219cdf0e10cSrcweir                             0,
220cdf0e10cSrcweir                             0,
221cdf0e10cSrcweir                             PM_REMOVE ) )
222cdf0e10cSrcweir                     {
223cdf0e10cSrcweir                         TranslateMessage(&msg);
224cdf0e10cSrcweir                         DispatchMessage(&msg);
225cdf0e10cSrcweir                     }
226cdf0e10cSrcweir                 }
227cdf0e10cSrcweir             } // while
228cdf0e10cSrcweir 
229cdf0e10cSrcweir #endif
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         } // if
232cdf0e10cSrcweir     } // if
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     OleFlushClipboard( );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     OleUninitialize( );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     return 0;
239cdf0e10cSrcweir }
240