-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to support the interop between Win32 and WinRT objects, I've added a custom marshaler that gets brought it when needed. I also added a new test project to demonstrate WinRT interop.
- Loading branch information
Showing
10 changed files
with
562 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Microsoft.Windows.CsWin32/templates/WinRTCustomMarshaler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
namespace Windows.Win32.CsWin32.InteropServices | ||
{ | ||
internal class WinRTCustomMarshaler : global::System.Runtime.InteropServices.ICustomMarshaler | ||
{ | ||
private string winrtClassName; | ||
private bool lookedForFromAbi; | ||
private global::System.Reflection.MethodInfo fromAbi; | ||
|
||
private WinRTCustomMarshaler(string cookie) | ||
{ | ||
this.winrtClassName = cookie; | ||
} | ||
|
||
/// <summary> | ||
/// Gets an instance of the marshaler given a cookie | ||
/// </summary> | ||
/// <param name="cookie">Cookie used to create marshaler</param> | ||
/// <returns>Marshaler</returns> | ||
public static global::System.Runtime.InteropServices.ICustomMarshaler GetInstance(string cookie) | ||
{ | ||
return new WinRTCustomMarshaler(cookie); | ||
} | ||
|
||
void global::System.Runtime.InteropServices.ICustomMarshaler.CleanUpManagedData(object ManagedObj) | ||
{ | ||
} | ||
|
||
void global::System.Runtime.InteropServices.ICustomMarshaler.CleanUpNativeData(global::System.IntPtr pNativeData) | ||
{ | ||
global::System.Runtime.InteropServices.Marshal.Release(pNativeData); | ||
} | ||
|
||
int global::System.Runtime.InteropServices.ICustomMarshaler.GetNativeDataSize() | ||
{ | ||
throw new global::System.NotImplementedException(); | ||
} | ||
|
||
global::System.IntPtr global::System.Runtime.InteropServices.ICustomMarshaler.MarshalManagedToNative(object ManagedObj) | ||
{ | ||
throw new global::System.NotImplementedException(); | ||
} | ||
|
||
object global::System.Runtime.InteropServices.ICustomMarshaler.MarshalNativeToManaged(global::System.IntPtr pNativeData) | ||
{ | ||
if (!this.lookedForFromAbi) | ||
{ | ||
var assembly = typeof(global::Windows.Foundation.IMemoryBuffer).Assembly; | ||
var type = global::System.Type.GetType($"{this.winrtClassName}, {assembly.FullName}"); | ||
|
||
this.fromAbi = type.GetMethod("FromAbi"); | ||
this.lookedForFromAbi = true; | ||
} | ||
|
||
if (this.fromAbi != null) | ||
{ | ||
return this.fromAbi.Invoke(null, new object[] { pNativeData }); | ||
} | ||
else | ||
{ | ||
return global::System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pNativeData); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.