summaryrefslogtreecommitdiff
path: root/extensions/source/ole/unoobjw.cxx
blob: 6eff7843a0d9efa05213896b58d8a32df0d7bf74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
/*************************************************************************
 *
 *  $RCSfile: unoobjw.cxx,v $
 *
 *  $Revision: 1.15 $
 *
 *  last change: $Author: jl $ $Date: 2002-09-17 11:00:32 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include "ole2uno.hxx"

#include <tools/presys.h>
#include <olectl.h>
#include <vector>
#include <list>
#include <hash_map>
#include "comifaces.hxx"
#include <tools/postsys.h>


#include <vos/diagnose.hxx>
#include <vos/refernce.hxx>
#include <tools/debug.hxx>

#ifndef _RTL_USTRING_
#include <rtl/ustring>
#endif

#ifndef _COM_SUN_STAR_BEANS_METHODCONCEPT_HPP_
#include <com/sun/star/beans/MethodConcept.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_PROPERTYCONCEPT_HPP_
#include <com/sun/star/beans/PropertyConcept.hpp>
#endif
#ifndef _COM_SUN_STAR_SCRIPT_FAILREASON_HPP_
#include <com/sun/star/script/FailReason.hpp>
#endif
#ifndef _COM_SUN_STAR_REFLECTION_PARAMINFO_HPP_
#include <com/sun/star/reflection/ParamInfo.hpp>
#endif

#ifndef _COM_SUN_STAR_BEANS_XEXACTNAME_HPP_
#include <com/sun/star/beans/XExactName.hpp>
#endif
#ifndef _COM_SUN_STAR_CONTAINER_NOSUCHELEMENTEXCEPTION_HPP_
#include <com/sun/star/container/NoSuchElementException.hpp>
#endif

#include <com/sun/star/beans/XMaterialHolder.hpp>
#include <com/sun/star/script/XInvocation2.hpp>
#include <com/sun/star/script/MemberType.hpp>
#include <com/sun/star/reflection/XIdlReflection.hpp>

#ifndef _TYPELIB_TYPEDESCRIPTION_H_
#include <uno/typedescription.h>
#endif
#ifndef _OSL_INTERLOCK_H_
#include <osl/interlck.h>
#endif
#ifndef _COM_SUN_STAR_UNO_GENFUNC_H_
#include <com/sun/star/uno/genfunc.h>
#endif
#ifndef _CPPUHELPER_IMPLBASE1_HXX_
#include <cppuhelper/implbase1.hxx>
#endif

#include "comifaces.hxx"
#include "jscriptclasses.hxx"
#include "oleobjw.hxx"
#include "unoobjw.hxx"
#include "servprov.hxx"

using namespace vos;
using namespace std;
using namespace rtl;
using namespace osl;
using namespace cppu;
using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
using namespace com::sun::star::container;
using namespace com::sun::star::script;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge::ModelDependent;
using namespace com::sun::star::reflection;



#if _MSC_VER < 1200
extern "C" const GUID IID_IDispatchEx;
#endif

namespace ole_adapter
{
hash_map<sal_uInt32, WeakReference<XInterface> > UnoObjToWrapperMap;
static sal_Bool writeBackOutParameter(VARIANTARG* pDest, VARIANT* pSource);
static sal_Bool writeBackOutParameter2( VARIANTARG* pDest, VARIANT* pSource);
static HRESULT mapCannotConvertException( CannotConvertException e, unsigned int * puArgErr);

/*****************************************************************************

    class implementation: InterfaceOleWrapper_Impl

*****************************************************************************/
InterfaceOleWrapper_Impl::InterfaceOleWrapper_Impl( Reference<XMultiServiceFactory>& xFactory,
                                                    sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass):
        m_defaultValueType( 0),
        UnoConversionUtilities<InterfaceOleWrapper_Impl>( xFactory, unoWrapperClass, comWrapperClass)
{
}

InterfaceOleWrapper_Impl::~InterfaceOleWrapper_Impl()
{
    MutexGuard guard(getBridgeMutex());
    // remove entries in global map
    IT_Uno it= UnoObjToWrapperMap.find( (sal_uInt32) m_xOrigin.get());
    if(it != UnoObjToWrapperMap.end())
        UnoObjToWrapperMap.erase(it);
}

STDMETHODIMP InterfaceOleWrapper_Impl::QueryInterface(REFIID riid, LPVOID FAR * ppv)
{
    HRESULT ret= S_OK;

    if( !ppv)
        return E_POINTER;

    if(IsEqualIID(riid, IID_IUnknown))
    {
        AddRef();
        *ppv = (IUnknown*) (IDispatch*) this;
    }
    else if (IsEqualIID(riid, IID_IDispatch))
    {
        AddRef();
        *ppv = (IDispatch*) this;
    }
    else if( IsEqualIID( riid, __uuidof( IUnoObjectWrapper)))
    {
        AddRef();
        *ppv= (IUnoObjectWrapper*) this;
    }
    else
        ret= E_NOINTERFACE;
    return ret;
}

STDMETHODIMP_(ULONG) InterfaceOleWrapper_Impl::AddRef()
{
    acquire();
    // does not need to guard because one should not rely on the return value of
    // AddRef anyway
    return m_refCount;
}

STDMETHODIMP_(ULONG) InterfaceOleWrapper_Impl::Release()
{
    ULONG n= m_refCount;
    release();
    return n - 1;
}

