1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #pragma warning(disable: 4917)
32*cdf0e10cSrcweir #include <windows.h>
33*cdf0e10cSrcweir #include <comdef.h>
34*cdf0e10cSrcweir #include <tchar.h>
35*cdf0e10cSrcweir #include <atlbase.h>
36*cdf0e10cSrcweir #include<atlcom.h>
37*cdf0e10cSrcweir #include <stdio.h>
38*cdf0e10cSrcweir #include <com/sun/star/bridge/ModelDependent.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeSupplier2.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/script/XInvocation.hpp>
42*cdf0e10cSrcweir #include <oletest/XCallback.hpp>
43*cdf0e10cSrcweir #include <rtl/process.h>
44*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
45*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
46*cdf0e10cSrcweir #include <rtl/string.h>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace com::sun::star::lang;
51*cdf0e10cSrcweir using namespace com::sun::star::uno;
52*cdf0e10cSrcweir using namespace com::sun::star::script;
53*cdf0e10cSrcweir using namespace com::sun::star::bridge;
54*cdf0e10cSrcweir using namespace com::sun::star::bridge::ModelDependent;
55*cdf0e10cSrcweir using namespace cppu;
56*cdf0e10cSrcweir using namespace rtl;
57*cdf0e10cSrcweir HRESULT doTest();
58*cdf0e10cSrcweir bool incrementMultidimensionalIndex(
59*cdf0e10cSrcweir     sal_Int32 dimensions,
60*cdf0e10cSrcweir     const sal_Int32 * parDimensionLengths,
61*cdf0e10cSrcweir     sal_Int32 * parMultidimensionalIndex);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir int __cdecl _tmain( int /*argc*/, _TCHAR * /*argv[]*/ )
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir 	HRESULT hr;
66*cdf0e10cSrcweir 	if( FAILED( hr=CoInitialize(NULL)))
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir 		_tprintf(_T("CoInitialize failed \n"));
69*cdf0e10cSrcweir 		return -1;
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	if( FAILED(hr=doTest()))
74*cdf0e10cSrcweir 	{
75*cdf0e10cSrcweir 		_com_error err( hr);
76*cdf0e10cSrcweir 		const TCHAR * errMsg= err.ErrorMessage();
77*cdf0e10cSrcweir 		MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
78*cdf0e10cSrcweir 	}
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	CoUninitialize();
81*cdf0e10cSrcweir 	return 0;
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir HRESULT doTest()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	HRESULT hr= S_OK;
90*cdf0e10cSrcweir 	long j = 0;
91*cdf0e10cSrcweir 	SAFEARRAY* par;
92*cdf0e10cSrcweir 	CComDispatchDriver disp;
93*cdf0e10cSrcweir 	CComVariant result;
94*cdf0e10cSrcweir 	CComVariant param1;
95*cdf0e10cSrcweir 	CComPtr<IUnknown> spUnkFactory;
96*cdf0e10cSrcweir 	if( SUCCEEDED( spUnkFactory.CoCreateInstance(L"com.sun.star.ServiceManager")))
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		disp= spUnkFactory;
99*cdf0e10cSrcweir 		param1= L"oletest.OleTest";
100*cdf0e10cSrcweir 		disp.Invoke1( L"createInstance", &param1, &result);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 		disp= result.pdispVal;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 		// disp contains now oletest.OleTest
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 		// one dimensional array
107*cdf0e10cSrcweir 		par= SafeArrayCreateVector( VT_UI1, 0, 5);
108*cdf0e10cSrcweir 		unsigned char arbyte[]= { 1,2,3,4,5};
109*cdf0e10cSrcweir 		for(long i= 0; i < 5;i++)
110*cdf0e10cSrcweir 			hr=	SafeArrayPutElement( par, &i, &arbyte[i]);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		result.Clear();
113*cdf0e10cSrcweir 		param1.vt= VT_ARRAY| VT_UI1;
114*cdf0e10cSrcweir 		param1.byref= par;
115*cdf0e10cSrcweir 		disp.Invoke1(L"methodByte", &param1, &result);
116*cdf0e10cSrcweir 		SafeArrayDestroy( par);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		// two dimensional array
120*cdf0e10cSrcweir 		SAFEARRAYBOUND bounds[2];
121*cdf0e10cSrcweir 		// least significant dimension first, Dimension 1
122*cdf0e10cSrcweir 		bounds[0].cElements= 3;
123*cdf0e10cSrcweir 		bounds[0].lLbound= 0;
124*cdf0e10cSrcweir 		// Dimension 2
125*cdf0e10cSrcweir 		bounds[1].cElements= 2;
126*cdf0e10cSrcweir 		bounds[1].lLbound= 0;
127*cdf0e10cSrcweir 		par= SafeArrayCreate( VT_I4, 2, bounds );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 		long uBound1;
130*cdf0e10cSrcweir 		long uBound2;
131*cdf0e10cSrcweir 		hr= SafeArrayGetUBound( par, 1, &uBound1);
132*cdf0e10cSrcweir 		hr= SafeArrayGetUBound( par, 2, &uBound2);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		long index2[2];
135*cdf0e10cSrcweir 		memset( index2, 0, 2 * sizeof( long) );
136*cdf0e10cSrcweir 		long dimLengths[]={3,2};
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 		long data;
139*cdf0e10cSrcweir 		do
140*cdf0e10cSrcweir 		{
141*cdf0e10cSrcweir 			data= index2[1] * 3 + index2[0] +1;
142*cdf0e10cSrcweir 			hr= SafeArrayPutElement( par, index2, &data);
143*cdf0e10cSrcweir 		}while( incrementMultidimensionalIndex( 2, dimLengths, index2) );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 		long* pdata;
146*cdf0e10cSrcweir 		long (*dataL)[2][3];
147*cdf0e10cSrcweir 		hr= SafeArrayAccessData( par, (void**)&pdata);
148*cdf0e10cSrcweir 		dataL= (long(*)[2][3])pdata;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		for (long i= 0; i < 2; i ++)
151*cdf0e10cSrcweir 		{
152*cdf0e10cSrcweir 			for(long j= 0; j < 3; j++)
153*cdf0e10cSrcweir 				data= (*dataL)[i][j];
154*cdf0e10cSrcweir 		}
155*cdf0e10cSrcweir 		hr= SafeArrayUnaccessData(par);
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 		result.Clear();
158*cdf0e10cSrcweir 		param1.vt= VT_ARRAY | VT_I4;
159*cdf0e10cSrcweir 		param1.byref= par;
160*cdf0e10cSrcweir 		disp.Invoke1(L"methodSequence", &param1, &result);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 		SAFEARRAY* arRet= result.parray;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 		for(long i= 0; i < 2 ; i++)
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			CComVariant varx;
167*cdf0e10cSrcweir 			varx.Clear();
168*cdf0e10cSrcweir 			hr= SafeArrayGetElement( arRet, &i, &varx);
169*cdf0e10cSrcweir 			SAFEARRAY* ari= varx.parray;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 			for( j= 0; j < 3; j++)
172*cdf0e10cSrcweir 			{
173*cdf0e10cSrcweir 				CComVariant varj;
174*cdf0e10cSrcweir 				varj.Clear();
175*cdf0e10cSrcweir 				hr= SafeArrayGetElement( ari, &j, &varj);
176*cdf0e10cSrcweir 			}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 		}
181*cdf0e10cSrcweir 		SafeArrayDestroy( par);
182*cdf0e10cSrcweir 	}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	return hr;
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir // left index is least significant
188*cdf0e10cSrcweir bool incrementMultidimensionalIndex(
189*cdf0e10cSrcweir     sal_Int32 dimensions,
190*cdf0e10cSrcweir     const sal_Int32 * parDimensionLengths,
191*cdf0e10cSrcweir 	sal_Int32 * parMultidimensionalIndex)
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir 	if( dimensions < 1)
194*cdf0e10cSrcweir 		return sal_False;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	bool ret= sal_True;
197*cdf0e10cSrcweir 	bool carry= sal_True; // to get into the while loop
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	sal_Int32 currentDimension= 0; //most significant is 1
200*cdf0e10cSrcweir 	while( carry)
201*cdf0e10cSrcweir 	{
202*cdf0e10cSrcweir 		parMultidimensionalIndex[ currentDimension ]++;
203*cdf0e10cSrcweir 		// if carryover, set index to 0 and handle carry on a level above
204*cdf0e10cSrcweir 		if( parMultidimensionalIndex[ currentDimension] > (parDimensionLengths[ currentDimension] - 1))
205*cdf0e10cSrcweir 			parMultidimensionalIndex[ currentDimension]= 0;
206*cdf0e10cSrcweir 		else
207*cdf0e10cSrcweir 			carry= sal_False;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 		currentDimension ++;
210*cdf0e10cSrcweir 		// if dimensions drops below 1 and carry is set than then all indices are 0 again
211*cdf0e10cSrcweir 		// this is signalled by returning sal_False
212*cdf0e10cSrcweir 		if( currentDimension > dimensions - 1 && carry)
213*cdf0e10cSrcweir 		{
214*cdf0e10cSrcweir 			carry= sal_False;
215*cdf0e10cSrcweir 			ret= sal_False;
216*cdf0e10cSrcweir 		}
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 	return ret;
219*cdf0e10cSrcweir }
220