Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove FCall in interop code
  • Loading branch information
AaronRobinsonMSFT committed Jan 12, 2024
commit 777caa777614c9ad6432bb4dd53946b2c60fa251
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Microsoft.Win32
{
internal static class OAVariantLib
internal static unsafe partial class OAVariantLib
{
#region Constants

Expand Down Expand Up @@ -78,12 +79,19 @@ internal static Variant ChangeType(Variant source, Type targetClass, short optio
ArgumentNullException.ThrowIfNull(culture);

Variant result = default;
ChangeTypeEx(ref result, ref source,
culture.LCID,
targetClass.TypeHandle.Value, GetCVTypeFromClass(targetClass), options);
ChangeType(
Unsafe.AsPointer<Variant>(ref result),
Unsafe.AsPointer<Variant>(ref source),
culture.LCID,
targetClass.TypeHandle.Value,
GetCVTypeFromClass(targetClass),
options);
return result;
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "OAVariant_ChangeType")]
private static partial void ChangeType(void* result, void* source, int lcid, IntPtr typeHandle, int cvType, short flags);

#endregion


Expand All @@ -94,7 +102,10 @@ private static int GetCVTypeFromClass(Type ctype)
Debug.Assert(ctype != null);
Debug.Assert(ClassTypes[CV_OBJECT] == typeof(object), "OAVariantLib::ClassTypes[CV_OBJECT] == Object.class");

int cvtype = -1;
// OleAut Binder works better if unrecognized
// types were changed to Object.
int cvtype = CV_OBJECT;

for (int i = 0; i < ClassTypes.Length; i++)
{
if (ctype.Equals(ClassTypes[i]))
Expand All @@ -104,22 +115,9 @@ private static int GetCVTypeFromClass(Type ctype)
}
}

// OleAut Binder works better if unrecognized
// types were changed to Object. So don't throw here.
if (cvtype == -1)
cvtype = CV_OBJECT;

return cvtype;
}

#endregion


#region Private FCalls

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void ChangeTypeEx(ref Variant result, ref Variant source, int lcid, IntPtr typeHandle, int cvType, short flags);

#endregion
}
}
2 changes: 1 addition & 1 deletion src/coreclr/System.Private.CoreLib/src/System/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal struct Variant
// What are the consequences of making this an enum?
///////////////////////////////////////////////////////////////////////
// If you update this, update the corresponding stuff in OAVariantLib.cs,
// COMOAVariant.cpp (2 tables, forwards and reverse), and perhaps OleVariant.h
// OAVariant.cpp (2 tables, forwards and reverse), and perhaps OleVariant.h
///////////////////////////////////////////////////////////////////////
internal const int CV_EMPTY = 0x0;
internal const int CV_VOID = 0x1;
Expand Down
Loading