[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SetLastWin32Error(int error);
- public static int GetHRForLastWin32Error()
- {
- int dwLastError = GetLastWin32Error();
- if ((dwLastError & 0x80000000) == 0x80000000)
- {
- return dwLastError;
- }
-
- return (dwLastError & 0x0000FFFF) | unchecked((int)0x80070000);
- }
-
private static void PrelinkCore(MethodInfo m)
{
if (!(m is RuntimeMethodInfo rmi))
#endif // FEATURE_COMINTEROP
- /// <summary>
- /// Throws a CLR exception based on the HRESULT.
- /// </summary>
- public static void ThrowExceptionForHR(int errorCode)
- {
- if (errorCode < 0)
- {
- ThrowExceptionForHRInternal(errorCode, IntPtr.Zero);
- }
- }
-
- public static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo)
- {
- if (errorCode < 0)
- {
- ThrowExceptionForHRInternal(errorCode, errorInfo);
- }
- }
-
- [MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern void ThrowExceptionForHRInternal(int errorCode, IntPtr errorInfo);
-
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo);
return pNewMem;
}
- public static IntPtr AllocHGlobal(int cb) => AllocHGlobal((IntPtr)cb);
-
public static void FreeHGlobal(IntPtr hglobal)
{
if (!IsWin32Atom(hglobal))
/// </summary>
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr /* IUnknown* */ GetRawIUnknownForComObjectNoAddRef(object o);
-#endif // FEATURE_COMINTEROP
- public static IntPtr /* IDispatch */ GetIDispatchForObject(object o) => throw new PlatformNotSupportedException();
-
-#if FEATURE_COMINTEROP
/// <summary>
/// Return the IUnknown* representing the interface for the Object.
/// Object o should support Type T
/// </summary>
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetEndComSlot(Type t);
-#endif // FEATURE_COMINTEROP
-
- /// <summary>
- /// This method generates a PROGID for the specified type. If the type has
- /// a PROGID in the metadata then it is returned otherwise a stable PROGID
- /// is generated based on the fully qualified name of the type.
- /// </summary>
- public static string GenerateProgIdForType(Type type)
- {
- if (type == null)
- {
- throw new ArgumentNullException(nameof(type));
- }
- if (type.IsImport)
- {
- throw new ArgumentException(SR.Argument_TypeMustNotBeComImport, nameof(type));
- }
- if (type.IsGenericType)
- {
- throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
- }
- IList<CustomAttributeData> cas = CustomAttributeData.GetCustomAttributes(type);
- for (int i = 0; i < cas.Count; i++)
- {
- if (cas[i].Constructor.DeclaringType == typeof(ProgIdAttribute))
- {
- // Retrieve the PROGID string from the ProgIdAttribute.
- IList<CustomAttributeTypedArgument> caConstructorArgs = cas[i].ConstructorArguments;
- Debug.Assert(caConstructorArgs.Count == 1, "caConstructorArgs.Count == 1");
-
- CustomAttributeTypedArgument progIdConstructorArg = caConstructorArgs[0];
- Debug.Assert(progIdConstructorArg.ArgumentType == typeof(string), "progIdConstructorArg.ArgumentType == typeof(String)");
-
- string strProgId = (string)progIdConstructorArg.Value;
-
- if (strProgId == null)
- strProgId = string.Empty;
-
- return strProgId;
- }
- }
-
- // If there is no prog ID attribute then use the full name of the type as the prog id.
- return type.FullName;
- }
-
-#if FEATURE_COMINTEROP
public static object BindToMoniker(string monikerName)
{
CreateBindCtx(0, out IBindCtx bindctx);
FCFuncElement("OffsetOfHelper", MarshalNative::OffsetOfHelper)
QCFuncElement("InternalPrelink", MarshalNative::Prelink)
- FCFuncElement("ThrowExceptionForHRInternal", MarshalNative::ThrowExceptionForHR)
FCFuncElement("GetExceptionForHRInternal", MarshalNative::GetExceptionForHR)
FCFuncElement("GetDelegateForFunctionPointerInternal", MarshalNative::GetDelegateForFunctionPointerInternal)
FCFuncElement("GetFunctionPointerForDelegateInternal", MarshalNative::GetFunctionPointerForDelegateInternal)
return OBJECTREFToObject(RetExceptionObj);
}
FCIMPLEND
-
-FCIMPL2(void, MarshalNative::ThrowExceptionForHR, INT32 errorCode, LPVOID errorInfo)
-{
- CONTRACTL
- {
- FCALL_CHECK;
- PRECONDITION(FAILED(errorCode));
- PRECONDITION(CheckPointer(errorInfo, NULL_OK));
- }
- CONTRACTL_END;
-
-
- HELPER_METHOD_FRAME_BEGIN_0();
-
- // 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;
- }
-
- // Throw the exception based on the HR and the IErrorInfo.
- COMPlusThrowHR(errorCode, pErrorInfo);
-
- HELPER_METHOD_FRAME_END();
-}
-FCIMPLEND
-
FCIMPL1(int, MarshalNative::GetHRForException, Object* eUNSAFE)
{
//====================================================================
// These methods convert between an HR and and a managed exception.
//====================================================================
- static FCDECL2(void, ThrowExceptionForHR, INT32 errorCode, LPVOID errorInfo);
static FCDECL2(Object *, GetExceptionForHR, INT32 errorCode, LPVOID errorInfo);
static FCDECL1(int, GetHRForException, Object* eUNSAFE);
static FCDECL1(int, GetHRForException_WinRT, Object* eUNSAFE);
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Collections.Generic;
+using System.Diagnostics;
using System.Security;
using System.Reflection;
using System.Runtime.CompilerServices;
/// </summary>
public static readonly int SystemMaxDBCSCharSize = GetSystemMaxDBCSCharSize();
+ public static IntPtr AllocHGlobal(int cb) => AllocHGlobal((IntPtr)cb);
+
public static unsafe string PtrToStringAnsi(IntPtr ptr)
{
if (ptr == IntPtr.Zero || IsWin32Atom(ptr))
return GetExceptionForHRInternal(errorCode, errorInfo);
}
+ /// <summary>
+ /// Throws a CLR exception based on the HRESULT.
+ /// </summary>
+ public static void ThrowExceptionForHR(int errorCode)
+ {
+ if (errorCode < 0)
+ {
+ throw GetExceptionForHR(errorCode, IntPtr.Zero);
+ }
+ }
+
+ public static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo)
+ {
+ if (errorCode < 0)
+ {
+ throw GetExceptionForHR(errorCode, errorInfo);
+ }
+ }
+
public static IntPtr SecureStringToBSTR(SecureString s)
{
if (s == null)
return type.GUID;
}
+ /// <summary>
+ /// This method generates a PROGID for the specified type. If the type has
+ /// a PROGID in the metadata then it is returned otherwise a stable PROGID
+ /// is generated based on the fully qualified name of the type.
+ /// </summary>
+ public static string GenerateProgIdForType(Type type)
+ {
+ if (type == null)
+ {
+ throw new ArgumentNullException(nameof(type));
+ }
+ if (type.IsImport)
+ {
+ throw new ArgumentException(SR.Argument_TypeMustNotBeComImport, nameof(type));
+ }
+ if (type.IsGenericType)
+ {
+ throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
+ }
+
+ foreach (CustomAttributeData cad in type.GetCustomAttributesData())
+ {
+ if (cad.Constructor.DeclaringType == typeof(ProgIdAttribute))
+ {
+ // Retrieve the PROGID string from the ProgIdAttribute.
+ IList<CustomAttributeTypedArgument> caConstructorArgs = cad.ConstructorArguments;
+ Debug.Assert(caConstructorArgs.Count == 1, "caConstructorArgs.Count == 1");
+
+ CustomAttributeTypedArgument progIdConstructorArg = caConstructorArgs[0];
+ Debug.Assert(progIdConstructorArg.ArgumentType == typeof(string), "progIdConstructorArg.ArgumentType == typeof(string)");
+
+ string strProgId = (string)progIdConstructorArg.Value;
+
+ return strProgId ?? string.Empty;
+ }
+ }
+
+ // If there is no prog ID attribute then use the full name of the type as the prog id.
+ return type.FullName;
+ }
+
public static Delegate GetDelegateForFunctionPointer(IntPtr ptr, Type t)
{
if (ptr == IntPtr.Zero)
{
return GetFunctionPointerForDelegate((Delegate)(object)d);
}
+
+ public static int GetHRForLastWin32Error()
+ {
+ int dwLastError = GetLastWin32Error();
+ if ((dwLastError & 0x80000000) == 0x80000000)
+ {
+ return dwLastError;
+ }
+
+ return (dwLastError & 0x0000FFFF) | unchecked((int)0x80070000);
+ }
+
+ public static IntPtr /* IDispatch */ GetIDispatchForObject(object o) => throw new PlatformNotSupportedException();
}
-}
\ No newline at end of file
+}