1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | // |
5 | // File: MarshalNative.h |
6 | // |
7 | |
8 | // |
9 | // FCall's for the Marshal class |
10 | // |
11 | |
12 | |
13 | #ifndef __MARSHALNATIVE_H__ |
14 | #define __MARSHALNATIVE_H__ |
15 | |
16 | #include "fcall.h" |
17 | |
18 | #define MAX_UTF8_CHAR_SIZE 3 |
19 | |
20 | //!!! Must be kept in sync with ArrayWithOffset class layout. |
21 | struct ArrayWithOffsetData |
22 | { |
23 | BASEARRAYREF m_Array; |
24 | INT32 m_cbOffset; |
25 | INT32 m_cbCount; |
26 | }; |
27 | |
28 | |
29 | #ifdef FEATURE_COMINTEROP |
30 | enum ComMemberType |
31 | { |
32 | CMT_Method = 0, |
33 | CMT_PropGet = 1, |
34 | CMT_PropSet = 2 |
35 | }; |
36 | #endif // FEATURE_COMINTEROP |
37 | |
38 | class MarshalNative |
39 | { |
40 | public: |
41 | static INT32 QCALLTYPE NumParamBytes(MethodDesc * pMD); |
42 | static VOID QCALLTYPE Prelink(MethodDesc * pMD); |
43 | |
44 | //==================================================================== |
45 | // These methods convert between an HR and and a managed exception. |
46 | //==================================================================== |
47 | static FCDECL2(void, ThrowExceptionForHR, INT32 errorCode, LPVOID errorInfo); |
48 | static FCDECL2(Object *, GetExceptionForHR, INT32 errorCode, LPVOID errorInfo); |
49 | static FCDECL1(int, GetHRForException, Object* eUNSAFE); |
50 | static FCDECL1(int, GetHRForException_WinRT, Object* eUNSAFE); |
51 | |
52 | static FCDECL4(void, CopyToNative, Object* psrcUNSAFE, INT32 startindex, LPVOID pdst, INT32 length); |
53 | static FCDECL4(void, CopyToManaged, LPVOID psrc, Object* pdstUNSAFE, INT32 startindex, INT32 length); |
54 | static FCDECL2(UINT32, SizeOfClass, ReflectClassBaseObject* refClass, CLR_BOOL throwIfNotMarshalable); |
55 | |
56 | static FCDECL2(LPVOID, FCUnsafeAddrOfPinnedArrayElement, ArrayBase *arr, INT32 index); |
57 | |
58 | static FCDECL1(UINT32, OffsetOfHelper, ReflectFieldObject* pFieldUNSAFE); |
59 | static FCDECL0(int, GetLastWin32Error); |
60 | static FCDECL1(void, SetLastWin32Error, int error); |
61 | static FCDECL1(INT32, CalculateCount, ArrayWithOffsetData* pRef); |
62 | |
63 | static FCDECL3(VOID, StructureToPtr, Object* pObjUNSAFE, LPVOID ptr, CLR_BOOL fDeleteOld); |
64 | static FCDECL3(VOID, PtrToStructureHelper, LPVOID ptr, Object* pObjIn, CLR_BOOL allowValueClasses); |
65 | static FCDECL2(VOID, DestroyStructure, LPVOID ptr, ReflectClassBaseObject* refClassUNSAFE); |
66 | |
67 | //==================================================================== |
68 | // map a fiber cookie from the hosting APIs into a managed Thread object |
69 | //==================================================================== |
70 | static FCDECL1(THREADBASEREF, GetThreadFromFiberCookie, int cookie); |
71 | |
72 | static FCDECL3(LPVOID, GetUnmanagedThunkForManagedMethodPtr, LPVOID pfnMethodToWrap, PCCOR_SIGNATURE pbSignature, ULONG cbSignature); |
73 | static FCDECL3(LPVOID, GetManagedThunkForUnmanagedMethodPtr, LPVOID pfnMethodToWrap, PCCOR_SIGNATURE pbSignature, ULONG cbSignature); |
74 | |
75 | static FCDECL0(UINT32, GetSystemMaxDBCSCharSize); |
76 | |
77 | static FCDECL2(LPVOID, GCHandleInternalAlloc, Object *obj, int type); |
78 | static FCDECL1(VOID, GCHandleInternalFree, OBJECTHANDLE handle); |
79 | static FCDECL1(LPVOID, GCHandleInternalGet, OBJECTHANDLE handle); |
80 | static FCDECL3(VOID, GCHandleInternalSet, OBJECTHANDLE handle, Object *obj, CLR_BOOL isPinned); |
81 | static FCDECL4(Object*, GCHandleInternalCompareExchange, OBJECTHANDLE handle, Object *obj, Object* oldObj, CLR_BOOL isPinned); |
82 | static FCDECL1(LPVOID, GCHandleInternalAddrOfPinnedObject, OBJECTHANDLE handle); |
83 | static FCDECL1(INT32, GCHandleInternalGetHandleType, OBJECTHANDLE handle); |
84 | |
85 | static FCDECL2(Object*, GetDelegateForFunctionPointerInternal, LPVOID FPtr, ReflectClassBaseObject* refTypeUNSAFE); |
86 | static FCDECL1(LPVOID, GetFunctionPointerForDelegateInternal, Object* refDelegateUNSAFE); |
87 | |
88 | #ifdef FEATURE_COMINTEROP |
89 | //==================================================================== |
90 | // map GUID to Type |
91 | //==================================================================== |
92 | static FCDECL1(Object*, GetLoadedTypeForGUID, GUID* pGuid); |
93 | |
94 | //==================================================================== |
95 | // map Type to ITypeInfo* |
96 | //==================================================================== |
97 | static FCDECL1(ITypeInfo*, GetITypeInfoForType, ReflectClassBaseObject* refClassUNSAFE); |
98 | |
99 | //==================================================================== |
100 | // return the IUnknown* for an Object |
101 | //==================================================================== |
102 | static FCDECL2(IUnknown*, GetIUnknownForObjectNative, Object* orefUNSAFE, CLR_BOOL fOnlyInContext); |
103 | |
104 | //==================================================================== |
105 | // return the raw IUnknown* for a COM Object not related to current |
106 | // context |
107 | // Does not AddRef the returned pointer |
108 | //==================================================================== |
109 | static FCDECL1(IUnknown*, GetRawIUnknownForComObjectNoAddRef, Object* orefUNSAFE); |
110 | |
111 | //==================================================================== |
112 | // return the IDispatch* for an Object |
113 | //==================================================================== |
114 | static FCDECL2(IDispatch*, GetIDispatchForObjectNative, Object* orefUNSAFE, CLR_BOOL fOnlyInContext); |
115 | |
116 | //==================================================================== |
117 | // return the IUnknown* representing the interface for the Object |
118 | // Object o should support Type T |
119 | //==================================================================== |
120 | static FCDECL4(IUnknown*, GetComInterfaceForObjectNative, Object* orefUNSAFE, ReflectClassBaseObject* refClassUNSAFE, CLR_BOOL fOnlyInContext, CLR_BOOL bEnableCustomizedQueryInterface); |
121 | |
122 | //==================================================================== |
123 | // return an Object for IUnknown |
124 | //==================================================================== |
125 | static FCDECL1(Object*, GetObjectForIUnknown, IUnknown* pUnk); |
126 | |
127 | //==================================================================== |
128 | // return a unique cacheless Object for IUnknown |
129 | //==================================================================== |
130 | static FCDECL1(Object*, GetUniqueObjectForIUnknown, IUnknown* pUnk); |
131 | |
132 | //==================================================================== |
133 | // return an Object for IUnknown, using the Type T, |
134 | // NOTE: |
135 | // Type T should be either a COM imported Type or a sub-type of COM imported Type |
136 | //==================================================================== |
137 | static FCDECL2(Object*, GetTypedObjectForIUnknown, IUnknown* pUnk, ReflectClassBaseObject* refClassUNSAFE); |
138 | |
139 | //==================================================================== |
140 | // Free unused RCWs in the current COM+ context. |
141 | //==================================================================== |
142 | static FCDECL0(void, CleanupUnusedObjectsInCurrentContext); |
143 | |
144 | //==================================================================== |
145 | // Checks whether there are RCWs from any context available for cleanup. |
146 | //==================================================================== |
147 | static FCDECL0(FC_BOOL_RET, AreComObjectsAvailableForCleanup); |
148 | |
149 | //==================================================================== |
150 | // Create an object and aggregate it, then return the inner unknown. |
151 | //==================================================================== |
152 | static FCDECL2(IUnknown*, CreateAggregatedObject, IUnknown* pOuter, Object* refObjUNSAFE); |
153 | |
154 | //==================================================================== |
155 | // check if the object is classic COM component |
156 | //==================================================================== |
157 | static FCDECL1(FC_BOOL_RET, IsComObject, Object* objUNSAFE); |
158 | |
159 | //==================================================================== |
160 | // free the COM component and zombie this object |
161 | // further usage of this Object might throw an exception, |
162 | //==================================================================== |
163 | static FCDECL1(INT32, ReleaseComObject, Object* objUNSAFE); |
164 | static FCDECL1(void, FinalReleaseComObject, Object* objUNSAFE); |
165 | |
166 | //==================================================================== |
167 | // This method takes the given COM object and wraps it in an object |
168 | // of the specified type. The type must be derived from __ComObject. |
169 | //==================================================================== |
170 | static FCDECL2(Object*, InternalCreateWrapperOfType, Object* objUNSAFE, ReflectClassBaseObject* refClassUNSAFE); |
171 | |
172 | //==================================================================== |
173 | // check if the type is visible from COM. |
174 | //==================================================================== |
175 | static FCDECL1(FC_BOOL_RET, IsTypeVisibleFromCom, ReflectClassBaseObject* refClassUNSAFE); |
176 | |
177 | //==================================================================== |
178 | // IUnknown Helpers |
179 | //==================================================================== |
180 | static FCDECL3(HRESULT, QueryInterface, IUnknown* pUnk, REFGUID iid, void** ppv); |
181 | static FCDECL1(ULONG, AddRef, IUnknown* pUnk); |
182 | static FCDECL1(ULONG, Release, IUnknown* pUnk); |
183 | |
184 | //==================================================================== |
185 | // These methods convert OLE variants to and from objects. |
186 | //==================================================================== |
187 | static FCDECL2(void, GetNativeVariantForObject, Object* ObjUNSAFE, LPVOID pDestNativeVariant); |
188 | static FCDECL1(Object*, GetObjectForNativeVariant, LPVOID pSrcNativeVariant); |
189 | static FCDECL2(Object*, GetObjectsForNativeVariants, VARIANT* aSrcNativeVariant, int cVars); |
190 | |
191 | //==================================================================== |
192 | // This method generates a guid for the specified type. |
193 | //==================================================================== |
194 | static FCDECL2(void, DoGenerateGuidForType, GUID * result, ReflectClassBaseObject* refTypeUNSAFE); |
195 | |
196 | //==================================================================== |
197 | // Methods to retrieve information from TypeLibs and TypeInfos. |
198 | //==================================================================== |
199 | static FCDECL2(void, DoGetTypeLibGuid, GUID * result, Object* refTlbUNSAFE); |
200 | static FCDECL1(LCID, GetTypeLibLcid, Object* refTlbUNSAFE); |
201 | static FCDECL3(void, GetTypeLibVersion, Object* refTlbUNSAFE, int *pMajor, int *pMinor); |
202 | static FCDECL2(void, DoGetTypeInfoGuid, GUID * result, Object* refTypeInfoUNSAFE); |
203 | |
204 | //==================================================================== |
205 | // Given a assembly, return the TLBID that will be generated for the |
206 | // typelib exported from the assembly. |
207 | //==================================================================== |
208 | static FCDECL2(void, DoGetTypeLibGuidForAssembly, GUID * result, AssemblyBaseObject* refAsmUNSAFE); |
209 | |
210 | //==================================================================== |
211 | // Given a assembly, return the version number of the type library |
212 | // that would be exported from the assembly. |
213 | //==================================================================== |
214 | static FCDECL3(void, GetTypeLibVersionForAssembly, AssemblyBaseObject* refAsmUNSAFE, INT32 *pMajorVersion, INT32 *pMinorVersion); |
215 | |
216 | //==================================================================== |
217 | // These methods are used to map COM slots to method info's. |
218 | //==================================================================== |
219 | static FCDECL1(int, GetStartComSlot, ReflectClassBaseObject* tUNSAFE); |
220 | static FCDECL1(int, GetEndComSlot, ReflectClassBaseObject* tUNSAFE); |
221 | static FCDECL3(Object*, GetMethodInfoForComSlot, ReflectClassBaseObject* tUNSAFE, INT32 slot, ComMemberType* pMemberType); |
222 | |
223 | static FCDECL1(int, GetComSlotForMethodInfo, ReflectMethodObject* pMethodUNSAFE); |
224 | |
225 | static FCDECL1(Object*, WrapIUnknownWithComObject, IUnknown* pUnk); |
226 | |
227 | static FCDECL2(void, ChangeWrapperHandleStrength, Object* orefUNSAFE, CLR_BOOL fIsWeak); |
228 | static FCDECL2(void, InitializeWrapperForWinRT, Object *unsafe_pThis, IUnknown **ppUnk); |
229 | static FCDECL2(void, InitializeManagedWinRTFactoryObject, Object *unsafe_pThis, ReflectClassBaseObject *unsafe_pType); |
230 | static FCDECL1(Object *, GetNativeActivationFactory, ReflectClassBaseObject *unsafe_pType); |
231 | static void QCALLTYPE GetInspectableIIDs(QCall::ObjectHandleOnStack hobj, QCall::ObjectHandleOnStack retArrayGuids); |
232 | |
233 | private: |
234 | static int GetComSlotInfo(MethodTable *pMT, MethodTable **ppDefItfMT); |
235 | static BOOL IsObjectInContext(OBJECTREF *pObj); |
236 | #endif // FEATURE_COMINTEROP |
237 | }; |
238 | |
239 | // Check that the supplied object is valid to put in a pinned handle, |
240 | // throwing an exception if not. |
241 | void ValidatePinnedObject(OBJECTREF obj); |
242 | |
243 | #endif |
244 | |