// IUnoObjectWrapper --------------------------------------------------------
STDMETHODIMP InterfaceOleWrapper_Impl::getWrapperXInterface( Reference<XInterface>* pXInt)
{
    *pXInt= Reference<XInterface>( static_cast<XWeak*>( this), UNO_QUERY);
    return pXInt->is() ? S_OK : E_FAIL;
}
STDMETHODIMP InterfaceOleWrapper_Impl::getOriginalUnoObject( Reference<XInterface>* pXInt)
{
    *pXInt= m_xOrigin;
    return m_xOrigin.is() ? S_OK : E_FAIL;
}
STDMETHODIMP  InterfaceOleWrapper_Impl::getOriginalUnoStruct( Any * pStruct)
{
    HRESULT ret= E_FAIL;
    if( !m_xOrigin.is())
    {
        Reference<XMaterialHolder> xMatHolder( m_xInvocation, UNO_QUERY);
        if( xMatHolder.is())
        {
            Any any = xMatHolder->getMaterial();
            if( any.getValueTypeClass() == TypeClass_STRUCT)
            {
                *pStruct= any;
                ret= S_OK;
            }
        }
    }
    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetTypeInfoCount( unsigned int * pctinfo )
{
    return E_NOTIMPL ;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo ** pptinfo)
{
    return E_NOTIMPL;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetIDsOfNames(REFIID riid,
                                                     OLECHAR ** rgszNames,
                                                     unsigned int cNames,
                                                     LCID lcid,
                                                     DISPID * rgdispid )
{

    MutexGuard guard( getBridgeMutex());
    if( ! rgdispid)
        return E_POINTER;
    HRESULT ret = E_UNEXPECTED;
    // ----------------------------------------
    if( ! wcscmp( *rgszNames, JSCRIPT_VALUE_FUNC) ||
        ! wcscmp( *rgszNames, BRIDGE_VALUE_FUNC))
    {
        *rgdispid= DISPID_JSCRIPT_VALUE_FUNC;
        return S_OK;
    }
    else if( ! wcscmp( *rgszNames, GET_STRUCT_FUNC) ||
             ! wcscmp( *rgszNames, BRIDGE_GET_STRUCT_FUNC))
    {
        *rgdispid= DISPID_GET_STRUCT_FUNC;
        return S_OK;
    }
    // ----------------------------------------
    if (m_xInvocation.is() && (cNames > 0))
    {
        OUString name(rgszNames[0]);
        NameToIdMap::iterator iter = m_nameToDispIdMap.find(name);

        if (iter == m_nameToDispIdMap.end())
        {
            OUString exactName;

            if (m_xExactName.is())
            {
                exactName = m_xExactName->getExactName(name);
            }
            else
            {
                exactName = name;
            }

            MemberInfo d(0, exactName);

            if (m_xInvocation->hasProperty(exactName))
            {
                d.flags |= DISPATCH_PROPERTYGET;
                d.flags |= DISPATCH_PROPERTYPUT;
                d.flags |= DISPATCH_PROPERTYPUTREF;
            }

            if (m_xInvocation->hasMethod(exactName))
            {
                d.flags |= DISPATCH_METHOD;
            }

            if (d.flags != 0)
            {
                m_MemberInfos.push_back(d);
                iter = m_nameToDispIdMap.insert(NameToIdMap::value_type(exactName, (DISPID)m_MemberInfos.size())).first;

                if (exactName != name)
                {
                    iter = m_nameToDispIdMap.insert(NameToIdMap::value_type(name, (DISPID)m_MemberInfos.size())).first;
                }
            }
        }

        if (iter == m_nameToDispIdMap.end())
        {
            ret = ResultFromScode(DISP_E_UNKNOWNNAME);
        }
        else
        {
            *rgdispid = (*iter).second;
            ret = ResultFromScode(S_OK);
        }
    }

    return ret;
}

// "convertDispparamsArgs" converts VARIANTS to their respecting Any counterparts
// The parameters "id", "wFlags" and "pdispparams" equal those as used in
// IDispatch::Invoke. The function handles special JavaScript
// cases where a VARIANT of type VT_DISPATCH is ambiguous and could represent
// an object, array ( JavaScript Array object), out parameter and in/out ( JavaScript Array object)
//  parameter (JavaScript Array object)
// parameter. Because all those VT_DISPATCH objects need a different conversion
// we have to find out what the object is supposed to be. The function does this
// by either using type information or by help of a specialized ValueObject object.

// A. Type Information
// -----------------------------------------------------------------------------
// With the help of type information the kind of parameter can be exactly determined
// and an appropriate conversion can be choosen. A problem arises if a method expects
// an Any. Then the type info does not tell what the type of the value, that is kept
// by the any, should be. In this situation the decision wheter the param is a
// sequence or an object is made upon the fact if the object has a property "0"
// ( see function "isJScriptArray"). Since this is unsafe it is recommended to use
// the JScript value objects within a JScript script on such an occasion.

// B. JavaScript Value Object ( class JScriptValue )
// -----------------------------------------------------------------------------
// A JScriptValue (ValueObject) object is a COM object in that it implements IDispatch and the
// IJScriptValue object interface. Such objects are provided by all UNO wrapper
// objects used within a JScript script. To obtain an instance one has to call
// "_GetValueObject() or Bridge_GetValueObject()" on an UNO wrapper object (class InterfaceOleWrapper_Impl).
// A value object is appropriately initialized within the script and passed as
// parameter to an UNO object method or property. The convertDispparamsArgs function
// can easily find out that a param is such an object by queriing for the
// IJScriptValue interface. By this interface one the type and kind ( out, in/out)
// can be determined and the right conversion can be applied.
// Using ValueObjects we spare us the effort of aquiring and examining type information
// in order to figure out what the an IDispatch parameter is meant for.

// Normal JScript object parameter can be mixed with JScriptValue object. If an
// VARIANT contains an VT_DISPATCH that is no JScriptValue than the type information
// is used to find out about the reqired type.

// out Parameter in Visual Basic have the type VT_BYREF | VT_VARIANT and if the bridge
// is used from C++ than the type is usually VT_BYREF |VT_xxx. Those parameter are converted
// and we don't go to the trouble of aquiring type information.
HRESULT InterfaceOleWrapper_Impl::convertDispparamsArgs(  DISPID id, unsigned short wFlags, DISPPARAMS* pdispparams, Sequence<Any>& rSeq)
{
    HRESULT hr= S_OK;
    sal_Int32 countArgs= pdispparams->cArgs;
    if( countArgs == 0)
        return S_OK;
    sal_Bool convOk = sal_True;


    // The sequence will hold information whether a param at a certain index has already been converted.
    Sequence< sal_Bool > seqConvertedParams( countArgs);
    rSeq.realloc( countArgs);
    Any*    pParams = rSeq.getArray();

    sal_Bool bTypeInfoNeeded= sal_False;
    Any anyParam;
    sal_Bool bHandled= sal_False;

    // Iterate over all parameters. The aim is to find out if we need to obtain
    // type information for the parameter, which is the case when we receive an
    // VT_DISPATCH or VT_ARRAY argument. Also, ValuObjects are converted in this loop.
    // An VT_DISPATCH can be a ValueObject (class JScriptValue) objects that originated in this bridge.
    // The index in the sequence "seqConvertedParams" matches the index in the
    // DISPPARAMS.rgvarg array. If the value is true then the corresponding VARIANT has been converted
    // and the any has been written to the Sequence that holds the converted values.
    for( int i= 0; i < countArgs; i++)
    {
        if( sal_False == convertValueObject( &pdispparams->rgvarg[i], anyParam, bHandled) )
            return DISP_E_BADVARTYPE;
        if( bHandled)
        {// a param is a ValueObject and could be converted
            pParams[countArgs - i - 1]= anyParam;
            seqConvertedParams[i]= sal_True;
            continue;
        }
        else
        {
            // if the param is no ValueObject although it is an IDispatch then we must
            // set a flag that indicates that we have to examine those objects further.
            // This is also true if the argument is an array
            CComVariant var;
            if( SUCCEEDED( var.ChangeType(VT_DISPATCH, &pdispparams->rgvarg[i]))
                || (pdispparams->rgvarg[i].vt & VT_ARRAY)
                ||  ( pdispparams->rgvarg[i].vt == (VT_VARIANT | VT_BYREF) &&
                      pdispparams->rgvarg[i].pvarVal->vt & VT_ARRAY))
            {
                bTypeInfoNeeded= sal_True;
            }
        }
    }
    try{
        // If there is a VT_DISPATCH  and if it is no JScriptValue object then
        // we need to know if it represents a Sequence, out or in/out parameter
        // or an actual object. Therefore we obtain type information.
        InvocationInfo info;
        sal_Bool bTypesAvailable= sal_False;
        if(  bTypeInfoNeeded)
        {
            if( ! (bTypesAvailable= getInvocationInfoForCall( id, info)))
                return DISP_E_EXCEPTION;
        }
        // Used within the following "for" loop. If the param is an out, in/out parameter in
        // JScript (Array object, with value at index 0) then we
        // extract Array[0] and put the value into varParam. At the end of the loop varParam
        // is converted if it contains a value otherwise the VARIANT from
        // DISPPARAMS is converted.
        CComVariant varParam;

        // In this loop all params are converted except for those which are already converted
        // by convertValueObject ( see above ). The Sequence seqConvertedParams contains information
        // about which params are already converted (convertValueObject). A value in seqConvertedParams
        // at a particular index relates to the VARIANTARGS param at the same index in DISPPARAMS.
        for (int i = 0; convOk && (i < countArgs); i++)
        {
            if( seqConvertedParams[i] == sal_True)
                continue;
            sal_Bool bDoConversion= TRUE;
            varParam.Clear();
            // Check for JScript out and in/out paramsobjects (VT_DISPATCH).
            // To find them out we use typeinformation of the function being called.
            if( pdispparams->rgvarg[i].vt == VT_DISPATCH )
            {
                if( info.eMemberType == MemberType_METHOD && info.aParamModes[ countArgs - i -1 ]  == ParamMode_INOUT)
                {
                    // INOUT-param
                    // Index ( property) "0" contains the actual IN-param. The object is a JScript
                    // Array object.
                    // Get the IN-param at index "0"
                    IDispatch* pdisp= pdispparams->rgvarg[i].pdispVal;

                    OLECHAR* sindex= L"0";
                    DISPID id;
                    DISPPARAMS noParams= {0,0,0,0};
                    if(SUCCEEDED( hr= pdisp->GetIDsOfNames( IID_NULL, &sindex, 1, LOCALE_USER_DEFAULT, &id)))
                        hr= pdisp->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
                            &noParams, &varParam, NULL, NULL);
                    if( FAILED( hr))
                    {
                        varParam.Clear();
                        convOk= sal_False; break;
                        hr= DISP_E_EXCEPTION;
                        break;
                    }
                }
                else if( info.eMemberType == MemberType_METHOD && info.aParamModes[ countArgs - i -1 ] == ParamMode_OUT)
                {
                    // No conversion necessary. The VT_DISPATCH represents
                    // an JScript Array in which the outparam is written on index 0
                    // or it is VT_DISPATCH | VT_BYREF, out param in C
                    bDoConversion= sal_False;
                }
            }
            if( bDoConversion)
            {
                if( varParam.vt == VT_EMPTY) // then it was no in/out parameter
                    varParam= pdispparams->rgvarg[i];

                Any theParam;
                if( bTypesAvailable && info.eMemberType == MemberType_METHOD)
                    convOk= variantToAny2( & varParam,theParam,
                            info.aParamTypes[ countArgs - i - 1]);
                else if( bTypesAvailable && info.eMemberType == MemberType_PROPERTY)
                    convOk= variantToAny2( & varParam,theParam, info.aType);
                else
                    convOk = variantToAny( & varParam, theParam);

                if( convOk)
                    pParams[countArgs - (i + 1)]= theParam;
            }
        }// end for / iterating over all parameters
    }catch( IllegalArgumentException ) // XInvocation2::getInfoForName
    {
        hr= DISP_E_BADVARTYPE;
    }
    catch(...)
    {
        hr= DISP_E_EXCEPTION;
    }
    if( SUCCEEDED( hr) && convOk == sal_False)
        hr= DISP_E_EXCEPTION;
    return hr;
}

sal_Bool  InterfaceOleWrapper_Impl::getInvocationInfoForCall( DISPID id, InvocationInfo& info)
{
    sal_Bool bTypesAvailable= sal_False;

    if( !m_xInvocation.is() )return false;
    Reference<XInvocation2> inv2( m_xInvocation, UNO_QUERY);
    if( inv2.is())
    {
        // We need the name of the property or method to get its type information.
        // The name can be identified through the param "id"
        // that is kept as value in the map m_nameToDispIdMap.
        // Proplem: the Windows JScript engine sometimes changes small letters to capital
        // letters as happens in xidlclass_obj.createObject( var) // in JScript.
        // IDispatch::GetIdsOfNames is then called with "CreateObject" !!!
        // m_nameToDispIdMap can contain several names for one DISPID but only one is
        // the exact one. If there's no m_xExactName and therefore no exact name then
        // there's only one entry in the map.
        typedef NameToIdMap::const_iterator cit;
        OUString sMemberName;

        for(cit ci1= m_nameToDispIdMap.begin(); ci1 != m_nameToDispIdMap.end(); ci1++)
        {
            if( (*ci1).second == id) // iterator is a pair< OUString, DISPID>
            {
                sMemberName= (*ci1).first;
                break;
            }
        }
        // Get information for the current call ( property or method).
        // There could be similar names which only differ in the cases
        // of letters. First we assume that the name which was passed into
        // GetIDsOfNames is correct. If we won't get information with that
        // name then we have the invocation service use the XExactName interface.
        sal_Bool validInfo= sal_True;
        InvocationInfo invInfo;
        try{
            invInfo= inv2->getInfoForName( sMemberName, sal_False);
        }
        catch( IllegalArgumentException )
        {
            validInfo= sal_False;
        }

        if( ! validInfo)
        {
            invInfo= inv2->getInfoForName( sMemberName, sal_True);
        }
        if( invInfo.aName.pData)
        {
            bTypesAvailable= sal_True;
            info= invInfo;
        }
    }
    return bTypesAvailable;
}
// XBridgeSupplier2 ---------------------------------------------------
// only bridges itself ( this instance of InterfaceOleWrapper_Impl)from UNO to IDispatch
// If sourceModelType is UNO than any UNO interface implemented by InterfaceOleWrapper_Impl
// can bridged to IDispatch ( if destModelType == OLE). The IDispatch is
// implemented by this class.
Any SAL_CALL InterfaceOleWrapper_Impl::createBridge(const Any& modelDepObject,
                                const Sequence<sal_Int8>& ProcessId,
                                sal_Int16 sourceModelType,
                                sal_Int16 destModelType)
            throw (IllegalArgumentException, RuntimeException)
{

    Any retAny;
    if( sourceModelType == UNO && destModelType == OLE &&
        modelDepObject.getValueTypeClass() == TypeClass_INTERFACE )
    {
        Reference<XInterface> xInt;
        if( modelDepObject >>= xInt )
        {
            if( xInt == Reference<XInterface>( static_cast<XWeak*>( this), UNO_QUERY))
            {
                VARIANT *pVar= (VARIANT*)CoTaskMemAlloc( sizeof( VARIANT));
                if( pVar)
                {
                    pVar->vt= VT_DISPATCH;
                    pVar->pdispVal= static_cast<IDispatch*>( this);
                    AddRef();

                    retAny<<= reinterpret_cast< sal_uInt32 >( pVar);
                }
            }
        }
    }

    return retAny;
}

// XInterface ------------------------------------------------------
// void SAL_CALL InterfaceOleWrapper_Impl::acquire(  ) throw()
// {
//  AddRef();
// }
// void SAL_CALL InterfaceOleWrapper_Impl::release(  ) throw()
// {
//  Release();
// }


// XInitialization --------------------------------------------------
void SAL_CALL InterfaceOleWrapper_Impl::initialize( const Sequence< Any >& aArguments )
        throw(Exception, RuntimeException)
{
    switch( aArguments.getLength() )
    {
    case 2: // the object wraps an UNO struct
        aArguments[0] >>= m_xInvocation;
        aArguments[1] >>= m_defaultValueType;
        break;
    case 3: // the object wraps an UNO interface
        aArguments[0] >>= m_xInvocation;
        aArguments[1] >>= m_xOrigin;
        aArguments[2] >>= m_defaultValueType;
        break;
    }

    m_xExactName= Reference<XExactName>( m_xInvocation, UNO_QUERY);
}

Reference< XInterface > InterfaceOleWrapper_Impl::createUnoWrapperInstance()
{
    Reference<XWeak> xWeak= static_cast<XWeak*>( new InterfaceOleWrapper_Impl(
                            m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
    return Reference<XInterface>( xWeak, UNO_QUERY);
}

Reference<XInterface> InterfaceOleWrapper_Impl::createComWrapperInstance()
{
    Reference<XWeak> xWeak= static_cast<XWeak*>( new IUnknownWrapper_Impl(
                            m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
    return Reference<XInterface>( xWeak, UNO_QUERY);
}



// "getType" is used in convertValueObject to map the string denoting the type
// to an actual Type object.
Type getType( const BSTR type)
{
    Type retType;
    typelib_TypeDescription * pDesc= NULL;
    OUString str( type);
    typelib_typedescription_getByName( &pDesc, str.pData );
    if( pDesc)
    {
        retType= Type( pDesc->pWeakRef );
        typelib_typedescription_release( pDesc);
    }
    return retType;
}

static sal_Bool writeBackOutParameter2( VARIANTARG* pDest, VARIANT* pSource)
{
    sal_Bool ret = sal_False;
    HRESULT hr;

    // Handle JScriptValue objects and JScript out params ( Array object )
    CComVariant varDest( *pDest);

    if( SUCCEEDED( varDest.ChangeType(VT_DISPATCH)))

//      if( (pDest->vt ==  VT_DISPATCH) ||
//          (pDest->vt == (VT_VARIANT | VT_BYREF) &&
//          pDest->pvarVal->vt == VT_DISPATCH) )
    {
        CComPtr<IDispatch> spDispDest(varDest.pdispVal);
//          if( pDest->vt == VT_DISPATCH)
//              spDispDest= pDest->pdispVal;
//          else
//              spDispDest= pDest->pvarVal->pdispVal;

//      CComPtr <IJScriptValueObject> spValueDest;

        // special Handling for a JScriptValue object
        CComQIPtr<IJScriptValueObject> spValueDest(spDispDest);
        if (spValueDest)
        {
            VARIANT_BOOL varBool= VARIANT_FALSE;
            if( SUCCEEDED( hr= spValueDest->IsOutParam( &varBool) )
                && varBool == VARIANT_TRUE  ||
                SUCCEEDED(hr= spValueDest->IsInOutParam( &varBool) )
                && varBool == VARIANT_TRUE )
            {
                if( SUCCEEDED( spValueDest->Set( CComVariant(), *pSource)))
                    ret= sal_True;
            }
        }
        else if (pDest->vt == VT_DISPATCH)// VT_DISPATCH -> JScript out param
        {
            // We use IDispatchEx because its GetDispID function causes the creation
            // of a property if it does not exist already. This is convenient for
            // out parameters in JScript. Then the user must not specify propery "0"
            // explicitly
            CComQIPtr<IDispatchEx> spDispEx( spDispDest);
            if( spDispEx)
            {
                CComBSTR nullProp(L"0");
                DISPID dwDispID;
                if( SUCCEEDED( spDispEx->GetDispID( nullProp, fdexNameEnsure, &dwDispID)))
                {
                    DISPPARAMS dispparams = {NULL, NULL, 1, 1};
                    dispparams.rgvarg = pSource;
                    DISPID dispidPut = DISPID_PROPERTYPUT;
                    dispparams.rgdispidNamedArgs = &dispidPut;

                    if (pSource->vt == VT_UNKNOWN || pSource->vt == VT_DISPATCH ||
                        (pSource->vt & VT_ARRAY) || (pSource->vt & VT_BYREF))
                        hr = spDispEx->InvokeEx(dwDispID, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF,
                                                &dispparams, NULL, NULL, NULL);
                    else
                        hr= spDispEx->InvokeEx(dwDispID, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT,
                                               &dispparams, NULL, NULL, NULL);
                    if( SUCCEEDED(hr))
                        ret= sal_True;
                }
            }
        }
        else
            ret= writeBackOutParameter( pDest, pSource);
    }
    else // The param can't be a JScript out-parameter ( an Array object), it could be a VBScript
    {   // param. The function checks itself for correct VBScript params
        ret= writeBackOutParameter( pDest, pSource);
    }
    return ret;
}
// VisualBasic Script passes arguments as VT_VARIANT | VT_BYREF be it in or out parameter.
// Thus we are in charge of freeing an eventual value contained by the inner VARIANT
// Please note: VariantCopy doesn't free a VT_BYREF value
// The out parameters are expected to have always a valid type
static sal_Bool writeBackOutParameter(VARIANTARG* pDest, VARIANT* pSource)
{
    HRESULT hr;
    sal_Bool ret = FALSE;
    // Out parameter must be VT_BYREF
    if ((V_VT(pDest) & VT_BYREF) != 0 )
    {
        VARTYPE oleTypeFlags = V_VT(pSource);

        // if caller accept VARIANT as out parameter, any value must be converted
        if (V_VT(pDest) == (VT_VARIANT | VT_BYREF))
        {
            // When the user provides a VARIANT rather then a concrete type
            // we just copy the source to the out, in/out parameter
            // VT_DISPATCH, VT_UNKNOWN, VT_ARRAY, VT_BSTR in the VARIANT that
            // is contained in pDest are released by VariantCopy
            VariantCopy(V_VARIANTREF(pDest), pSource);
            ret = sal_True;
        }
        else
        {
            // variantarg and variant must have same type
              if ((V_VT(pDest) & oleTypeFlags) == oleTypeFlags)
            {
                if ((oleTypeFlags & VT_ARRAY) != 0)
                {
                    // In / Out Param
                    if( *V_ARRAYREF(pDest) != NULL)
                        hr= SafeArrayCopyData( V_ARRAY(pSource), *V_ARRAYREF(pDest));
                    else
                        // Out Param
                        hr= SafeArrayCopy(V_ARRAY(pSource), V_ARRAYREF(pDest)) == NOERROR;
                    if( SUCCEEDED( hr))
                        ret = sal_True;
                }
                else
                {
                    // copy base type
                    switch (V_VT(pSource))
                    {
                    case VT_I2:
                    {
                        *V_I2REF(pDest) = V_I2(pSource);
                        ret = sal_True;
                        break;
                    }
                    case VT_I4:
                        *V_I4REF(pDest) = V_I4(pSource);
                        ret = sal_True;
                        break;
                    case VT_R4:
                        *V_R4REF(pDest) = V_R4(pSource);
                        ret = sal_True;
                        break;
                    case VT_R8:
                        *V_R8REF(pDest) = V_R8(pSource);
                        ret = sal_True;
                        break;
                    case VT_CY:
                        *V_CYREF(pDest) = V_CY(pSource);
                        ret = sal_True;
                        break;
                    case VT_DATE:
                        *V_DATEREF(pDest) = V_DATE(pSource);
                        ret = sal_True;
                        break;
                    case VT_BSTR:
                        SysFreeString( *pDest->pbstrVal);

                        *V_BSTRREF(pDest) = SysAllocString(V_BSTR(pSource));
                        ret = sal_True;
                        break;
                    case VT_DISPATCH:
                        if (*V_DISPATCHREF(pDest) != NULL)
                            (*V_DISPATCHREF(pDest))->Release();

                        *V_DISPATCHREF(pDest) = V_DISPATCH(pSource);

                        if (*V_DISPATCHREF(pDest) != NULL)
                            (*V_DISPATCHREF(pDest))->AddRef();

                        ret = sal_True;
                        break;
                    case VT_ERROR:
                        *V_ERRORREF(pDest) = V_ERROR(pSource);
                        ret = sal_True;
                        break;
                    case VT_BOOL:
                        *V_BOOLREF(pDest) = V_BOOL(pSource);
                        ret = sal_True;
                        break;
                    case VT_UNKNOWN:
                        if (*V_UNKNOWNREF(pDest) != NULL)
                            (*V_UNKNOWNREF(pDest))->Release();

                        *V_UNKNOWNREF(pDest) = V_UNKNOWN(pSource);

                        if (*V_UNKNOWNREF(pDest) != NULL)
                            (*V_UNKNOWNREF(pDest))->AddRef();

                        ret = sal_True;
                        break;
                    case VT_I1:
                        *V_I1REF(pDest) = V_I1(pSource);
                        ret = sal_True;
                        break;
                    case VT_UI1:
                        *V_UI1REF(pDest) = V_UI1(pSource);
                        ret = sal_True;
                        break;
                    case VT_UI2:
                        *V_UI2REF(pDest) = V_UI2(pSource);
                        ret = sal_True;
                        break;
                    case VT_UI4:
                        *V_UI4REF(pDest) = V_UI4(pSource);
                        ret = sal_True;
                        break;
                    case VT_INT:
                        *V_INTREF(pDest) = V_INT(pSource);
                        ret = sal_True;
                        break;
                    case VT_UINT:
                        *V_UINTREF(pDest) = V_UINT(pSource);
                        ret = sal_True;
                        break;
                    default:
                        break;
                    }
                }
            }
            else
            {
                // Handling of special cases
                // Destination and source types are different
                if( pDest->vt == (VT_BSTR | VT_BYREF)
                    && pSource->vt == VT_I2)
                {
                    // When the user provides a String as out our in/out parameter
                    // and the type is char (TypeClass_CHAR) then we convert to a BSTR
                    // instead of VT_I2 as is done otherwise
                    OLECHAR buff[]= {0,0};
                    buff[0]= pSource->iVal;

                    SysFreeString( *pDest->pbstrVal);
                    *pDest->pbstrVal= SysAllocString( buff);
                }
            }
        }
    }
    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::Invoke(DISPID dispidMember,
                                              REFIID riid,
                                              LCID lcid,
                                              unsigned short wFlags,
                                               DISPPARAMS * pdispparams,
                                              VARIANT * pvarResult,
                                              EXCEPINFO * pexcepinfo,
                                               unsigned int * puArgErr )
{
    HRESULT ret = S_OK;

    sal_Bool bHandled= sal_False;
    ret= InvokeGeneral( dispidMember,  wFlags, pdispparams, pvarResult,  pexcepinfo,
                   puArgErr, bHandled);
    if( bHandled)
        return ret;

    if ((dispidMember > 0) && (dispidMember <= m_MemberInfos.size()) && m_xInvocation.is())
    {
        MemberInfo d = m_MemberInfos[dispidMember - 1];
        DWORD flags = wFlags & d.flags;

        if (flags != 0)
        {
            if ((flags & DISPATCH_METHOD) != 0)
            {
                if (pdispparams->cNamedArgs > 0)
                    ret = DISP_E_NONAMEDARGS;
                else
                {
                    Sequence<Any> params;

                    if( SUCCEEDED(ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams , params ) ))
                    {
                        ret= doInvoke(  pdispparams, pvarResult,
                          pexcepinfo, puArgErr, d.name, params );
                    }
                    else
                        ret = DISP_E_BADVARTYPE;
                }
            }
            else if ((flags & DISPATCH_PROPERTYGET) != 0)
            {
                ret=  doGetProperty( pdispparams, pvarResult,
                                    pexcepinfo, d.name);
            }
            else if ((flags & DISPATCH_PROPERTYPUT || flags & DISPATCH_PROPERTYPUTREF) != 0)
            {
                if (pdispparams->cArgs != 1)
                    ret = DISP_E_BADPARAMCOUNT;
                else
                {
                    Sequence<Any> params;
                    ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params );
                    if( SUCCEEDED( ret) && (params.getLength() > 0))
                    {
                        ret= doSetProperty( pdispparams, pvarResult, pexcepinfo, puArgErr, d.name, params);
                    }
                    else
                        ret = DISP_E_BADVARTYPE;
                }
            }
        }
        else
            ret= DISP_E_MEMBERNOTFOUND;
    }
    else
        ret = DISP_E_MEMBERNOTFOUND;

    return ret;
}

HRESULT InterfaceOleWrapper_Impl::doInvoke( DISPPARAMS * pdispparams, VARIANT * pvarResult,
                              EXCEPINFO * pexcepinfo, unsigned int * puArgErr, OUString& name, Sequence<Any>& params)
{

    HRESULT ret= S_OK;
    Sequence<INT16>     outIndex;
    Sequence<Any>   outParams;
    Any                 returnValue;

    if (pdispparams->cNamedArgs > 0)
        return DISP_E_NONAMEDARGS;

    // invoke method and take care of exceptions
    try
    {
        returnValue = m_xInvocation->invoke(name,
                                            params,
                                            outIndex,
                                            outParams);

        // try to write back out parameter
        if (outIndex.getLength() > 0)
        {
             const INT16* pOutIndex = outIndex.getConstArray();
            const Any* pOutParams = outParams.getConstArray();

            for (UINT32 i = 0; i < outIndex.getLength(); i++)
            {
                VARIANT variant;

                VariantInit(&variant);

                // Currently a Sequence is converted to an SafeArray of VARIANTs.
                anyToVariant( &variant, pOutParams[i]);

                // out parameter need special handling if they are VT_DISPATCH
                // and used in JScript
                int outindex= pOutIndex[i];
                writeBackOutParameter2(&(pdispparams->rgvarg[pdispparams->cArgs - 1 - outindex]),
                                      &variant );

                VariantClear(&variant);
            }
        }

        // write back return value
        if ((pvarResult != NULL) && ! anyToVariant(pvarResult, returnValue))
        {
            ret = DISP_E_BADVARTYPE;
        }
    }
    catch(IllegalArgumentException e)
    {
        ret = DISP_E_TYPEMISMATCH;
    }
    catch(CannotConvertException e)
    {
        ret= mapCannotConvertException( e, puArgErr);
    }
    catch(InvocationTargetException e)
    {
        if (pexcepinfo != NULL)
        {
            Any org = e.TargetException;

            pexcepinfo->wCode = UNO_2_OLE_EXCEPTIONCODE;
            pexcepinfo->bstrSource = SysAllocString(L"any ONE component");
            pexcepinfo->bstrDescription = SysAllocString(
                  org.getValueType().getTypeName());
        }
        ret = ResultFromScode(DISP_E_EXCEPTION);
    }
    catch( NoSuchMethodException e)
    {
        ret= DISP_E_MEMBERNOTFOUND;
    }
    catch( ... )
    {
        ret= DISP_E_EXCEPTION;
    }
    return ret;
}

HRESULT InterfaceOleWrapper_Impl::doGetProperty( DISPPARAMS * pdispparams, VARIANT * pvarResult,
                                                EXCEPINFO * pexcepinfo, OUString& name)
{
    HRESULT ret= S_OK;

    Any value;
    try
    {
        Any returnValue = m_xInvocation->getValue( name);
        // write back return value
        if (!anyToVariant(pvarResult, returnValue))     {
            ret = DISP_E_BADVARTYPE;
        }
    }
    catch(UnknownPropertyException e)
    {
        ret = DISP_E_MEMBERNOTFOUND;
    }
    catch( ... )
    {
        ret= DISP_E_EXCEPTION;
    }
    return  ret;
}

HRESULT InterfaceOleWrapper_Impl::doSetProperty( DISPPARAMS * pdispparams, VARIANT * pvarResult,
                                        EXCEPINFO * pexcepinfo, unsigned int * puArgErr, OUString& name, Sequence<Any> params)
{
    HRESULT ret= S_OK;

    try
    {
        m_xInvocation->setValue( name, params.getConstArray()[0]);
    }
    catch(UnknownPropertyException )
    {
        ret = DISP_E_MEMBERNOTFOUND;
    }
    catch(CannotConvertException e)
    {
        ret= mapCannotConvertException( e, puArgErr);
    }
    catch(InvocationTargetException e)
    {
        if (pexcepinfo != NULL)
        {
            Any org = e.TargetException;

            pexcepinfo->wCode = UNO_2_OLE_EXCEPTIONCODE;
            pexcepinfo->bstrSource = SysAllocString(L"any ONE component");
            pexcepinfo->bstrDescription = SysAllocString(
                org.getValueType().getTypeName());
        }
        ret = DISP_E_EXCEPTION;
    }
    catch( ... )
    {
        ret= DISP_E_EXCEPTION;
    }
    return ret;
}

HRESULT InterfaceOleWrapper_Impl::InvokeGeneral( DISPID dispidMember, unsigned short wFlags,
                         DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo,
                         unsigned int * puArgErr, sal_Bool& bHandled)
{
    HRESULT ret= S_OK;
// DISPID_VALUE | The DEFAULT Value is required in JScript when the situation
// is that we put an object into an Array object ( out parameter). We have to return
// IDispatch otherwise the object cannot be accessed from the Script.
    if( dispidMember == DISPID_VALUE && wFlags == DISPATCH_PROPERTYGET
    && m_defaultValueType != VT_EMPTY && pvarResult != NULL)
    {
        bHandled= sal_True;
        if( m_defaultValueType == VT_DISPATCH)
        {
            pvarResult->vt= VT_DISPATCH;
            pvarResult->pdispVal= static_cast<IDispatch*>( this);
            AddRef();
            ret= S_OK;
        }
    }
// ---------
    // function: _GetValueObject
    else if( dispidMember == DISPID_JSCRIPT_VALUE_FUNC)
    {
        bHandled= sal_True;
        if( !pvarResult)
            ret= E_POINTER;
        CComObject< JScriptValue>* pValue;
        if( SUCCEEDED( CComObject<JScriptValue>::CreateInstance( &pValue)))
        {
            pValue->AddRef();
            pvarResult->vt= VT_DISPATCH;
            pvarResult->pdispVal= CComQIPtr<IDispatch>(pValue->GetUnknown());
            ret= S_OK;
        }
        else
            ret= DISP_E_EXCEPTION;
    }
    else if( dispidMember == DISPID_GET_STRUCT_FUNC)
    {
        bHandled= sal_True;
        sal_Bool bStruct= sal_False;


        Reference<XInterface> xIntCore= m_smgr->createInstance( OUString::createFromAscii("com.sun.star.reflection.CoreReflection"));
        Reference<XIdlReflection> xRefl( xIntCore, UNO_QUERY);
        if( xRefl.is() )
        {
            // the first parameter is in DISPPARAMS rgvargs contains the name of the struct.
            CComVariant arg;
            if( pdispparams->cArgs == 1 && SUCCEEDED( arg.ChangeType( VT_BSTR, &pdispparams->rgvarg[0])) )
            {
                Reference<XIdlClass> classStruct= xRefl->forName( arg.bstrVal);
                if( classStruct.is())
                {
                    Any anyStruct;
                    classStruct->createObject( anyStruct);
                    CComVariant var;
                    if( anyToVariant( &var, anyStruct ))
                    {
                        if( var.vt == VT_DISPATCH)
                        {
                            VariantCopy( pvarResult, & var);
                            bStruct= sal_True;
                        }
                    }

                }
            }
        }
        ret= bStruct == sal_True ? S_OK : DISP_E_EXCEPTION;
    }
    return ret;
}




STDMETHODIMP InterfaceOleWrapper_Impl::GetDispID(BSTR bstrName, DWORD grfdex, DISPID __RPC_FAR *pid)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::InvokeEx(
    /* [in] */ DISPID id,
    /* [in] */ LCID lcid,
    /* [in] */ WORD wFlags,
    /* [in] */ DISPPARAMS __RPC_FAR *pdp,
    /* [out] */ VARIANT __RPC_FAR *pvarRes,
    /* [out] */ EXCEPINFO __RPC_FAR *pei,
    /* [unique][in] */ IServiceProvider __RPC_FAR *pspCaller)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}


STDMETHODIMP InterfaceOleWrapper_Impl::DeleteMemberByName(
    /* [in] */ BSTR bstr,
    /* [in] */ DWORD grfdex)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::DeleteMemberByDispID(DISPID id)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetMemberProperties(
    /* [in] */ DISPID id,
    /* [in] */ DWORD grfdexFetch,
    /* [out] */ DWORD __RPC_FAR *pgrfdex)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetMemberName(
    /* [in] */ DISPID id,
    /* [out] */ BSTR __RPC_FAR *pbstrName)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetNextDispID(
    /* [in] */ DWORD grfdex,
    /* [in] */ DISPID id,
    /* [out] */ DISPID __RPC_FAR *pid)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}

STDMETHODIMP InterfaceOleWrapper_Impl::GetNameSpaceParent(
    /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunk)
{
    HRESULT ret = ResultFromScode(E_NOTIMPL);

    return ret;
}


/*************************************************************************

    UnoObjectWrapperRemoteOpt

*************************************************************************/
UnoObjectWrapperRemoteOpt::UnoObjectWrapperRemoteOpt( Reference<XMultiServiceFactory>& aFactory,
                                                     sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass):
InterfaceOleWrapper_Impl( aFactory, unoWrapperClass, comWrapperClass),
m_currentId(1)

{
}
UnoObjectWrapperRemoteOpt::~UnoObjectWrapperRemoteOpt()
{
}

// UnoConversionUtilities
Reference< XInterface > UnoObjectWrapperRemoteOpt::createUnoWrapperInstance()
{
    Reference<XWeak> xWeak= static_cast<XWeak*>( new UnoObjectWrapperRemoteOpt(
                                                 m_smgr, m_nUnoWrapperClass, m_nComWrapperClass));
    return Reference<XInterface>( xWeak, UNO_QUERY);
}

STDMETHODIMP  UnoObjectWrapperRemoteOpt::GetIDsOfNames ( REFIID riid, OLECHAR ** rgszNames, unsigned int cNames,
                                LCID lcid, DISPID * rgdispid )
{
    MutexGuard guard( getBridgeMutex());

    if( ! rgdispid)
        return E_POINTER;
    HRESULT ret = E_UNEXPECTED;
    // ----------------------------------------
    // _GetValueObject
    if( ! wcscmp( *rgszNames, JSCRIPT_VALUE_FUNC))
    {
        *rgdispid= DISPID_JSCRIPT_VALUE_FUNC;
        return S_OK;
    }
    else if( ! wcscmp( *rgszNames, GET_STRUCT_FUNC))
    {
        *rgdispid= DISPID_GET_STRUCT_FUNC;
        return S_OK;
    }

    // ----------------------------------------
    if (m_xInvocation.is() && (cNames > 0))
    {
        OUString name(rgszNames[0]);
        // has this name been determined as "bad"
        BadNameMap::iterator badIter= m_badNameMap.find( name);
        if( badIter == m_badNameMap.end() )
        {
            // name has not been bad before( member exists
            typedef NameToIdMap::iterator ITnames;
            pair< ITnames, bool > pair_id= m_nameToDispIdMap.insert( NameToIdMap::value_type(name, m_currentId++));
            // new ID inserted ?
            if( pair_id.second )
            {// yes, now create MemberInfo and ad to IdToMemberInfoMap
                MemberInfo d(0, name);
                m_idToMemberInfoMap.insert( IdToMemberInfoMap::value_type( m_currentId - 1, d));
            }

            *rgdispid = pair_id.first->second;
            ret = S_OK;
        }
        else
            ret= DISP_E_UNKNOWNNAME;
    }
    return ret;
}

STDMETHODIMP  UnoObjectWrapperRemoteOpt::Invoke ( DISPID dispidMember, REFIID riid, LCID lcid, unsigned short wFlags,
                         DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo,
                         unsigned int * puArgErr )
{
    HRESULT ret = S_OK;

    sal_Bool bHandled= sal_False;
    ret= InvokeGeneral( dispidMember,  wFlags, pdispparams, pvarResult,  pexcepinfo,
                   puArgErr, bHandled);
    if( bHandled)
        return ret;

    if ( dispidMember > 0  && m_xInvocation.is())
    {

        IdToMemberInfoMap::iterator it_MemberInfo= m_idToMemberInfoMap.find( dispidMember);
        if( it_MemberInfo != m_idToMemberInfoMap.end() )
        {
            MemberInfo& info= it_MemberInfo->second;

            Sequence<Any> params; // holds converted any s
            if( ! info.flags )
            { // DISPID called for the first time
                if( wFlags == DISPATCH_METHOD )
                {

                    if( SUCCEEDED(ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )))
                    {
                        if( FAILED( ret= doInvoke( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params))
                                  && ret == DISP_E_MEMBERNOTFOUND)
                        {
                            // try to get the exact name
                            OUString exactName;
                            if (m_xExactName.is())
                            {
                                exactName = m_xExactName->getExactName( info.name);
                                // invoke again
                                if( exactName.getLength() != 0)
                                {
                                    if( SUCCEEDED( ret= doInvoke( pdispparams, pvarResult,
                                                        pexcepinfo, puArgErr, exactName, params)))
                                      info.name= exactName;
                                }
                            }
                        }
                        if( SUCCEEDED( ret ) )
                            info.flags= DISPATCH_METHOD;

                    } // end if ( convertDispparamsArgs )
                    else // convertDispparamsArgs failed
                        ret= DISP_E_BADVARTYPE;
                } //if( wFlags == DISPATCH_METHOD )

                else if( wFlags == DISPATCH_PROPERTYPUT || wFlags == DISPATCH_PROPERTYPUTREF)
                {
                    if( SUCCEEDED( ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )))
                    {
                        if( FAILED( ret= doSetProperty( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params))
                                  && ret == DISP_E_MEMBERNOTFOUND)
                        {
                            // try to get the exact name
                            OUString exactName;
                            if (m_xExactName.is())
                            {
                                exactName = m_xExactName->getExactName( info.name);
                                // invoke again
                                if( exactName.getLength() != 0)
                                {
                                    if( SUCCEEDED( ret= doSetProperty( pdispparams, pvarResult,
                                                        pexcepinfo, puArgErr, exactName, params)))
                                      info.name= exactName;
                                }
                            }
                        }
                        if( SUCCEEDED( ret ) )
                            info.flags= DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYGET;
                    } // end if ( convertDispparamsArgs )
                    else
                        ret= DISP_E_BADVARTYPE;

                }

                else if( wFlags == DISPATCH_PROPERTYGET)
                {
                    if( FAILED( ret= doGetProperty( pdispparams, pvarResult,
                                                    pexcepinfo, info.name))
                              && ret == DISP_E_MEMBERNOTFOUND)
                    {
                        // try to get the exact name
                        OUString exactName;
                        if (m_xExactName.is())
                        {
                            exactName = m_xExactName->getExactName( info.name);
                            // invoke again
                            if( exactName.getLength() != 0)
                            {
                                if( SUCCEEDED( ret= doGetProperty( pdispparams, pvarResult,
                                                    pexcepinfo, exactName)))
                                  info.name= exactName;
                            }
                        }
                    }
                    if( SUCCEEDED( ret ) )
                        info.flags= DISPATCH_PROPERTYGET | DISPATCH_PROPERTYPUT;
                }
                else if( wFlags & DISPATCH_METHOD &&
                    (wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF))
                {

                    OUString exactName;
                    // convert params for DISPATCH_METHOD or DISPATCH_PROPERTYPUT
                    if( SUCCEEDED( ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )) )
                    {// try first as method
                        if( FAILED( ret= doInvoke( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params))
                                  && ret == DISP_E_MEMBERNOTFOUND)
                        {
                            // try to get the exact name
                            if (m_xExactName.is())
                            {
                                exactName = m_xExactName->getExactName( info.name);
                                // invoke again
                                if( exactName.getLength() != 0)
                                {
                                    if( SUCCEEDED( ret= doInvoke( pdispparams, pvarResult,
                                                        pexcepinfo, puArgErr, exactName, params)))
                                      info.name= exactName;
                                }
                            }
                        }
                        if( SUCCEEDED( ret ) )
                            info.flags= DISPATCH_METHOD;

                        // try as property
                        if( FAILED( ret) && pdispparams->cArgs == 1)
                        {
                            if( FAILED( ret= doSetProperty( pdispparams, pvarResult,
                                      pexcepinfo, puArgErr, info.name, params))
                                      && ret == DISP_E_MEMBERNOTFOUND)
                            {
                                // try to get the exact name
                                if( exactName.getLength() != 0)
                                {
                                    if( SUCCEEDED( ret= doSetProperty( pdispparams, pvarResult,
                                                        pexcepinfo, puArgErr, exactName, params)))
                                      info.name= exactName;
                                }
                            }
                            if( SUCCEEDED( ret ) )
                                info.flags= DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYGET;
                        }
                    } // end if ( convertDispparamsArgs )
                    else
                        ret= DISP_E_BADVARTYPE;
                }
                else if( wFlags & DISPATCH_METHOD && wFlags & DISPATCH_PROPERTYGET)
                {
                    OUString exactName;
                    if( SUCCEEDED(ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )) )
                    {
                        if( FAILED( ret= doInvoke( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params))
                                  && ret == DISP_E_MEMBERNOTFOUND)
                        {
                            // try to get the exact name
                            if (m_xExactName.is())
                            {
                                exactName = m_xExactName->getExactName( info.name);
                                // invoke again
                                if( exactName.getLength() != 0)
                                {
                                    if( SUCCEEDED( ret= doInvoke( pdispparams, pvarResult,
                                                        pexcepinfo, puArgErr, exactName, params)))
                                      info.name= exactName;
                                }
                            }
                        }
                        if( SUCCEEDED( ret ) )
                            info.flags= DISPATCH_METHOD;

                    } // end if ( convertDispparamsArgs )
                    else
                        ret= DISP_E_BADVARTYPE;

                    // try as property
                    if( FAILED( ret) && pdispparams->cArgs == 1)
                    {
                        if( FAILED( ret= doGetProperty( pdispparams, pvarResult,
                                  pexcepinfo, info.name))
                                  && ret == DISP_E_MEMBERNOTFOUND)
                        {
                            if( exactName.getLength() != 0)
                            {
                                if( SUCCEEDED( ret= doSetProperty( pdispparams, pvarResult,
                                                    pexcepinfo, puArgErr, exactName, params)))
                                  info.name= exactName;
                            }
                        }
                        if( SUCCEEDED( ret ) )
                            info.flags= DISPATCH_PROPERTYGET;
                    }
                }

                // update ínformation about this member
                if( ret == DISP_E_MEMBERNOTFOUND)
                {
                    // Remember the name as not existing
                    // and remove the MemberInfo
                    m_badNameMap[info.name]= sal_False;
                    m_idToMemberInfoMap.erase( it_MemberInfo);
                }
            } // if( ! info.flags )
            else // IdToMemberInfoMap contains a MemberInfo
            {
                if( wFlags & DISPATCH_METHOD && info.flags == DISPATCH_METHOD)
                {
                    if( SUCCEEDED(ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )) )
                    {
                        ret= doInvoke( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params);
                    }
                    else
                        ret= DISP_E_BADVARTYPE;
                }
                else if( (wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF )  &&
                        info.flags & DISPATCH_PROPERTYPUT)
                {
                    if( SUCCEEDED( ret= convertDispparamsArgs(dispidMember, wFlags, pdispparams, params )) )
                    {
                        ret= doSetProperty( pdispparams, pvarResult,
                                  pexcepinfo, puArgErr, info.name, params);
                    }
                    else
                        ret= DISP_E_BADVARTYPE;
                }
                else if( (wFlags & DISPATCH_PROPERTYGET) && ( info.flags & DISPATCH_PROPERTYGET))
                {
                    ret= doGetProperty( pdispparams, pvarResult,
                              pexcepinfo, info.name);
                }
                else
                {
                    ret= DISP_E_MEMBERNOTFOUND;
                }
            }
        }//     if( it_MemberInfo != m_idToMemberInfoMap.end() )
        else
            ret= DISP_E_MEMBERNOTFOUND;
    }
    return ret;
}

