/// <summary> /// A simple create instance, doesn't create a wrapper!! /// </summary> /// <returns>T</returns> public static T CreateInstance <T>() { Type type = typeof(T); if (null == type) { throw new ArgumentNullException(nameof(T)); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", nameof(T)); } ComProgIdAttribute progIdAttribute = ComProgIdAttribute.GetAttribute(type); if (string.IsNullOrEmpty(progIdAttribute?.Value)) { throw new ArgumentException("The specified type must define a ComProgId attribute.", nameof(T)); } string progId = progIdAttribute.Value; Type comType = null; if (progId.StartsWith("clsid:")) { Guid guid = new Guid(progId.Substring(6)); try { comType = Type.GetTypeFromCLSID(guid); } catch (Exception ex) { Log.WarnFormat("Error {1} type for {0}", progId, ex.Message); } } else { try { comType = Type.GetTypeFromProgID(progId, true); } catch (Exception ex) { Log.WarnFormat("Error {1} type for {0}", progId, ex.Message); } } object comObject = null; if (comType != null) { try { comObject = Activator.CreateInstance(comType); if (comObject != null) { Log.DebugFormat("Created new instance of {0} object.", progId); } } catch (Exception e) { Log.WarnFormat("Error {1} creating object for {0}", progId, e.Message); throw; } } if (comObject != null) { return((T)comObject); } return(default(T)); }
/// <summary> /// Gets a COM object and returns the transparent proxy /// which intercepts all calls to the object /// </summary> /// <param name="type">Interface which defines the method and properties to intercept</param> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks>The <paramref name="type"/> must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks> public static object GetOrCreateInstance(Type type) { if (null == type) { throw new ArgumentNullException("type"); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", "type"); } ComProgIdAttribute progID = ComProgIdAttribute.GetAttribute(type); if (null == progID || null == progID.Value || 0 == progID.Value.Length) { throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); } object comObject = null; Type comType = Type.GetTypeFromProgID(progID.Value, true); try { comObject = Marshal.GetActiveObject(progID.Value); } catch (COMException comE) { if (comE.ErrorCode == MK_E_UNAVAILABLE) { LOG.DebugFormat("No current instance of {0} object available.", progID.Value); } else { LOG.Warn("Error getting active object for " + progID.Value, comE); } } catch (Exception e) { LOG.Warn("Error getting active object for " + progID.Value, e); } // Did we get the current instance? If not, try to create a new if (comObject == null) { try { comObject = Activator.CreateInstance(comType); if (comObject != null) { LOG.DebugFormat("Created new instance of {0} object.", progID.Value); } } catch (Exception e) { LOG.Warn("Error creating object for " + progID.Value, e); } } if (comObject != null) { COMWrapper wrapper = new COMWrapper(comObject, type); return(wrapper.GetTransparentProxy()); } throw new TypeLoadException(string.Format("Unable to get or create an instance of the specified COM server \"{0}\".", progID.Value)); }
/// <summary> /// Gets a COM object and returns the transparent proxy which intercepts all calls to the object /// </summary> /// <typeparam name="T">Interface which defines the method and properties to intercept</typeparam> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks>T must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks> public static T GetInstance <T>() { Type type = typeof(T); if (null == type) { throw new ArgumentNullException(nameof(T)); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", nameof(T)); } ComProgIdAttribute progIdAttribute = ComProgIdAttribute.GetAttribute(type); if (string.IsNullOrEmpty(progIdAttribute?.Value)) { throw new ArgumentException("The specified type must define a ComProgId attribute.", nameof(T)); } string progId = progIdAttribute.Value; object comObject = null; try { comObject = Marshal.GetActiveObject(progId); } catch (COMException comE) { if (comE.ErrorCode == MK_E_UNAVAILABLE) { Debug.WriteLine($"No current instance of {progId} object available."); } else if (comE.ErrorCode == CO_E_CLASSSTRING) { Debug.WriteLine($"Unknown progId {progId}"); } } catch (Exception ex) { Debug.WriteLine($"Error getting active object for {progId} {ex.Message}"); } if (comObject != null) { COMWrapper wrapper = new COMWrapper(comObject, type); return((T)wrapper.GetTransparentProxy()); } return(default(T)); }
/// <summary> /// Gets a COM object and returns the transparent proxy which intercepts all calls to the object /// </summary> /// <param name="type">Interface which defines the method and properties to intercept</param> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks>The <paramref name="type"/> must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks> public static T GetInstance <T>() { Type type = typeof(T); if (null == type) { throw new ArgumentNullException("type"); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", "type"); } ComProgIdAttribute progIDAttribute = ComProgIdAttribute.GetAttribute(type); if (null == progIDAttribute || null == progIDAttribute.Value || 0 == progIDAttribute.Value.Length) { throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); } string progId = progIDAttribute.Value; // Convert from clsid to Prog ID, if needed if (progId.StartsWith("clsid:")) { Guid guid = new Guid(progId.Substring(6)); int result = ProgIDFromCLSID(ref guid, out progId); if (result != 0) { //LOG.WarnFormat("Error {0} getting progId {1}", result, progIDAttribute.Value); } else { //LOG.InfoFormat("Mapped {0} to progId {1}", progIDAttribute.Value, progId); } } object comObject = null; try { comObject = Marshal.GetActiveObject(progId); } catch (COMException comE) { if (comE.ErrorCode == MK_E_UNAVAILABLE) { //LOG.DebugFormat("No current instance of {0} object available.", progId); } else if (comE.ErrorCode == CO_E_CLASSSTRING) { //LOG.WarnFormat("Unknown progId {0}", progId); } else { //LOG.Warn("Error getting active object for " + progId, comE); } } catch (Exception) { //LOG.Warn("Error getting active object for " + progId, e); } if (comObject != null) { COMWrapper wrapper = new COMWrapper(comObject, type); return((T)wrapper.GetTransparentProxy()); } return(default(T)); }
/// <summary> /// Gets or creates a COM object and returns the transparent proxy which intercepts all calls to the object /// The ComProgId can be a normal ComProgId or a GUID prefixed with "clsid:" /// </summary> /// <param name="type">Interface which defines the method and properties to intercept</param> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks>The <paramref name="type"/> must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks> public static T GetOrCreateInstance <T>() { Type type = typeof(T); if (null == type) { throw new ArgumentNullException("type"); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", "type"); } ComProgIdAttribute progIDAttribute = ComProgIdAttribute.GetAttribute(type); if (null == progIDAttribute || null == progIDAttribute.Value || 0 == progIDAttribute.Value.Length) { throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); } object comObject = null; Type comType = null; string progId = progIDAttribute.Value; Guid guid = Guid.Empty; // Convert from clsid to Prog ID, if needed if (progId.StartsWith("clsid:")) { guid = new Guid(progId.Substring(6)); int result = ProgIDFromCLSID(ref guid, out progId); if (result != 0) { // Restore progId, as it's overwritten progId = progIDAttribute.Value; try { GetActiveObject(ref guid, IntPtr.Zero, out comObject); } catch (Exception) { LOG.WarnFormat("Error {0} getting instance for class id {1}", result, progIDAttribute.Value); } if (comObject == null) { LOG.WarnFormat("Error {0} getting progId {1}", result, progIDAttribute.Value); } } else { LOG.InfoFormat("Mapped {0} to progId {1}", progIDAttribute.Value, progId); } } if (comObject == null) { if (!progId.StartsWith("clsid:")) { try { comObject = Marshal.GetActiveObject(progId); } catch (COMException comE) { if (comE.ErrorCode == MK_E_UNAVAILABLE) { LOG.DebugFormat("No current instance of {0} object available.", progId); } else if (comE.ErrorCode == CO_E_CLASSSTRING) { LOG.WarnFormat("Unknown progId {0} (application not installed)", progId); return(default(T)); } else { LOG.Warn("Error getting active object for " + progId, comE); } } catch (Exception e) { LOG.Warn("Error getting active object for " + progId, e); } } } // Did we get the current instance? If not, try to create a new if (comObject == null) { try { comType = Type.GetTypeFromProgID(progId, true); } catch (Exception ex) { if (Guid.Empty != guid) { comType = Type.GetTypeFromCLSID(guid); } else { LOG.Warn("Error type for " + progId, ex); } } if (comType != null) { try { comObject = Activator.CreateInstance(comType); if (comObject != null) { LOG.DebugFormat("Created new instance of {0} object.", progId); } } catch (Exception e) { LOG.Warn("Error creating object for " + progId, e); } } } if (comObject != null) { if (comObject is IDispatch) { COMWrapper wrapper = new COMWrapper(comObject, type, progIDAttribute.Value); return((T)wrapper.GetTransparentProxy()); } else { return((T)comObject); } } return(default(T)); }
/// <summary> /// Gets a COM object and returns the transparent proxy which intercepts all calls to the object /// </summary> /// <typeparam name="T">Interface which defines the method and properties to intercept</typeparam> /// <returns>Transparent proxy to the real proxy for the object</returns> /// <remarks>T must be an interface decorated with the <see cref="ComProgIdAttribute"/>attribute.</remarks> public static T GetInstance <T>() { Type type = typeof(T); if (null == type) { throw new ArgumentNullException(nameof(T)); } if (!type.IsInterface) { throw new ArgumentException("The specified type must be an interface.", nameof(T)); } var progIdAttribute = ComProgIdAttribute.GetAttribute(type); if (string.IsNullOrEmpty(progIdAttribute?.Value)) { throw new ArgumentException("The specified type must define a ComProgId attribute.", nameof(T)); } string progId = progIdAttribute.Value; object comObject = null; // Convert from clsid to Prog ID, if needed if (progId.StartsWith("clsid:")) { Guid guid = new Guid(progId.Substring(6)); int result = ProgIDFromCLSID(ref guid, out progId); if (result != 0) { // Restore progId, as it's overwritten progId = progIdAttribute.Value; try { GetActiveObject(ref guid, IntPtr.Zero, out comObject); } catch (Exception) { Log.WarnFormat("Error {0} getting instance for class id {1}", result, progIdAttribute.Value); } if (comObject == null) { Log.WarnFormat("Error {0} getting progId {1}", result, progIdAttribute.Value); } } else { Log.InfoFormat("Mapped {0} to progId {1}", progIdAttribute.Value, progId); } } if (comObject == null) { try { comObject = Marshal.GetActiveObject(progId); } catch (COMException comE) { if (comE.ErrorCode == MK_E_UNAVAILABLE) { Log.DebugFormat("No current instance of {0} object available.", progId); } else if (comE.ErrorCode == CO_E_CLASSSTRING) { Log.WarnFormat("Unknown progId {0}", progId); } else { Log.Warn("Error getting active object for " + progIdAttribute.Value, comE); } } catch (Exception e) { Log.Warn("Error getting active object for " + progIdAttribute.Value, e); } } if (comObject != null) { if (comObject is IDispatch) { COMWrapper wrapper = new COMWrapper(comObject, type, progIdAttribute.Value); return((T)wrapper.GetTransparentProxy()); } else { return((T)comObject); } } return(default(T)); }