| | | 1 | | using SolidEdgeCommunity.Runtime.InteropServices.ComTypes; |
| | | 2 | | using System; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | using System.Runtime.InteropServices.ComTypes; |
| | | 6 | | |
| | | 7 | | namespace SolidEdgeCommunity.Runtime.InteropServices; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// COM object wrapper class. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ComObject |
| | | 13 | | { |
| | | 14 | | private const int LOCALE_SYSTEM_DEFAULT = 2048; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Using IDispatch, returns the ITypeInfo of the specified object. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="comObject"></param> |
| | | 20 | | /// <returns></returns> |
| | | 21 | | public static ITypeInfo GetITypeInfo(object comObject) |
| | | 22 | | { |
| | 3 | 23 | | if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException(); |
| | | 24 | | |
| | 1 | 25 | | if (comObject is IDispatch dispatch) |
| | | 26 | | { |
| | 1 | 27 | | return dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | return null; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns a strongly typed property by name using the specified COM object. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="T">The type of the property to return.</typeparam> |
| | | 37 | | /// <param name="comObject"></param> |
| | | 38 | | /// <param name="name">The name of the property to retrieve.</param> |
| | | 39 | | /// <returns></returns> |
| | | 40 | | public static T GetPropertyValue<T>(object comObject, string name) |
| | | 41 | | { |
| | 1 | 42 | | if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException(); |
| | | 43 | | |
| | 1 | 44 | | var type = comObject.GetType(); |
| | 1 | 45 | | var value = type.InvokeMember(name, System.Reflection.BindingFlags.GetProperty, null, comObject, null); |
| | | 46 | | |
| | 1 | 47 | | return (T)value; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Returns a strongly typed property by name using the specified COM object. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <typeparam name="T">The type of the property to return.</typeparam> |
| | | 54 | | /// <param name="comObject"></param> |
| | | 55 | | /// <param name="name">The name of the property to retrieve.</param> |
| | | 56 | | /// <param name="defaultValue">The value to return if the property does not exist.</param> |
| | | 57 | | /// <returns></returns> |
| | | 58 | | public static T GetPropertyValue<T>(object comObject, string name, T defaultValue) |
| | | 59 | | { |
| | 3 | 60 | | if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException(); |
| | | 61 | | |
| | 1 | 62 | | var type = comObject.GetType(); |
| | | 63 | | |
| | | 64 | | try |
| | | 65 | | { |
| | 1 | 66 | | var value = type.InvokeMember(name, System.Reflection.BindingFlags.GetProperty, null, comObject, null); |
| | 0 | 67 | | return (T)value; |
| | | 68 | | } |
| | 1 | 69 | | catch |
| | | 70 | | { |
| | 1 | 71 | | return defaultValue; |
| | | 72 | | } |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Using IDispatch, determine the managed type of the specified object. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="comObject"></param> |
| | | 79 | | /// <returns></returns> |
| | | 80 | | public static Type GetType(object comObject) |
| | | 81 | | { |
| | 3 | 82 | | if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException(); |
| | | 83 | | |
| | 1 | 84 | | Type type = null; |
| | 1 | 85 | | ITypeInfo typeInfo = null; |
| | 1 | 86 | | var pTypeAttr = IntPtr.Zero; |
| | 1 | 87 | | var typeAttr = default(TYPEATTR); |
| | | 88 | | |
| | | 89 | | try |
| | | 90 | | { |
| | 1 | 91 | | if (comObject is IDispatch dispatch) |
| | | 92 | | { |
| | 1 | 93 | | typeInfo = dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT); |
| | 1 | 94 | | typeInfo.GetTypeAttr(out pTypeAttr); |
| | 1 | 95 | | typeAttr = System.Runtime.InteropServices.Marshal.PtrToStructure<TYPEATTR>(pTypeAttr); |
| | | 96 | | |
| | | 97 | | // Type can technically be defined in any loaded assembly. |
| | 1 | 98 | | var assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| | | 99 | | |
| | | 100 | | // Scan each assembly for a type with a matching GUID. |
| | 146 | 101 | | foreach (var assembly in assemblies) |
| | | 102 | | { |
| | 72 | 103 | | type = assembly.GetTypes() |
| | 16884 | 104 | | .Where(x => x.IsInterface) |
| | 2892 | 105 | | .FirstOrDefault(x => x.GUID.Equals(typeAttr.guid)); |
| | | 106 | | |
| | 72 | 107 | | if (type != null) |
| | | 108 | | { |
| | | 109 | | // Found what we're looking for so break out of the loop. |
| | 0 | 110 | | break; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |
| | 1 | 114 | | } |
| | | 115 | | finally |
| | | 116 | | { |
| | 1 | 117 | | if (typeInfo != null) |
| | | 118 | | { |
| | 1 | 119 | | typeInfo.ReleaseTypeAttr(pTypeAttr); |
| | 1 | 120 | | System.Runtime.InteropServices.Marshal.ReleaseComObject(typeInfo); |
| | | 121 | | } |
| | 1 | 122 | | } |
| | | 123 | | |
| | 1 | 124 | | return type; |
| | | 125 | | } |
| | | 126 | | } |