HRESULT UnoObjectWrapperRemoteOpt::methodInvoke( DISPID dispidMember, DISPPARAMS * pdispparams, VARIANT * pvarResult,
                              EXCEPINFO * pexcepinfo, unsigned int * puArgErr, Sequence<Any> params)
{
    return S_OK;
}


// The returned HRESULT is only appropriate for IDispatch::Invoke
static HRESULT mapCannotConvertException( CannotConvertException e, unsigned int * puArgErr)
{
    HRESULT ret;
    sal_Bool bWriteIndex= sal_True;

    switch ( e.Reason)
    {
        case FailReason::OUT_OF_RANGE:
            ret = DISP_E_OVERFLOW;
            break;
        case FailReason::IS_NOT_NUMBER:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::IS_NOT_ENUM:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::IS_NOT_BOOL:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::NO_SUCH_INTERFACE:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::SOURCE_IS_NO_DERIVED_TYPE:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::TYPE_NOT_SUPPORTED:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::INVALID:
            ret = DISP_E_TYPEMISMATCH;
            break;
        case FailReason::NO_DEFAULT_AVAILABLE:
            ret = DISP_E_BADPARAMCOUNT;
            break;
        case FailReason::UNKNOWN:
            ret = E_UNEXPECTED;
            break;
        default:
            ret = E_UNEXPECTED;
            bWriteIndex= sal_False;
            break;
    }

    if( bWriteIndex &&  puArgErr != NULL)
        *puArgErr = e.ArgumentIndex;
    return ret;
}

// The function maps the TypeClass of the any to VARTYPE: If
// the Any contains STRUCT or INTERFACE then the return value
// is VT_DISPATCH. The function is used from o2u_createUnoObjectWrapper
// and the result is put into the constructor of the uno - wrapper
// object. If a client asks the object for DISPID_VALUE and this
// funtion returned VT_DISPATCH then the IDispatch of the same
// object is being returned.
// See InterfaceOleWrapper_Impl::Invoke, InterfaceOleWrapper_Impl::m_defaultValueType
const VARTYPE getVarType( const Any& value)
{
    VARTYPE ret= VT_EMPTY;

    switch ( value.getValueTypeClass())
    {
    case TypeClass_STRUCT: ret= VT_DISPATCH; break;
    case TypeClass_INTERFACE: ret= VT_DISPATCH; break;
    default: break;
    }
    return ret;
}




} // end namespace