Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'CoCreateInstance' helper #1717

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/WinRT.Runtime/IInspectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ enum TrustLevel
FullTrust = PartialTrust + 1
}

/// <summary>
/// Values that are used in activation calls to indicate the execution contexts in which an object is to be run.
/// </summary>
public enum ClassContext : uint
{
/// <summary>
/// The code that creates and manages objects of this class is a DLL that runs in the same process as the caller of the function specifying the class context.
/// </summary>
InProcServer = 0x1,

/// <summary>
/// The EXE code that creates and manages objects of this class runs on same machine but is loaded in a separate process space.
/// </summary>
LocalServer = 0x4
}

// IInspectable
#if !NET
[global::WinRT.ObjectReferenceWrapper(nameof(_obj))]
Expand Down Expand Up @@ -174,6 +190,45 @@ public unsafe string GetRuntimeClassName(bool noThrow = false)
Platform.WindowsDeleteString(__retval);
}
}
}

/// <summary>
/// Activates and marshals a projected WinRT type that does not participate in WinRT activation, by using <c>CoCreateInstance</c>.
/// This method can be used to activate WinRT types from out-of-proc COM servers (eg. Windows Package Manager types).
/// </summary>
/// <typeparam name="T">The type of the projected WinRT object to activate and marshal.</typeparam>
/// <param name="classId">The CLSID associated with the data and code that will be used to create the object.</param>
/// <param name="interfaceId">The identifier of the interface to be used to communicate with the object.</param>
/// <param name="classContext">The context in which the code that manages the newly created object will run.</param>
/// <returns>The resulting marshalled instance.</returns>
public static unsafe T CoCreateInstance<T>(in Guid classId, in Guid interfaceId, ClassContext classContext)
where T : class
{
IntPtr thisPtr = IntPtr.Zero;

try
{
fixed (Guid* rclsid = &classId)
fixed (Guid* riid = &interfaceId)
{
int hresult = Platform.CoCreateInstance(
clsid: rclsid,
outer: IntPtr.Zero,
clsContext: (uint)classContext,
iid: riid,
instance: &thisPtr);

Marshal.ThrowExceptionForHR(hresult);
}

return MarshalInspectable<T>.FromAbi(thisPtr);
}
finally
{
if (thisPtr != IntPtr.Zero)
{
Marshal.Release(thisPtr);
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,6 @@ TypesMustExist : Type 'Windows.UI.Xaml.LayoutCycleException' does not exist in t
TypesMustExist : Type 'Windows.UI.Xaml.Automation.ElementNotAvailableException' does not exist in the reference but it does exist in the implementation.
TypesMustExist : Type 'Windows.UI.Xaml.Automation.ElementNotEnabledException' does not exist in the reference but it does exist in the implementation.
TypesMustExist : Type 'Windows.UI.Xaml.Markup.XamlParseException' does not exist in the reference but it does exist in the implementation.
Total Issues: 278
TypesMustExist : Type 'WinRT.ClassContext' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public T WinRT.IInspectable.CoCreateInstance<T>(System.Guid, System.Guid, WinRT.ClassContext)' does not exist in the reference but it does exist in the implementation.
Total Issues: 280