/// <summary> /// Creates 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 CreateInstance(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"); } Type comType = Type.GetTypeFromProgID(progID.Value, true); object comObject = Activator.CreateInstance(comType); if (null == comObject) { throw new TypeLoadException( string.Format("Unable to create an instance of the specified COM server \"{0}\".", progID.Value)); } COMWrapper wrapper = new COMWrapper(comObject, type); return(wrapper.GetTransparentProxy()); }
/// <summary> /// get the prog-id corresponding to the given clisid /// </summary> /// <param name="strProgid"></param> /// <returns></returns> private static string getClsidFromProgId(string strProgid) { ComProgIdAttribute progID = new ComProgIdAttribute(strProgid); if (null == progID || null == progID.Value || 0 == progID.Value.Length) throw new ArgumentException("The specified type must define a ComProgId attribute.", "type"); Type comType = Type.GetTypeFromProgID(progID.Value, true); return comType.GUID.ToString("D"); }