1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #pragma warning(disable: 4917)
28cdf0e10cSrcweir #include <windows.h>
29cdf0e10cSrcweir #include <comdef.h>
30cdf0e10cSrcweir #include <tchar.h>
31cdf0e10cSrcweir #include <atlbase.h>
32cdf0e10cSrcweir #include<atlcom.h>
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir #include <com/sun/star/bridge/ModelDependent.hpp>
35cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeSupplier2.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37cdf0e10cSrcweir #include <com/sun/star/script/XInvocation.hpp>
38cdf0e10cSrcweir #include <oletest/XCallback.hpp>
39cdf0e10cSrcweir #include <rtl/process.h>
40cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
41cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
42cdf0e10cSrcweir #include <rtl/string.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace com::sun::star::lang;
47cdf0e10cSrcweir using namespace com::sun::star::uno;
48cdf0e10cSrcweir using namespace com::sun::star::script;
49cdf0e10cSrcweir using namespace com::sun::star::bridge;
50cdf0e10cSrcweir using namespace com::sun::star::bridge::ModelDependent;
51cdf0e10cSrcweir using namespace cppu;
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir HRESULT doTest();
54cdf0e10cSrcweir bool incrementMultidimensionalIndex(
55cdf0e10cSrcweir     sal_Int32 dimensions,
56cdf0e10cSrcweir     const sal_Int32 * parDimensionLengths,
57cdf0e10cSrcweir     sal_Int32 * parMultidimensionalIndex);
58cdf0e10cSrcweir 
_tmain(int,_TCHAR *)59cdf0e10cSrcweir int __cdecl _tmain( int /*argc*/, _TCHAR * /*argv[]*/ )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	HRESULT hr;
62cdf0e10cSrcweir 	if( FAILED( hr=CoInitialize(NULL)))
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		_tprintf(_T("CoInitialize failed \n"));
65cdf0e10cSrcweir 		return -1;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	if( FAILED(hr=doTest()))
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		_com_error err( hr);
72cdf0e10cSrcweir 		const TCHAR * errMsg= err.ErrorMessage();
73cdf0e10cSrcweir 		MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
74cdf0e10cSrcweir 	}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	CoUninitialize();
77cdf0e10cSrcweir 	return 0;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
doTest()83cdf0e10cSrcweir HRESULT doTest()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	HRESULT hr= S_OK;
86cdf0e10cSrcweir 	long j = 0;
87cdf0e10cSrcweir 	SAFEARRAY* par;
88cdf0e10cSrcweir 	CComDispatchDriver disp;
89cdf0e10cSrcweir 	CComVariant result;
90cdf0e10cSrcweir 	CComVariant param1;
91cdf0e10cSrcweir 	CComPtr<IUnknown> spUnkFactory;
92cdf0e10cSrcweir 	if( SUCCEEDED( spUnkFactory.CoCreateInstance(L"com.sun.star.ServiceManager")))
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir 		disp= spUnkFactory;
95cdf0e10cSrcweir 		param1= L"oletest.OleTest";
96cdf0e10cSrcweir 		disp.Invoke1( L"createInstance", &param1, &result);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 		disp= result.pdispVal;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		// disp contains now oletest.OleTest
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		// one dimensional array
103cdf0e10cSrcweir 		par= SafeArrayCreateVector( VT_UI1, 0, 5);
104cdf0e10cSrcweir 		unsigned char arbyte[]= { 1,2,3,4,5};
105cdf0e10cSrcweir 		for(long i= 0; i < 5;i++)
106cdf0e10cSrcweir 			hr=	SafeArrayPutElement( par, &i, &arbyte[i]);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		result.Clear();
109cdf0e10cSrcweir 		param1.vt= VT_ARRAY| VT_UI1;
110cdf0e10cSrcweir 		param1.byref= par;
111cdf0e10cSrcweir 		disp.Invoke1(L"methodByte", &param1, &result);
112cdf0e10cSrcweir 		SafeArrayDestroy( par);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 		// two dimensional array
116cdf0e10cSrcweir 		SAFEARRAYBOUND bounds[2];
117cdf0e10cSrcweir 		// least significant dimension first, Dimension 1
118cdf0e10cSrcweir 		bounds[0].cElements= 3;
119cdf0e10cSrcweir 		bounds[0].lLbound= 0;
120cdf0e10cSrcweir 		// Dimension 2
121cdf0e10cSrcweir 		bounds[1].cElements= 2;
122cdf0e10cSrcweir 		bounds[1].lLbound= 0;
123cdf0e10cSrcweir 		par= SafeArrayCreate( VT_I4, 2, bounds );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		long uBound1;
126cdf0e10cSrcweir 		long uBound2;
127cdf0e10cSrcweir 		hr= SafeArrayGetUBound( par, 1, &uBound1);
128cdf0e10cSrcweir 		hr= SafeArrayGetUBound( par, 2, &uBound2);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		long index2[2];
131cdf0e10cSrcweir 		memset( index2, 0, 2 * sizeof( long) );
132cdf0e10cSrcweir 		long dimLengths[]={3,2};
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 		long data;
135cdf0e10cSrcweir 		do
136cdf0e10cSrcweir 		{
137cdf0e10cSrcweir 			data= index2[1] * 3 + index2[0] +1;
138cdf0e10cSrcweir 			hr= SafeArrayPutElement( par, index2, &data);
139cdf0e10cSrcweir 		}while( incrementMultidimensionalIndex( 2, dimLengths, index2) );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		long* pdata;
142cdf0e10cSrcweir 		long (*dataL)[2][3];
143cdf0e10cSrcweir 		hr= SafeArrayAccessData( par, (void**)&pdata);
144cdf0e10cSrcweir 		dataL= (long(*)[2][3])pdata;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		for (long i= 0; i < 2; i ++)
147cdf0e10cSrcweir 		{
148cdf0e10cSrcweir 			for(long j= 0; j < 3; j++)
149cdf0e10cSrcweir 				data= (*dataL)[i][j];
150cdf0e10cSrcweir 		}
151cdf0e10cSrcweir 		hr= SafeArrayUnaccessData(par);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		result.Clear();
154cdf0e10cSrcweir 		param1.vt= VT_ARRAY | VT_I4;
155cdf0e10cSrcweir 		param1.byref= par;
156cdf0e10cSrcweir 		disp.Invoke1(L"methodSequence", &param1, &result);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		SAFEARRAY* arRet= result.parray;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		for(long i= 0; i < 2 ; i++)
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			CComVariant varx;
163cdf0e10cSrcweir 			varx.Clear();
164cdf0e10cSrcweir 			hr= SafeArrayGetElement( arRet, &i, &varx);
165cdf0e10cSrcweir 			SAFEARRAY* ari= varx.parray;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 			for( j= 0; j < 3; j++)
168cdf0e10cSrcweir 			{
169cdf0e10cSrcweir 				CComVariant varj;
170cdf0e10cSrcweir 				varj.Clear();
171cdf0e10cSrcweir 				hr= SafeArrayGetElement( ari, &j, &varj);
172cdf0e10cSrcweir 			}
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 		}
177cdf0e10cSrcweir 		SafeArrayDestroy( par);
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	return hr;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir // left index is least significant
incrementMultidimensionalIndex(sal_Int32 dimensions,const sal_Int32 * parDimensionLengths,sal_Int32 * parMultidimensionalIndex)184cdf0e10cSrcweir bool incrementMultidimensionalIndex(
185cdf0e10cSrcweir     sal_Int32 dimensions,
186cdf0e10cSrcweir     const sal_Int32 * parDimensionLengths,
187cdf0e10cSrcweir 	sal_Int32 * parMultidimensionalIndex)
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	if( dimensions < 1)
190cdf0e10cSrcweir 		return sal_False;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	bool ret= sal_True;
193cdf0e10cSrcweir 	bool carry= sal_True; // to get into the while loop
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	sal_Int32 currentDimension= 0; //most significant is 1
196cdf0e10cSrcweir 	while( carry)
197cdf0e10cSrcweir 	{
198cdf0e10cSrcweir 		parMultidimensionalIndex[ currentDimension ]++;
199cdf0e10cSrcweir 		// if carryover, set index to 0 and handle carry on a level above
200cdf0e10cSrcweir 		if( parMultidimensionalIndex[ currentDimension] > (parDimensionLengths[ currentDimension] - 1))
201cdf0e10cSrcweir 			parMultidimensionalIndex[ currentDimension]= 0;
202cdf0e10cSrcweir 		else
203cdf0e10cSrcweir 			carry= sal_False;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		currentDimension ++;
206cdf0e10cSrcweir 		// if dimensions drops below 1 and carry is set than then all indices are 0 again
207cdf0e10cSrcweir 		// this is signalled by returning sal_False
208cdf0e10cSrcweir 		if( currentDimension > dimensions - 1 && carry)
209cdf0e10cSrcweir 		{
210cdf0e10cSrcweir 			carry= sal_False;
211cdf0e10cSrcweir 			ret= sal_False;
212cdf0e10cSrcweir 		}
213cdf0e10cSrcweir 	}
214cdf0e10cSrcweir 	return ret;
215cdf0e10cSrcweir }
216