Skip to content

Commit 4d5b659

Browse files
Modify some defines so COM and Windows Runtime support works on Unity AOT profile.
1 parent 237c030 commit 4d5b659

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public static class Marshal
5353
public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
5454
public static readonly int SystemDefaultCharSize = Environment.IsRunningOnWindows ? 2 : 1;
5555

56-
#if !MOBILE
56+
#if !MOBILE || UNITY_AOT
5757
[MethodImplAttribute (MethodImplOptions.InternalCall)]
5858
private extern static int AddRefInternal (IntPtr pUnk);
5959
#endif
6060

6161
public static int AddRef (IntPtr pUnk)
6262
{
63-
#if !MOBILE
63+
#if !MOBILE || UNITY_AOT
6464
if (pUnk == IntPtr.Zero)
6565
throw new ArgumentException ("Value cannot be null.", "pUnk");
6666
return AddRefInternal (pUnk);
@@ -210,7 +210,9 @@ public static IntPtr CreateAggregatedObject<T> (IntPtr pOuter, T o) {
210210

211211
public static object CreateWrapperOfType (object o, Type t)
212212
{
213-
#if FULL_AOT_RUNTIME
213+
#if UNITY_AOT
214+
throw new NotImplementedException("Marshal.CreateWrapperOfType is not implemented.");
215+
#elif FULL_AOT_RUNTIME
214216
throw new PlatformNotSupportedException ();
215217
#else
216218
__ComObject co = o as __ComObject;
@@ -307,7 +309,7 @@ public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
307309
FreeHGlobal (s);
308310
}
309311

310-
#if !FULL_AOT_RUNTIME
312+
#if !FULL_AOT_RUNTIME || UNITY_AOT
311313
public static Guid GenerateGuidForType (Type type)
312314
{
313315
return type.GUID;
@@ -342,23 +344,25 @@ public static object GetActiveObject (string progID)
342344
throw new NotImplementedException ();
343345
}
344346

345-
#if !MOBILE
347+
#if !MOBILE || UNITY_AOT
346348
[MethodImplAttribute (MethodImplOptions.InternalCall)]
347349
private extern static IntPtr GetCCW (object o, Type T);
348350

349351
private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
350352
{
353+
#if !UNITY_AOT
351354
if (IsComObject (o))
352355
return ((__ComObject)o).GetInterface (T);
353356
else
357+
#endif
354358
return GetCCW (o, T);
355359
}
356360
#endif
357361
#endif // !FULL_AOT_RUNTIME
358362

359363
public static IntPtr GetComInterfaceForObject (object o, Type T)
360364
{
361-
#if MOBILE
365+
#if MOBILE && !UNITY_AOT
362366
throw new PlatformNotSupportedException ();
363367
#else
364368
IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
@@ -377,7 +381,7 @@ public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
377381
return GetComInterfaceForObject ((object)o, typeof (T));
378382
}
379383

380-
#if !FULL_AOT_RUNTIME
384+
#if !FULL_AOT_RUNTIME || UNITY_AOT
381385
[MonoTODO]
382386
public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
383387
{
@@ -430,7 +434,7 @@ public static IntPtr GetHINSTANCE (Module m)
430434

431435
return m.GetHINSTANCE ();
432436
}
433-
#endif // !FULL_AOT_RUNTIME
437+
#endif // !FULL_AOT_RUNTIME || UNITY_AOT
434438

435439
public static int GetExceptionCode ()
436440
{
@@ -453,14 +457,14 @@ public static int GetHRForException (Exception e)
453457
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
454458
public static int GetHRForLastWin32Error()
455459
{
456-
#if FULL_AOT_RUNTIME
460+
#if FULL_AOT_RUNTIME && !UNITY_AOT
457461
throw new PlatformNotSupportedException ();
458462
#else
459463
throw new NotImplementedException ();
460464
#endif
461465
}
462-
463-
#if !FULL_AOT_RUNTIME
466+
467+
#if !FULL_AOT_RUNTIME || UNITY_AOT
464468
[MethodImplAttribute (MethodImplOptions.InternalCall)]
465469
private extern static IntPtr GetIDispatchForObjectInternal (object o);
466470

@@ -471,7 +475,9 @@ public static IntPtr GetIDispatchForObject (object o)
471475
AddRef (pUnk);
472476
return pUnk;
473477
}
478+
#endif // !FULL_AOT_RUNTIME || UNITY_AOT
474479

480+
#if !FULL_AOT_RUNTIME
475481
[MonoTODO]
476482
public static IntPtr GetIDispatchForObjectInContext (object o)
477483
{
@@ -502,15 +508,16 @@ public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMembe
502508
{
503509
throw new NotImplementedException ();
504510
}
511+
#endif // !FULL_AOT_RUNTIME
505512

513+
#if !FULL_AOT_RUNTIME || UNITY_AOT
506514
[MethodImplAttribute (MethodImplOptions.InternalCall)]
507515
private extern static IntPtr GetIUnknownForObjectInternal (object o);
508-
509-
#endif // !FULL_AOT_RUNTIME
516+
#endif // !FULL_AOT_RUNTIME || UNITY_AOT
510517

511518
public static IntPtr GetIUnknownForObject (object o)
512519
{
513-
#if FULL_AOT_RUNTIME
520+
#if FULL_AOT_RUNTIME && !UNITY_AOT
514521
throw new PlatformNotSupportedException ();
515522
#else
516523
IntPtr pUnk = GetIUnknownForObjectInternal (o);
@@ -522,7 +529,7 @@ public static IntPtr GetIUnknownForObject (object o)
522529

523530
public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
524531
{
525-
#if FULL_AOT_RUNTIME
532+
#if FULL_AOT_RUNTIME && !UNITY_AOT
526533
throw new PlatformNotSupportedException ();
527534
#else
528535
Variant vt = new Variant();
@@ -535,29 +542,31 @@ public static void GetNativeVariantForObject<T> (T obj, IntPtr pDstNativeVariant
535542
GetNativeVariantForObject ((object)obj, pDstNativeVariant);
536543
}
537544

538-
#if !MOBILE && !FULL_AOT_RUNTIME
545+
#if (!MOBILE && !FULL_AOT_RUNTIME) || UNITY_AOT
539546
[MethodImplAttribute (MethodImplOptions.InternalCall)]
540547
private static extern object GetObjectForCCW (IntPtr pUnk);
541548
#endif
542549

543550
public static object GetObjectForIUnknown (IntPtr pUnk)
544551
{
545-
#if MOBILE || FULL_AOT_RUNTIME
552+
#if (MOBILE || FULL_AOT_RUNTIME) && !UNITY_AOT
546553
throw new PlatformNotSupportedException ();
547554
#else
548555
object obj = GetObjectForCCW (pUnk);
556+
#if !UNITY_AOT
549557
// was not a CCW
550558
if (obj == null) {
551559
ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
552560
obj = proxy.GetTransparentProxy ();
553561
}
562+
#endif
554563
return obj;
555564
#endif
556565
}
557566

558567
public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
559568
{
560-
#if FULL_AOT_RUNTIME
569+
#if FULL_AOT_RUNTIME && !UNITY_AOT
561570
throw new PlatformNotSupportedException ();
562571
#else
563572
Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
@@ -567,7 +576,7 @@ public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
567576

568577
public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant)
569578
{
570-
#if FULL_AOT_RUNTIME
579+
#if FULL_AOT_RUNTIME && !UNITY_AOT
571580
throw new PlatformNotSupportedException ();
572581
#else
573582
Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
@@ -577,7 +586,7 @@ public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant)
577586

578587
public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
579588
{
580-
#if FULL_AOT_RUNTIME
589+
#if FULL_AOT_RUNTIME && !UNITY_AOT
581590
throw new PlatformNotSupportedException ();
582591
#else
583592
if (cVars < 0)
@@ -592,7 +601,7 @@ public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, in
592601

593602
public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars)
594603
{
595-
#if FULL_AOT_RUNTIME
604+
#if FULL_AOT_RUNTIME && !UNITY_AOT
596605
throw new PlatformNotSupportedException ();
597606
#else
598607
if (cVars < 0)
@@ -608,7 +617,7 @@ public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int
608617
[MonoTODO]
609618
public static int GetStartComSlot (Type t)
610619
{
611-
#if FULL_AOT_RUNTIME
620+
#if FULL_AOT_RUNTIME && !UNITY_AOT
612621
throw new PlatformNotSupportedException ();
613622
#else
614623
throw new NotImplementedException ();
@@ -735,7 +744,7 @@ public static object GetUniqueObjectForIUnknown (IntPtr unknown)
735744
throw new PlatformNotSupportedException ();
736745
}
737746

738-
#if !MOBILE
747+
#if !MOBILE || UNITY_AOT
739748
[MethodImplAttribute (MethodImplOptions.InternalCall)]
740749
public extern static bool IsComObject (object o);
741750
#else
@@ -815,14 +824,14 @@ public static T PtrToStructure<T> (IntPtr ptr) {
815824
return (T) PtrToStructure (ptr, typeof (T));
816825
}
817826

818-
#if !MOBILE
827+
#if !MOBILE || UNITY_AOT
819828
[MethodImplAttribute (MethodImplOptions.InternalCall)]
820829
private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
821830
#endif
822831

823832
public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
824833
{
825-
#if !MOBILE
834+
#if !MOBILE || UNITY_AOT
826835
if (pUnk == IntPtr.Zero)
827836
throw new ArgumentException ("Value cannot be null.", "pUnk");
828837
return QueryInterfaceInternal (pUnk, ref iid, out ppv);
@@ -986,7 +995,7 @@ public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object pt
986995
[MethodImplAttribute(MethodImplOptions.InternalCall)]
987996
public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
988997

989-
#if !MOBILE
998+
#if !MOBILE || UNITY_AOT
990999
[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
9911000
[MethodImplAttribute (MethodImplOptions.InternalCall)]
9921001
private extern static int ReleaseInternal (IntPtr pUnk);
@@ -995,7 +1004,7 @@ public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object pt
9951004
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
9961005
public static int Release (IntPtr pUnk)
9971006
{
998-
#if !MOBILE
1007+
#if !MOBILE || UNITY_AOT
9991008
if (pUnk == IntPtr.Zero)
10001009
throw new ArgumentException ("Value cannot be null.", "pUnk");
10011010

@@ -1005,14 +1014,14 @@ public static int Release (IntPtr pUnk)
10051014
#endif
10061015
}
10071016

1008-
#if !FULL_AOT_RUNTIME
1017+
#if !FULL_AOT_RUNTIME || UNITY_AOT
10091018
[MethodImplAttribute (MethodImplOptions.InternalCall)]
10101019
private extern static int ReleaseComObjectInternal (object co);
10111020
#endif
10121021

10131022
public static int ReleaseComObject (object o)
10141023
{
1015-
#if FULL_AOT_RUNTIME
1024+
#if FULL_AOT_RUNTIME && !UNITY_AOT
10161025
throw new PlatformNotSupportedException ();
10171026
#else
10181027
if (o == null)
@@ -1752,7 +1761,7 @@ internal static void SetLastWin32Error (int error)
17521761
{
17531762
}
17541763

1755-
#if FEATURE_COMINTEROP || MONO_COM
1764+
#if FEATURE_COMINTEROP || MONO_COM || UNITY_AOT
17561765
// Copied from referencesource/mscorlib/system/runtime/interopservices/marshal.cs
17571766
//====================================================================
17581767
// return the raw IUnknown* for a COM Object not related to current

mcs/class/referencesource/mscorlib/system/runtime/interopservices/attributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if MONO_COM
1+
#if MONO_COM || UNITY_AOT
22
#define FEATURE_COMINTEROP
33
#endif
44
// ==++==

mcs/class/referencesource/mscorlib/system/runtime/interopservices/windowsruntime/windowsruntimemarshal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ public static IActivationFactory GetActivationFactory(Type type)
12781278
if (type == null)
12791279
throw new ArgumentNullException("type");
12801280

1281-
#if FEATURE_COMINTEROP || MONO_COM
1281+
#if FEATURE_COMINTEROP || MONO_COM || UNITY_AOT
12821282
if (type.IsWindowsRuntimeObject && type.IsImport)
12831283
{
12841284
return (IActivationFactory)Marshal.GetNativeActivationFactory(type);

mcs/class/referencesource/mscorlib/system/type.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ public bool IsCOMObject {
12491249
get {return IsCOMObjectImpl();}
12501250
}
12511251

1252-
#if FEATURE_COMINTEROP || MONO_COM
1252+
#if FEATURE_COMINTEROP || MONO_COM || UNITY_AOT
12531253
internal bool IsWindowsRuntimeObject {
12541254
[Pure]
12551255
get { return IsWindowsRuntimeObjectImpl(); }
@@ -1313,7 +1313,7 @@ protected virtual bool IsValueTypeImpl()
13131313
// Protected routine to determine if this class represents a COM object
13141314
abstract protected bool IsCOMObjectImpl();
13151315

1316-
#if FEATURE_COMINTEROP || MONO_COM
1316+
#if FEATURE_COMINTEROP || MONO_COM || UNITY_AOT
13171317
// Protected routine to determine if this class represents a Windows Runtime object
13181318
virtual internal bool IsWindowsRuntimeObjectImpl() {
13191319
throw new NotImplementedException();

0 commit comments

Comments
 (0)