public static int CoCreateInstance( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr rReturnedComObject ) { rReturnedComObject = IntPtr.Zero; return -1; }
/// <summary> /// Creates a COM object with the specified interface. /// </summary> /// <param name="iid">The interface identifier. This parameter is a reference to a GUID that identifies the interface that the caller requests be activated. The caller will use this interface to communicate with the COM object.</param> /// <param name="context">The execution context in which the code that manages the newly created object will run. </param> /// <param name="activationParams">Use <see cref="IntPtr.Zero"/> as the default value. See http://msdn.microsoft.com/en-us/library/windows/desktop/dd371405%28v=vs.85%29.aspx for more details.</param> /// <returns>A pointer variable into which the method writes the address of the interface specified by parameter <paramref name="iid"/>.</returns> public IntPtr Activate(Guid iid, CLSCTX context, IntPtr activationParams) { IntPtr ptr; CoreAudioAPIException.Try(ActivateNative(iid, context, activationParams, out ptr), InterfaceName, "Activate"); return ptr; }
public object CreateClassFactory(CLSCTX dwContext, string server) { return(COMUtilities.CreateClassFactory(Clsid, COMInterfaceEntry.IID_IUnknown, dwContext, server)); }
public static extern HResult CoCreateInstance(Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, Guid riid, out IntPtr ppv);
public object GetInstance(InstanceContext instanceContext, Message message) { object result = null; Guid incomingTransactionID = Guid.Empty; if (ContextUtil.IsInTransaction) { incomingTransactionID = ContextUtil.TransactionId; } ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationRequest, SR.TraceCodeComIntegrationInstanceCreationRequest, this.info, message, incomingTransactionID); WindowsIdentity callerIdentity = null; callerIdentity = MessageUtil.GetMessageIdentity(message); WindowsImpersonationContext impersonateContext = null; try { try { if (this.info.HostingMode == HostingMode.WebHostOutOfProcess) { if (SecurityUtils.IsAtleastImpersonationToken(new SafeCloseHandle(callerIdentity.Token, false))) { impersonateContext = callerIdentity.Impersonate(); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(SR.GetString(SR.BadImpersonationLevelForOutOfProcWas), HR.ERROR_BAD_IMPERSONATION_LEVEL)); } } CLSCTX clsctx = CLSCTX.SERVER; if (PlatformSupportsBitness && (this.info.HostingMode == HostingMode.WebHostOutOfProcess)) { if (this.info.Bitness == Bitness.Bitness32) { clsctx |= CLSCTX.ACTIVATE_32_BIT_SERVER; } else { clsctx |= CLSCTX.ACTIVATE_64_BIT_SERVER; } } result = SafeNativeMethods.CoCreateInstance( info.Clsid, null, clsctx, IID_IUnknown); } finally { if (impersonateContext != null) { impersonateContext.Undo(); } } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } Uri from = null; if (message.Headers.From != null) { from = message.Headers.From.Uri; } DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus, (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInstanceCreationError, from == null ? string.Empty : from.ToString(), this.info.AppID.ToString(), this.info.Clsid.ToString(), incomingTransactionID.ToString(), callerIdentity.Name, e.ToString()); throw TraceUtility.ThrowHelperError(e, message); } TransactionProxy proxy = instanceContext.Extensions.Find <TransactionProxy>(); if (proxy != null) { proxy.InstanceID = result.GetHashCode(); } ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationSuccess, SR.TraceCodeComIntegrationInstanceCreationSuccess, this.info, message, result.GetHashCode(), incomingTransactionID); return(result); }
public object CreateInstanceAsObject(CLSCTX dwContext, string server) { return(CreateInstance(server, false)); }
static extern object CoCreateInstance( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsContext, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
private COMEnumerateInterfaces(Guid clsid, CLSCTX clsctx, List<COMInterfaceInstance> interfaces, List<COMInterfaceInstance> factory_interfaces) { _interfaces = interfaces; _factory_interfaces = factory_interfaces; _clsid = clsid; _clsctx = clsctx; }
static extern void CoCreateInstanceEx( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, COSERVERINFO pServerInfo, uint cmq, [In, Out] MULTI_QI[] pResults);
public object GetInstance(InstanceContext instanceContext, Message message) { object obj2 = null; Guid empty = Guid.Empty; if (ContextUtil.IsInTransaction) { empty = ContextUtil.TransactionId; } ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, 0x50012, "TraceCodeComIntegrationInstanceCreationRequest", this.info, message, empty); WindowsIdentity messageIdentity = null; messageIdentity = MessageUtil.GetMessageIdentity(message); WindowsImpersonationContext context = null; try { try { if (this.info.HostingMode == HostingMode.WebHostOutOfProcess) { if (!System.ServiceModel.ComIntegration.SecurityUtils.IsAtleastImpersonationToken(new SafeCloseHandle(messageIdentity.Token, false))) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(System.ServiceModel.SR.GetString("BadImpersonationLevelForOutOfProcWas"), HR.ERROR_BAD_IMPERSONATION_LEVEL)); } context = messageIdentity.Impersonate(); } CLSCTX sERVER = CLSCTX.SERVER; if (PlatformSupportsBitness && (this.info.HostingMode == HostingMode.WebHostOutOfProcess)) { if (this.info.Bitness == Bitness.Bitness32) { sERVER |= CLSCTX.ACTIVATE_32_BIT_SERVER; } else { sERVER |= CLSCTX.ACTIVATE_64_BIT_SERVER; } } obj2 = System.ServiceModel.ComIntegration.SafeNativeMethods.CoCreateInstance(this.info.Clsid, null, sERVER, IID_IUnknown); } finally { if (context != null) { context.Undo(); } } } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } Uri uri = null; if (message.Headers.From != null) { uri = message.Headers.From.Uri; } System.ServiceModel.DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId)(-1073610726), new string[] { (uri == null) ? string.Empty : uri.ToString(), this.info.AppID.ToString(), this.info.Clsid.ToString(), empty.ToString(), messageIdentity.Name, exception.ToString() }); throw TraceUtility.ThrowHelperError(exception, message); } System.ServiceModel.ComIntegration.TransactionProxy proxy = instanceContext.Extensions.Find <System.ServiceModel.ComIntegration.TransactionProxy>(); if (proxy != null) { proxy.InstanceID = obj2.GetHashCode(); } ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, 0x50013, "TraceCodeComIntegrationInstanceCreationSuccess", this.info, message, obj2.GetHashCode(), empty); return(obj2); }
private static extern Result CoCreateInstanceFromApp([In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, IntPtr reserved, int countMultiQuery, ref MultiQueryInterface query);
private static extern int CoCreateInstance(ref Guid clsid, IntPtr pUnkOuter, CLSCTX dwClsContext, ref Guid iid, out IntPtr ptrIf);
/// <summary>Initializes a new instance of the <see cref="ComClassFactory"/> class.</summary> /// <param name="punkObject">The COM object that is to be registered as a class object and queried for interfaces.</param> /// <param name="classContext">The context within which the COM object is to be run.</param> /// <param name="classUse">Indicates how connections are made to the class object.</param> public ComClassFactory(IComObject punkObject, CLSCTX classContext, REGCLS classUse) { comObj = punkObject ?? throw new ArgumentNullException(nameof(punkObject)); CoRegisterClassObject(comObj.GetType().CLSID(), this, classContext, classUse, out registrationId).ThrowIfFailed(); }
public static extern HRESULT CoCreateInstance( ref Guid rclsid, IntPtr punkOuter, CLSCTX dwClsContext, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);
public static extern void CoCreateInstance( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsContext, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);
public static extern int CoRegisterClassObject( ref Guid rclsid, [MarshalAs(UnmanagedType.Interface)] IClassFactory pUnk, CLSCTX dwClsContext, REGCLS flags, out uint lpdwRegister);
static extern void CoGetInstanceFromFile( COSERVERINFO pServerInfo, ref Guid pClsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, STGM grfMode, string pwszName, uint cmq, [In, Out] MULTI_QI[] rgmqResults);
internal static extern HResult CoCreateInstance([MarshalAs(UnmanagedType.LPStruct)] [In] Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, [MarshalAs(UnmanagedType.LPStruct)] [In] Guid riid, out IntPtr comObject);
static extern void CoGetInstanceFromIStorage(COSERVERINFO pServerInfo, ref Guid pclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, IStorage pstg, uint cmq, [In, Out] MULTI_QI[] rgmqResults);
internal static extern int CoCreateInstanceEx([In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, IntPtr pServerInfo, uint cmq, [In, Out] MULTI_QI[] pResults);
public static extern int CoCreateInstance( ref Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, ref Guid riid, out IntPtr ppv);
public object CreateClassFactory(CLSCTX dwContext, string server) { return(CreateInstance(server, true)); }
public static extern object CoGetClassObject( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, CLSCTX dwClsContext, IntPtr pServerInfo, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
public static extern uint CoCreateInstance( [In] ref Guid rclsid, IntPtr pUnkOuter, [MarshalAs(UnmanagedType.I4)] CLSCTX dwClsContext, [In] ref Guid riid, [Out] out IntPtr ppv);
public static extern HRESULT CoGetClassObject( ref Guid rclsid, CLSCTX dwContext, IntPtr pvReserved, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IClassFactory2 ppv);
public static extern object CoCreateInstance( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsContext, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
public IntPtr CreateInstance(CLSCTX dwContext, string server) { IntPtr pInterface = IntPtr.Zero; if (dwContext == CLSCTX.ALL) { if (DefaultServerType == COMServerType.InProcServer32) { dwContext = CLSCTX.INPROC_SERVER; } else if (DefaultServerType == COMServerType.LocalServer32) { dwContext = CLSCTX.LOCAL_SERVER; } else if (DefaultServerType == COMServerType.InProcHandler32) { dwContext = CLSCTX.INPROC_HANDLER; } else { dwContext = CLSCTX.SERVER; } } Guid iid = COMInterfaceEntry.CreateKnownInterface(COMInterfaceEntry.KnownInterfaces.IUnknown).Iid; Guid clsid = Clsid; int hr = 0; if (server != null) { MULTI_QI[] qis = new MULTI_QI[1]; qis[0] = new MULTI_QI(iid); COSERVERINFO server_info = new COSERVERINFO(server); try { hr = COMUtilities.CoCreateInstanceEx(ref clsid, IntPtr.Zero, dwContext, server_info, 1, qis); if (hr == 0) { hr = qis[0].HResult(); if (hr == 0) { pInterface = qis[0].GetObjectPointer(); } } } finally { ((IDisposable)qis[0]).Dispose(); } } else { hr = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, dwContext, ref iid, out pInterface); } if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } return(pInterface); }
/// <summary> /// Creates a COM object with the specified interface. /// </summary> /// <param name="iid">The interface identifier. This parameter is a reference to a GUID that identifies the interface that the caller requests be activated. The caller will use this interface to communicate with the COM object.</param> /// <param name="context">The execution context in which the code that manages the newly created object will run. </param> /// <param name="activationParams">Use <see cref="IntPtr.Zero"/> as the default value. See http://msdn.microsoft.com/en-us/library/windows/desktop/dd371405%28v=vs.85%29.aspx for more details.</param> /// <param name="pinterface">A pointer variable into which the method writes the address of the interface specified by parameter <paramref name="iid"/>.</param> /// <returns>HRESULT</returns> public unsafe int ActivateNative(Guid iid, CLSCTX context, IntPtr activationParams, out IntPtr pinterface) { pinterface = IntPtr.Zero; fixed (void* ppinterface = &pinterface) { var result = InteropCalls.CallI(UnsafeBasePtr, &iid, (uint) context, activationParams, new IntPtr(ppinterface), ((void**) (*(void**) UnsafeBasePtr))[3]); return result; } }
private void GetInterfacesInternal(NtToken token) { IntPtr punk = IntPtr.Zero; IntPtr pfactory = IntPtr.Zero; Guid IID_IUnknown = COMInterfaceEntry.IID_IUnknown; CLSCTX clsctx = _clsctx; if (token != null) { clsctx |= CLSCTX.ENABLE_CLOAKING; } using (var imp = token?.Impersonate()) { int hr = 0; if (_winrt_component) { hr = COMUtilities.RoGetActivationFactory(_activatable_classid, ref IID_IUnknown, out pfactory); } else { hr = COMUtilities.CoGetClassObject(ref _clsid, clsctx, null, ref IID_IUnknown, out pfactory); } // If we can't get class object, no chance we'll get object. if (hr != 0) { throw new Win32Exception(hr); } if (_winrt_component) { hr = COMUtilities.RoActivateInstance(_activatable_classid, out punk); } else { hr = COMUtilities.CoCreateInstance(ref _clsid, IntPtr.Zero, clsctx, ref IID_IUnknown, out punk); } if (hr != 0) { punk = IntPtr.Zero; } try { Guid[] additional_iids = new[] { COMInterfaceEntry.IID_IMarshal, COMInterfaceEntry.IID_IPSFactoryBuffer, COMInterfaceEntry.IID_IStdMarshalInfo, COMInterfaceEntry.IID_IMarshal2 }; Dictionary <IntPtr, string> module_names = new Dictionary <IntPtr, string>(); foreach (var iid in additional_iids) { QueryInterface(punk, iid, module_names, _interfaces); QueryInterface(pfactory, iid, module_names, _factory_interfaces); } var actctx = ActivationContext.FromProcess(); if (actctx != null) { foreach (var intf in actctx.ComInterfaces) { QueryInterface(punk, intf.Iid, module_names, _interfaces); QueryInterface(pfactory, intf.Iid, module_names, _factory_interfaces); } } using (RegistryKey interface_key = Registry.ClassesRoot.OpenSubKey("Interface")) { foreach (string iid_string in interface_key.GetSubKeyNames()) { if (Guid.TryParse(iid_string, out Guid iid)) { QueryInterface(punk, iid, module_names, _interfaces); QueryInterface(pfactory, iid, module_names, _factory_interfaces); } } } QueryInspectableInterfaces(punk, module_names, _interfaces); QueryInspectableInterfaces(pfactory, module_names, _factory_interfaces); } finally { if (pfactory != IntPtr.Zero) { Marshal.Release(pfactory); } if (punk != IntPtr.Zero) { Marshal.Release(punk); } } } }
public COMEnumerateInterfaces(Guid clsid, CLSCTX clsctx, string activatable_classid, bool sta, int timeout, NtToken token) : this(clsid, clsctx, activatable_classid, new List <COMInterfaceInstance>(), new List <COMInterfaceInstance>()) { GetInterfaces(sta, timeout, token); }
/// <summary>Initializes a new instance of the <see cref="ShellExecuteCommand"/> class.</summary> /// <param name="classContext">The context within which the COM object is to be run.</param> /// <param name="classUse">Indicates how connections are made to the class object.</param> protected ShellExecuteCommand(CLSCTX classContext, REGCLS classUse) : base(classContext, classUse) { }
public COMEnumerateInterfaces(Guid clsid, CLSCTX clsctx, bool sta, int timeout) : this(clsid, clsctx, new List<COMInterfaceInstance>(), new List<COMInterfaceInstance>()) { GetInterfaces(sta, timeout); }
public static extern int CoGetInstanceFromIStorage(COSERVERINFO pServerInfo, ref Guid pclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, IStorage pstg, uint cmq, [In, Out] MULTI_QI[] rgmqResults);
public static extern int CoGetClassObject(ref Guid rclsid, CLSCTX dwClsContext, IntPtr pServerInfo, ref Guid riid, out IntPtr ppv);
internal static extern HResult CoCreateInstance([MarshalAs(UnmanagedType.LPStruct)][In] Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, [MarshalAs(UnmanagedType.LPStruct)][In] Guid riid, out IntPtr comObject);
private static extern int CoCreateInstance( ref Guid clsid, IntPtr pUnkOuter, CLSCTX dwClsContext, ref Guid iid, out IntPtr ptrIf );
public COMEnumerateInterfaces(Guid clsid, CLSCTX clsctx, bool sta, int timeout) : this(clsid, clsctx, new List <COMInterfaceInstance>(), new List <COMInterfaceInstance>()) { GetInterfaces(sta, timeout); }
public static extern int CoCreateInstance(ref Guid rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, ref Guid riid, out IntPtr ppv);
public async Task CreateInstanceFromCLSID(Guid clsid, CLSCTX clsctx, bool class_factory) { try { COMCLSIDEntry ent = null; Dictionary <string, string> props = new Dictionary <string, string>(); object comObj = null; string strObjName = ""; IEnumerable <COMInterfaceEntry> ints = null; if (m_registry.Clsids.ContainsKey(clsid)) { ent = m_registry.Clsids[clsid]; strObjName = ent.Name; props.Add("CLSID", ent.Clsid.FormatGuid()); props.Add("Name", ent.Name); props.Add("Server", ent.DefaultServer); await ent.LoadSupportedInterfacesAsync(false); if (class_factory) { comObj = ent.CreateClassFactory(); ints = ent.FactoryInterfaces.Select(i => m_registry.MapIidToInterface(i.Iid)); } else { comObj = ent.CreateInstanceAsObject(clsctx, null); ints = ent.Interfaces.Select(i => m_registry.MapIidToInterface(i.Iid)); } } else { Guid unk = COMInterfaceEntry.IID_IUnknown; IntPtr pObj; int hr; if (class_factory) { hr = COMUtilities.CoGetClassObject(ref clsid, clsctx, null, ref unk, out pObj); } else { hr = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, clsctx, ref unk, out pObj); } if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } try { ints = m_registry.GetInterfacesForIUnknown(pObj).ToArray(); comObj = Marshal.GetObjectForIUnknown(pObj); strObjName = clsid.FormatGuid(); props.Add("CLSID", clsid.FormatGuid()); } finally { Marshal.Release(pObj); } } if (comObj != null) { /* Need to implement a type library reader */ Type dispType = COMUtilities.GetDispatchTypeInfo(this, comObj); HostControl(new ObjectInformation(m_registry, ent, strObjName, comObj, props, ints.ToArray())); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }