Skip to content

Commit

Permalink
Fix broken links documentation (microsoft#619)
Browse files Browse the repository at this point in the history
* Remove links, add example code

* fix link
  • Loading branch information
j0shuams authored Dec 7, 2020
1 parent 9162cec commit bd1fd97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion docs/interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,23 @@ SetString(MarshalString.GetAbi(marshalStr));
**Note:** The CLR still supports marshaling COM (IUnknown), but not WinRT (IInspectable), interop interfaces. There are two approaches to marshaling IInspectable interfaces in C#/WinRT.

##### Projected
If possible, the interop interface should be defined in IDL and a C#/WinRT projection produced for it. This automatically generates all marshaling logic so that calling code can pass and receive projected types. For an example of this, see the [IUserConsentVerifierInterop definition](https://github.com/microsoft/CsWinRT/blob/master/TestComponentCSharp/TestComponentCSharp.idl#L356) and [related test code](https://github.com/microsoft/CsWinRT/blob/master/UnitTest/TestComponentCSharp_Tests.cs#L1039).
If possible, the interop interface should be defined in IDL and a C#/WinRT projection produced for it. This automatically generates all marshaling logic so that calling code can pass and receive projected types. This definition of `IUserConsentVerifierInterop` from one of our test components is an example of this:

```csharp
// IInspectable-based interop interface
[uuid(39E050C3-4E74-441A-8DC0-B81104DF949C)]
interface IUserConsentVerifierInterop
{
Windows.Foundation.IAsyncOperation<Windows.Security.Credentials.UI.UserConsentVerificationResult> RequestVerificationForWindowAsync(UInt64 appWindow, String message, GUID riid);
}
```

And here is an example of using it -- from the test `TestFactoryCast` in our file `TestComponentCSharp_Tests`:

```csharp
// IInspectable-based (projected) interop interface
var interop = Windows.Security.Credentials.UI.UserConsentVerifier.As<IUserConsentVerifierInterop>();
```

##### ComImport
Another technique is to define the interface in C#, with the ComImport attribute attached. This uses the CLR's support for marshaling COM (IUnknown) interfaces, which is still supported. This technique can be adapted to WinRT (IInspectable) interfaces, with a minor change. Normally, defining the interface with ComInterfaceType.InterfaceIsIInspectable will succeed in the cast operation, but method calls will crash in the CLR. Instead, define the interface with ComInterfaceType.InterfaceIsIUnknown and explicitly add the IInspectable methods - GetIids, GetRuntimeClassName, and GetTrustLevel:
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Component Project

A component project adds a NuGet reference to C#/WinRT to invoke cswinrt.exe at build time, generate projection sources, and compile these into an interop assembly. For an example of this, see the [Projection Sample](https://github.com/microsoft/CsWinRT/tree/master/Samples/Net5ProjectionSample). Command line options can be displayed by running **cswinrt -?**. The interop assembly is then typically distributed as a NuGet package itself.
A component project adds a NuGet reference to C#/WinRT to invoke cswinrt.exe at build time, generate projection sources, and compile these into an interop assembly. For an example of this, see the [Projection Sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/Net5ProjectionSample). Command line options can be displayed by running **cswinrt -?**. The interop assembly is then typically distributed as a NuGet package itself.

### Example

Expand Down

0 comments on commit bd1fd97

Please sign in to comment.