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