-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
marshalnative.cpp
1387 lines (1090 loc) · 37.3 KB
/
marshalnative.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// File: MarshalNative.cpp
//
//
// FCall's for the PInvoke classlibs
//
#include "common.h"
#include "clsload.hpp"
#include "method.hpp"
#include "class.h"
#include "object.h"
#include "field.h"
#include "util.hpp"
#include "excep.h"
#include "siginfo.hpp"
#include "threads.h"
#include "stublink.h"
#include "dllimport.h"
#include "jitinterface.h"
#include "eeconfig.h"
#include "log.h"
#include "fieldmarshaler.h"
#include "cgensys.h"
#include "gcheaputilities.h"
#include "dbginterface.h"
#include "marshalnative.h"
#include "fcall.h"
#include "dllimportcallback.h"
#include "comdelegate.h"
#include "typestring.h"
#include "appdomain.inl"
#ifdef FEATURE_COMINTEROP
#include "comcallablewrapper.h"
#include "commtmemberinfomap.h"
#include "runtimecallablewrapper.h"
#include "olevariant.h"
#include "interoputil.h"
#endif // FEATURE_COMINTEROP
// Prelink
// Does advance loading of an N/Direct library
VOID QCALLTYPE MarshalNative::Prelink(MethodDesc * pMD)
{
QCALL_CONTRACT;
// Arguments are check on managed side
PRECONDITION(pMD != NULL);
// If the code is already ready, we are done. Else, we need to execute the prestub
// This is a perf thing since it's always safe to execute the prestub twice.
if (!pMD->IsPointingToPrestub())
return;
// Silently ignore if not N/Direct and not runtime generated.
if (!(pMD->IsNDirect()) && !(pMD->IsRuntimeSupplied()))
return;
BEGIN_QCALL;
pMD->CheckRestore();
pMD->DoPrestub(NULL);
END_QCALL;
}
// IsBuiltInComSupported
// Built-in COM support is only checked from the native side to ensure the runtime
// is in a consistent state
BOOL QCALLTYPE MarshalNative::IsBuiltInComSupported()
{
QCALL_CONTRACT;
BOOL ret = TRUE;
BEGIN_QCALL;
#ifdef FEATURE_COMINTEROP
ret = g_pConfig->IsBuiltInCOMSupported();
#else // FEATURE_COMINTEROP
ret = FALSE;
#endif // FEATURE_COMINTEROP
END_QCALL;
return ret;
}
FCIMPL3(VOID, MarshalNative::StructureToPtr, Object* pObjUNSAFE, LPVOID ptr, CLR_BOOL fDeleteOld)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(ptr, NULL_OK));
}
CONTRACTL_END;
OBJECTREF pObj = (OBJECTREF) pObjUNSAFE;
HELPER_METHOD_FRAME_BEGIN_1(pObj);
if (ptr == NULL)
COMPlusThrowArgumentNull(W("ptr"));
if (pObj == NULL)
COMPlusThrowArgumentNull(W("structure"));
// Code path will accept both regular layout objects and boxed value classes
// with layout.
MethodTable *pMT = pObj->GetMethodTable();
if (pMT->HasInstantiation())
COMPlusThrowArgumentException(W("structure"), W("Argument_NeedNonGenericObject"));
if (pMT->IsBlittable())
{
memcpyNoGCRefs(ptr, pObj->GetData(), pMT->GetNativeSize());
}
else if (pMT->HasLayout())
{
MethodDesc* structMarshalStub;
{
GCX_PREEMP();
structMarshalStub = NDirect::CreateStructMarshalILStub(pMT);
}
if (fDeleteOld)
{
MarshalStructViaILStub(structMarshalStub, pObj->GetData(), ptr, StructMarshalStubs::MarshalOperation::Cleanup);
}
MarshalStructViaILStub(structMarshalStub, pObj->GetData(), ptr, StructMarshalStubs::MarshalOperation::Marshal);
}
else
{
COMPlusThrowArgumentException(W("structure"), W("Argument_MustHaveLayoutOrBeBlittable"));
}
HELPER_METHOD_FRAME_END();
}
FCIMPLEND
FCIMPL3(VOID, MarshalNative::PtrToStructureHelper, LPVOID ptr, Object* pObjIn, CLR_BOOL allowValueClasses)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(ptr, NULL_OK));
}
CONTRACTL_END;
OBJECTREF pObj = ObjectToOBJECTREF(pObjIn);
HELPER_METHOD_FRAME_BEGIN_1(pObj);
if (ptr == NULL)
COMPlusThrowArgumentNull(W("ptr"));
if (pObj == NULL)
COMPlusThrowArgumentNull(W("structure"));
// Code path will accept regular layout objects.
MethodTable *pMT = pObj->GetMethodTable();
// Validate that the object passed in is not a value class.
if (!allowValueClasses && pMT->IsValueType())
{
COMPlusThrowArgumentException(W("structure"), W("Argument_StructMustNotBeValueClass"));
}
else if (pMT->IsBlittable())
{
memcpyNoGCRefs(pObj->GetData(), ptr, pMT->GetNativeSize());
}
else if (pMT->HasLayout())
{
MethodDesc* structMarshalStub;
{
GCX_PREEMP();
structMarshalStub = NDirect::CreateStructMarshalILStub(pMT);
}
MarshalStructViaILStub(structMarshalStub, pObj->GetData(), ptr, StructMarshalStubs::MarshalOperation::Unmarshal);
}
else
{
COMPlusThrowArgumentException(W("structure"), W("Argument_MustHaveLayoutOrBeBlittable"));
}
HELPER_METHOD_FRAME_END();
}
FCIMPLEND
FCIMPL2(VOID, MarshalNative::DestroyStructure, LPVOID ptr, ReflectClassBaseObject* refClassUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(ptr, NULL_OK));
}
CONTRACTL_END;
REFLECTCLASSBASEREF refClass = (REFLECTCLASSBASEREF) refClassUNSAFE;
HELPER_METHOD_FRAME_BEGIN_1(refClass);
if (ptr == NULL)
COMPlusThrowArgumentNull(W("ptr"));
if (refClass == NULL)
COMPlusThrowArgumentNull(W("structureType"));
if (refClass->GetMethodTable() != g_pRuntimeTypeClass)
COMPlusThrowArgumentException(W("structureType"), W("Argument_MustBeRuntimeType"));
TypeHandle th = refClass->GetType();
if (th.HasInstantiation())
COMPlusThrowArgumentException(W("structureType"), W("Argument_NeedNonGenericType"));
if (th.IsBlittable())
{
// ok to call with blittable structure, but no work to do in this case.
}
else if (th.HasLayout())
{
MethodDesc* structMarshalStub;
{
GCX_PREEMP();
structMarshalStub = NDirect::CreateStructMarshalILStub(th.GetMethodTable());
}
MarshalStructViaILStub(structMarshalStub, nullptr, ptr, StructMarshalStubs::MarshalOperation::Cleanup);
}
else
{
COMPlusThrowArgumentException(W("structureType"), W("Argument_MustHaveLayoutOrBeBlittable"));
}
HELPER_METHOD_FRAME_END();
}
FCIMPLEND
FCIMPL1(FC_BOOL_RET, MarshalNative::IsPinnable, Object* obj)
{
FCALL_CONTRACT;
VALIDATEOBJECT(obj);
if (obj == NULL)
FC_RETURN_BOOL(TRUE);
if (obj->GetMethodTable() == g_pStringClass)
FC_RETURN_BOOL(TRUE);
if (obj->GetMethodTable()->IsArray())
{
BASEARRAYREF asArray = (BASEARRAYREF)ObjectToOBJECTREF(obj);
if (CorTypeInfo::IsPrimitiveType(asArray->GetArrayElementType()))
FC_RETURN_BOOL(TRUE);
TypeHandle th = asArray->GetArrayElementTypeHandle();
if (!th.IsTypeDesc())
{
MethodTable *pMT = th.AsMethodTable();
if (pMT->IsValueType() && pMT->IsBlittable())
FC_RETURN_BOOL(TRUE);
}
FC_RETURN_BOOL(FALSE);
}
FC_RETURN_BOOL(obj->GetMethodTable()->IsBlittable());
}
FCIMPLEND
/************************************************************************
* PInvoke.SizeOf(Class)
*/
FCIMPL2(UINT32, MarshalNative::SizeOfClass, ReflectClassBaseObject* refClassUNSAFE, CLR_BOOL throwIfNotMarshalable)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(refClassUNSAFE));
}
CONTRACTL_END;
UINT32 rv = 0;
REFLECTCLASSBASEREF refClass = (REFLECTCLASSBASEREF)refClassUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(refClass);
// refClass is validated to be non-NULL RuntimeType by callers
TypeHandle th = refClass->GetType();
if (throwIfNotMarshalable && (!th.IsBlittable() || th.IsArray()))
{
GCX_PREEMP();
// Determine if the type is marshalable
if (!IsStructMarshalable(th))
{
// It isn't marshalable so throw an ArgumentException.
StackSString strTypeName;
TypeString::AppendType(strTypeName, th);
COMPlusThrow(kArgumentException, IDS_CANNOT_MARSHAL, strTypeName.GetUnicode(), NULL, NULL);
}
}
// The type is marshalable or we don't care so return its size.
rv = th.GetMethodTable()->GetNativeSize();
HELPER_METHOD_FRAME_END();
return rv;
}
FCIMPLEND
/************************************************************************
* PInvoke.OffsetOfHelper(Class, Field)
*/
FCIMPL1(UINT32, MarshalNative::OffsetOfHelper, ReflectFieldObject *pFieldUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(pFieldUNSAFE));
}
CONTRACTL_END;
REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
FieldDesc *pField = refField->GetField();
TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable());
if (th.IsBlittable())
{
return pField->GetOffset();
}
UINT32 externalOffset;
HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
{
GCX_PREEMP();
// Determine if the type is marshalable.
if (!IsStructMarshalable(th))
{
// It isn't marshalable so throw an ArgumentException.
StackSString strTypeName;
TypeString::AppendType(strTypeName, th);
COMPlusThrow(kArgumentException, IDS_CANNOT_MARSHAL, strTypeName.GetUnicode(), NULL, NULL);
}
EEClassNativeLayoutInfo const* pNativeLayoutInfo = th.GetMethodTable()->GetNativeLayoutInfo();
NativeFieldDescriptor const*pNFD = pNativeLayoutInfo->GetNativeFieldDescriptors();
UINT numReferenceFields = pNativeLayoutInfo->GetNumFields();
#ifdef _DEBUG
bool foundField = false;
#endif
while (numReferenceFields--)
{
if (pNFD->GetFieldDesc() == pField)
{
externalOffset = pNFD->GetExternalOffset();
INDEBUG(foundField = true);
break;
}
pNFD++;
}
CONSISTENCY_CHECK_MSG(foundField, "We should never hit this point since we already verified that the requested field was present from managed code");
}
HELPER_METHOD_FRAME_END();
return externalOffset;
}
FCIMPLEND
FCIMPL2(Object*, MarshalNative::GetDelegateForFunctionPointerInternal, LPVOID FPtr, ReflectClassBaseObject* refTypeUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(refTypeUNSAFE != NULL);
}
CONTRACTL_END;
OBJECTREF refDelegate = NULL;
REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF) refTypeUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refDelegate);
// Retrieve the method table from the RuntimeType. We already verified in managed
// code that the type was a RuntimeType that represented a delegate. Because type handles
// for delegates must have a method table, we are safe in telling prefix to assume it below.
MethodTable* pMT = refType->GetType().GetMethodTable();
PREFIX_ASSUME(pMT != NULL);
refDelegate = COMDelegate::ConvertToDelegate(FPtr, pMT);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(refDelegate);
}
FCIMPLEND
FCIMPL1(LPVOID, MarshalNative::GetFunctionPointerForDelegateInternal, Object* refDelegateUNSAFE)
{
FCALL_CONTRACT;
LPVOID pFPtr = NULL;
OBJECTREF refDelegate = (OBJECTREF) refDelegateUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(refDelegate);
pFPtr = COMDelegate::ConvertToCallback(refDelegate);
HELPER_METHOD_FRAME_END();
return pFPtr;
}
FCIMPLEND
#ifdef _DEBUG
namespace
{
BOOL STDMETHODCALLTYPE IsInCooperativeGCMode()
{
return GetThread()->PreemptiveGCDisabled();
}
}
MarshalNative::IsInCooperativeGCMode_fn QCALLTYPE MarshalNative::GetIsInCooperativeGCModeFunctionPointer()
{
QCALL_CONTRACT;
MarshalNative::IsInCooperativeGCMode_fn ret = NULL;
BEGIN_QCALL;
ret = IsInCooperativeGCMode;
END_QCALL;
return ret;
}
#endif
/************************************************************************
* Marshal.GetLastPInvokeError
*/
FCIMPL0(int, MarshalNative::GetLastPInvokeError)
{
FCALL_CONTRACT;
return (UINT32)(GetThread()->m_dwLastError);
}
FCIMPLEND
/************************************************************************
* Marshal.SetLastPInvokeError
*/
FCIMPL1(void, MarshalNative::SetLastPInvokeError, int error)
{
FCALL_CONTRACT;
GetThread()->m_dwLastError = (DWORD)error;
}
FCIMPLEND
/************************************************************************
* Support for the GCHandle class.
*/
// Check that the supplied object is valid to put in a pinned handle.
// Throw an exception if not.
void ValidatePinnedObject(OBJECTREF obj)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
}
CONTRACTL_END;
// NULL is fine.
if (obj == NULL)
return;
if (obj->GetMethodTable() == g_pStringClass)
return;
if (obj->GetMethodTable()->IsArray())
{
BASEARRAYREF asArray = (BASEARRAYREF) obj;
if (CorTypeInfo::IsPrimitiveType(asArray->GetArrayElementType()))
return;
TypeHandle th = asArray->GetArrayElementTypeHandle();
if (!th.IsTypeDesc())
{
MethodTable *pMT = th.AsMethodTable();
if (pMT->IsValueType() && pMT->IsBlittable())
return;
}
}
else if (obj->GetMethodTable()->IsBlittable())
{
return;
}
COMPlusThrow(kArgumentException, IDS_EE_NOTISOMORPHIC);
}
NOINLINE static OBJECTHANDLE FCDiagCreateHandle(OBJECTREF objRef, int type)
{
OBJECTHANDLE hnd = NULL;
FC_INNER_PROLOG(MarshalNative::GCHandleInternalAlloc);
// Make the stack walkable for the profiler
HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(Frame::FRAME_ATTR_EXACT_DEPTH | Frame::FRAME_ATTR_CAPTURE_DEPTH_2);
hnd = GetAppDomain()->CreateTypedHandle(objRef, static_cast<HandleType>(type));
HELPER_METHOD_FRAME_END_POLL();
FC_INNER_EPILOG();
return hnd;
}
FCIMPL2(LPVOID, MarshalNative::GCHandleInternalAlloc, Object *obj, int type)
{
FCALL_CONTRACT;
OBJECTREF objRef(obj);
assert(type >= HNDTYPE_WEAK_SHORT && type <= HNDTYPE_WEAK_NATIVE_COM);
if (CORProfilerTrackGC())
{
FC_INNER_RETURN(LPVOID, (LPVOID) FCDiagCreateHandle(objRef, type));
}
OBJECTHANDLE hnd = GetAppDomain()->GetHandleStore()->CreateHandleOfType(OBJECTREFToObject(objRef), static_cast<HandleType>(type));
if (!hnd)
{
FCThrow(kOutOfMemoryException);
}
return (LPVOID) hnd;
}
FCIMPLEND
NOINLINE static void FCDiagDestroyHandle(OBJECTHANDLE handle)
{
FC_INNER_PROLOG(MarshalNative::GCHandleInternalFree);
// Make the stack walkable for the profiler
HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH | Frame::FRAME_ATTR_CAPTURE_DEPTH_2);
DestroyTypedHandle(handle);
HELPER_METHOD_FRAME_END();
FC_INNER_EPILOG();
}
// Free a GC handle.
FCIMPL1(VOID, MarshalNative::GCHandleInternalFree, OBJECTHANDLE handle)
{
FCALL_CONTRACT;
if (CORProfilerTrackGC())
{
FC_INNER_RETURN_VOID(FCDiagDestroyHandle(handle));
}
GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfUnknownType(handle);
}
FCIMPLEND
// Get the object referenced by a GC handle.
FCIMPL1(LPVOID, MarshalNative::GCHandleInternalGet, OBJECTHANDLE handle)
{
FCALL_CONTRACT;
OBJECTREF objRef;
objRef = ObjectFromHandle(handle);
return *((LPVOID*)&objRef);
}
FCIMPLEND
// Update the object referenced by a GC handle.
FCIMPL2(VOID, MarshalNative::GCHandleInternalSet, OBJECTHANDLE handle, Object *obj)
{
FCALL_CONTRACT;
OBJECTREF objRef(obj);
StoreObjectInHandle(handle, objRef);
}
FCIMPLEND
// Update the object referenced by a GC handle.
FCIMPL3(Object*, MarshalNative::GCHandleInternalCompareExchange, OBJECTHANDLE handle, Object *obj, Object* oldObj)
{
FCALL_CONTRACT;
OBJECTREF newObjref(obj);
OBJECTREF oldObjref(oldObj);
LPVOID ret = NULL;
// Update the stored object reference.
ret = InterlockedCompareExchangeObjectInHandle(handle, newObjref, oldObjref);
return (Object*)ret;
}
FCIMPLEND
//====================================================================
// *** Interop Helpers ***
//====================================================================
FCIMPL2(Object *, MarshalNative::GetExceptionForHR, INT32 errorCode, LPVOID errorInfo)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(FAILED(errorCode));
PRECONDITION(CheckPointer(errorInfo, NULL_OK));
}
CONTRACTL_END;
OBJECTREF RetExceptionObj = NULL;
HELPER_METHOD_FRAME_BEGIN_RET_1(RetExceptionObj);
// Retrieve the IErrorInfo to use.
IErrorInfo *pErrorInfo = (IErrorInfo*)errorInfo;
if (pErrorInfo == (IErrorInfo*)(-1))
{
pErrorInfo = NULL;
}
else if (!pErrorInfo)
{
if (SafeGetErrorInfo(&pErrorInfo) != S_OK)
pErrorInfo = NULL;
}
::GetExceptionForHR(errorCode, pErrorInfo, &RetExceptionObj);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(RetExceptionObj);
}
FCIMPLEND
FCIMPL1(int, MarshalNative::GetHRForException, Object* eUNSAFE)
{
CONTRACTL {
NOTHROW; // Used by reverse COM IL stubs, so we must not throw exceptions back to COM
DISABLED(GC_TRIGGERS); // FCALLS with HELPER frames have issues with GC_TRIGGERS
MODE_COOPERATIVE;
} CONTRACTL_END;
int retVal = 0;
OBJECTREF e = (OBJECTREF) eUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_NOTHROW_1({ retVal = COR_E_STACKOVERFLOW; }, e);
retVal = SetupErrorInfo(e);
HELPER_METHOD_FRAME_END_NOTHROW();
return retVal;
}
FCIMPLEND
#ifdef FEATURE_COMINTEROP
//====================================================================
// return the IUnknown* for an Object.
//====================================================================
FCIMPL1(IUnknown*, MarshalNative::GetIUnknownForObjectNative, Object* orefUNSAFE)
{
FCALL_CONTRACT;
IUnknown* retVal = NULL;
OBJECTREF oref = (OBJECTREF) orefUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
_ASSERTE(oref != NULL);
// Ensure COM is started up.
EnsureComStarted();
retVal = GetComIPFromObjectRef(&oref, ComIpType_OuterUnknown, NULL);
HELPER_METHOD_FRAME_END();
return retVal;
}
FCIMPLEND
//====================================================================
// return the IDispatch* for an Object.
//====================================================================
FCIMPL1(IDispatch*, MarshalNative::GetIDispatchForObjectNative, Object* orefUNSAFE)
{
FCALL_CONTRACT;
IDispatch* retVal = NULL;
OBJECTREF oref = (OBJECTREF) orefUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
_ASSERTE(oref != NULL);
// Ensure COM is started up.
EnsureComStarted();
retVal = (IDispatch*)GetComIPFromObjectRef(&oref, ComIpType_Dispatch, NULL);
HELPER_METHOD_FRAME_END();
return retVal;
}
FCIMPLEND
//====================================================================
// return the IUnknown* representing the interface for the Object
// Object o should support Type T
//====================================================================
FCIMPL3(IUnknown*, MarshalNative::GetComInterfaceForObjectNative, Object* orefUNSAFE, ReflectClassBaseObject* refClassUNSAFE, CLR_BOOL bEnableCustomizedQueryInterface)
{
FCALL_CONTRACT;
IUnknown* retVal = NULL;
OBJECTREF oref = (OBJECTREF) orefUNSAFE;
REFLECTCLASSBASEREF refClass = (REFLECTCLASSBASEREF) refClassUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_2(oref, refClass);
_ASSERTE(oref != NULL);
_ASSERTE(refClass != NULL);
// Ensure COM is started up.
EnsureComStarted();
if (refClass->GetMethodTable() != g_pRuntimeTypeClass)
COMPlusThrowArgumentException(W("t"), W("Argument_MustBeRuntimeType"));
TypeHandle th = refClass->GetType();
if (th.HasInstantiation())
COMPlusThrowArgumentException(W("t"), W("Argument_NeedNonGenericType"));
if (oref->GetMethodTable()->HasInstantiation())
COMPlusThrowArgumentException(W("o"), W("Argument_NeedNonGenericObject"));
// If the IID being asked for does not represent an interface then
// throw an argument exception.
if (!th.IsInterface())
COMPlusThrowArgumentException(W("t"), W("Arg_MustBeInterface"));
// If the interface being asked for is not visible from COM then
// throw an argument exception.
if (!::IsTypeVisibleFromCom(th))
COMPlusThrowArgumentException(W("t"), W("Argument_TypeMustBeVisibleFromCom"));
retVal = GetComIPFromObjectRef(&oref, th.GetMethodTable(), bEnableCustomizedQueryInterface);
HELPER_METHOD_FRAME_END();
return retVal;
}
FCIMPLEND
//====================================================================
// return an Object for IUnknown
//====================================================================
FCIMPL1(Object*, MarshalNative::GetObjectForIUnknownNative, IUnknown* pUnk)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(pUnk));
}
CONTRACTL_END;
OBJECTREF oref = NULL;
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
// Ensure COM is started up.
EnsureComStarted();
GetObjectRefFromComIP(&oref, pUnk);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(oref);
}
FCIMPLEND
FCIMPL1(Object*, MarshalNative::GetUniqueObjectForIUnknownNative, IUnknown* pUnk)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(pUnk));
}
CONTRACTL_END;
OBJECTREF oref = NULL;
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
HRESULT hr = S_OK;
// Ensure COM is started up.
EnsureComStarted();
GetObjectRefFromComIP(&oref, pUnk, NULL, NULL, ObjFromComIP::UNIQUE_OBJECT);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(oref);
}
FCIMPLEND
//====================================================================
// return an Object for IUnknown, using the Type T,
// NOTE:
// Type T should be either a COM imported Type or a sub-type of COM imported Type
//====================================================================
FCIMPL2(Object*, MarshalNative::GetTypedObjectForIUnknown, IUnknown* pUnk, ReflectClassBaseObject* refClassUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(pUnk, NULL_OK));
}
CONTRACTL_END;
OBJECTREF oref = NULL;
REFLECTCLASSBASEREF refClass = (REFLECTCLASSBASEREF) refClassUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_2(refClass, oref);
HRESULT hr = S_OK;
MethodTable* pMTClass = NULL;
if(!pUnk)
COMPlusThrowArgumentNull(W("pUnk"));
if(refClass != NULL)
{
if (refClass->GetMethodTable() != g_pRuntimeTypeClass)
COMPlusThrowArgumentException(W("t"), W("Argument_MustBeRuntimeType"));
TypeHandle th = refClass->GetType();
if (th.HasInstantiation())
COMPlusThrowArgumentException(W("t"), W("Argument_NeedNonGenericType"));
pMTClass = th.GetMethodTable();
}
else
COMPlusThrowArgumentNull(W("t"));
// Ensure COM is started up.
EnsureComStarted();
GetObjectRefFromComIP(&oref, pUnk, pMTClass);
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(oref);
}
FCIMPLEND
FCIMPL2(IUnknown*, MarshalNative::CreateAggregatedObjectNative, IUnknown* pOuter, Object* refObjUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
PRECONDITION(CheckPointer(pOuter, NULL_OK));
}
CONTRACTL_END;
IUnknown* pInner = NULL;
OBJECTREF oref = (OBJECTREF)refObjUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
HRESULT hr = S_OK;
if (!pOuter)
COMPlusThrowArgumentNull(W("pOuter"));
if (oref == NULL)
COMPlusThrowArgumentNull(W("o"));
// Ensure COM is started up.
EnsureComStarted();
if (NULL != ComCallWrapper::GetWrapperForObject(oref))
COMPlusThrowArgumentException(W("o"), W("Argument_AlreadyACCW"));
//get wrapper for the object, this could enable GC
CCWHolder pWrap = ComCallWrapper::InlineGetWrapper(&oref);
// Aggregation support,
pWrap->InitializeOuter(pOuter);
IfFailThrow(pWrap->GetInnerUnknown((LPVOID*)&pInner));
HELPER_METHOD_FRAME_END();
return pInner;
}
FCIMPLEND
//====================================================================
// Free unused RCWs in the current COM+ context.
//====================================================================
FCIMPL0(void, MarshalNative::CleanupUnusedObjectsInCurrentContext)
{
FCALL_CONTRACT;
HELPER_METHOD_FRAME_BEGIN_0();
if (g_pRCWCleanupList)
{
g_pRCWCleanupList->CleanupWrappersInCurrentCtxThread(
TRUE, // fWait
TRUE, // fManualCleanupRequested
TRUE // bIgnoreComObjectEagerCleanupSetting
);
}
HELPER_METHOD_FRAME_END();
}
FCIMPLEND
//====================================================================
// Checks whether there are RCWs from any context available for cleanup.
//====================================================================
FCIMPL0(FC_BOOL_RET, MarshalNative::AreComObjectsAvailableForCleanup)
{
FCALL_CONTRACT;
BOOL retVal = FALSE;
if (g_pRCWCleanupList)
{
retVal = !g_pRCWCleanupList->IsEmpty();
}
FC_RETURN_BOOL(retVal);
}
FCIMPLEND
//====================================================================
// free the COM component and zombie this object if the ref count hits 0
// further usage of this Object might throw an exception,
//====================================================================
FCIMPL1(INT32, MarshalNative::ReleaseComObject, Object* objUNSAFE)
{
FCALL_CONTRACT;
INT32 retVal = 0;
OBJECTREF obj = (OBJECTREF) objUNSAFE;
HELPER_METHOD_FRAME_BEGIN_RET_1(obj);
if(!obj)
COMPlusThrowArgumentNull(W("o"));
MethodTable* pMT = obj->GetMethodTable();
PREFIX_ASSUME(pMT != NULL);
if(!pMT->IsComObjectType())
COMPlusThrow(kArgumentException, IDS_EE_SRC_OBJ_NOT_COMOBJECT);
// remove the wrapper from the object
retVal = RCW::ExternalRelease(&obj);
HELPER_METHOD_FRAME_END();
return retVal;
}
FCIMPLEND
//====================================================================
// free the COM component and zombie this object
// further usage of this Object might throw an exception,
//====================================================================
FCIMPL1(void, MarshalNative::FinalReleaseComObject, Object* objUNSAFE)
{
FCALL_CONTRACT;
OBJECTREF obj = (OBJECTREF) objUNSAFE;
HELPER_METHOD_FRAME_BEGIN_1(obj);
if(!obj)
COMPlusThrowArgumentNull(W("o"));
MethodTable* pMT = obj->GetMethodTable();
PREFIX_ASSUME(pMT != NULL);
if(!pMT->IsComObjectType())
COMPlusThrow(kArgumentException, IDS_EE_SRC_OBJ_NOT_COMOBJECT);
// remove the wrapper from the object
RCW::FinalExternalRelease(&obj);
HELPER_METHOD_FRAME_END();
}