-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into orleans-shoppingcart-implement-cosmos
- Loading branch information
Showing
95 changed files
with
939 additions
and
339 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
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
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
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
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
1 change: 1 addition & 0 deletions
1
core/interop/source-generation/ComWrappersGeneration/.gitignore
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 @@ | ||
OutputFiles |
13 changes: 13 additions & 0 deletions
13
core/interop/source-generation/ComWrappersGeneration/Client/Client.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<!-- Uncomment to publish as a native executable --> | ||
<!-- <PublishAOT>true</PublishAOT> --> | ||
</PropertyGroup> | ||
|
||
</Project> |
79 changes: 79 additions & 0 deletions
79
core/interop/source-generation/ComWrappersGeneration/Client/Program.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,79 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.Marshalling; | ||
|
||
[assembly: DisableRuntimeMarshalling] | ||
|
||
namespace Tutorial; | ||
|
||
public class Program | ||
{ | ||
public static unsafe void Main(string[] args) | ||
{ | ||
var clsid = new Guid(Clsids.Calculator); | ||
var iid = new Guid(ICalculator.IID); | ||
Console.WriteLine($"Client: Requesting a Calculator (CLSID {clsid}) with ICalculator (IID {iid})"); | ||
int hr = Ole32.CoCreateInstance(ref clsid, /* No aggregation */ 0, (uint)Ole32.CLSCTX.CLSCTX_INPROC_SERVER, ref iid, out object comObject); | ||
Marshal.ThrowExceptionForHR(hr); | ||
ICalculator calculator = (ICalculator) comObject; | ||
|
||
int a = 5; | ||
int b = 3; | ||
int c; | ||
c = calculator.Add(a, b); | ||
Console.WriteLine($"Client: {a} + {b} = {c}"); | ||
c = calculator.Subtract(a, b); | ||
Console.WriteLine($"Client: {a} - {b} = {c}"); | ||
} | ||
} | ||
|
||
internal static unsafe partial class Ole32 | ||
{ | ||
// https://docs.microsoft.com/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance | ||
[LibraryImport(nameof(Ole32))] | ||
public static partial int CoCreateInstance( | ||
ref Guid rclsid, | ||
nint pUnkOuter, | ||
uint dwClsContext, | ||
ref Guid riid, | ||
// The default ComInterfaceMarshaller will unwrap a .NET object if it can tell the COM instance is a ComWrapper. | ||
// This causes issues when casting to ICalculator, since the Server's Calculator class doesn't implement the Client's interface. | ||
// UniqueComInterfaceMarshaller doesn't try to unwrap the object and always creates a new COM object. | ||
[MarshalUsing(typeof(UniqueComInterfaceMarshaller<object>))] | ||
out object ppv); | ||
|
||
// https://learn.microsoft.com/windows/win32/api/wtypesbase/ne-wtypesbase-clsctx | ||
public enum CLSCTX : uint | ||
{ | ||
CLSCTX_INPROC_SERVER = 0x1, | ||
CLSCTX_INPROC_HANDLER = 0x2, | ||
CLSCTX_LOCAL_SERVER = 0x4, | ||
CLSCTX_INPROC_SERVER16 = 0x8, | ||
CLSCTX_REMOTE_SERVER = 0x10, | ||
CLSCTX_INPROC_HANDLER16 = 0x20, | ||
CLSCTX_RESERVED1 = 0x40, | ||
CLSCTX_RESERVED2 = 0x80, | ||
CLSCTX_RESERVED3 = 0x100, | ||
CLSCTX_RESERVED4 = 0x200, | ||
CLSCTX_NO_CODE_DOWNLOAD = 0x400, | ||
CLSCTX_RESERVED5 = 0x800, | ||
CLSCTX_NO_CUSTOM_MARSHAL = 0x1000, | ||
CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000, | ||
CLSCTX_NO_FAILURE_LOG = 0x4000, | ||
CLSCTX_DISABLE_AAA = 0x8000, | ||
CLSCTX_ENABLE_AAA = 0x10000, | ||
CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000, | ||
CLSCTX_ACTIVATE_X86_SERVER = 0x40000, | ||
CLSCTX_ACTIVATE_32_BIT_SERVER, | ||
CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000, | ||
CLSCTX_ENABLE_CLOAKING = 0x100000, | ||
CLSCTX_APPCONTAINER = 0x400000, | ||
CLSCTX_ACTIVATE_AAA_AS_IU = 0x800000, | ||
CLSCTX_RESERVED6 = 0x1000000, | ||
CLSCTX_ACTIVATE_ARM32_SERVER = 0x2000000, | ||
CLSCTX_ALLOW_LOWER_TRUST_REGISTRATION, | ||
CLSCTX_PS_DLL = 0x80000000, | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/interop/source-generation/ComWrappersGeneration/ComWrappersGeneration.sln
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,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{065F345B-901A-4BCD-8CAF-ACBB6B6910F8}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{DCA455A0-57CF-467F-9D5D-10C95EAA9441}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{065F345B-901A-4BCD-8CAF-ACBB6B6910F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{065F345B-901A-4BCD-8CAF-ACBB6B6910F8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{065F345B-901A-4BCD-8CAF-ACBB6B6910F8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{065F345B-901A-4BCD-8CAF-ACBB6B6910F8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{DCA455A0-57CF-467F-9D5D-10C95EAA9441}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{DCA455A0-57CF-467F-9D5D-10C95EAA9441}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{DCA455A0-57CF-467F-9D5D-10C95EAA9441}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{DCA455A0-57CF-467F-9D5D-10C95EAA9441}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
14 changes: 14 additions & 0 deletions
14
core/interop/source-generation/ComWrappersGeneration/Directory.Build.props
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,14 @@ | ||
<Project> | ||
<!-- Shared properties --> | ||
<PropertyGroup> | ||
<DefaultTargetFramework>net8.0</DefaultTargetFramework> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<CompilerGeneratedFilesOutputPath>bin\Generated</CompilerGeneratedFilesOutputPath> | ||
<PublishDir>$(MSBuildThisFileDirectory)\OutputFiles\$(MSBuildProjectName)\</PublishDir> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="../Shared/*.cs" /> | ||
</ItemGroup> | ||
</Project> |
38 changes: 38 additions & 0 deletions
38
core/interop/source-generation/ComWrappersGeneration/README.md
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,38 @@ | ||
--- | ||
languages: | ||
- csharp | ||
products: | ||
- dotnet | ||
page_type: sample | ||
name: "Source-Generated COM Sample" | ||
urlFragment: "generated-comwrappers" | ||
description: "A .NET codebase that uses source-generated COM in .NET" | ||
--- | ||
# .NET Source-Generated COM Sample | ||
|
||
This tutorial demonstrates how to use COM source generators in .NET 8+ to create a COM server and client for in-process COM. | ||
|
||
This example defines an interface `ICalculator` that provides `Add` and `Subtract` methods. The server provides an implementation of `ICalculator` for the client to use after the server has been registered. The client project creates an instance of the object using the [`CoCreateInstance`](https://learn.microsoft.com/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance) Win32 method, and calls methods on the object. | ||
|
||
This sample supports NativeAOT and standard CoreCLR deployments. The native methods that the Windows COM system requires are exported automatically with the `[UnmanagedCallersOnly]` attribute when publishing with NativeAOT. For CoreCLR, the [DNNE](https://github.com/AaronRobinsonMSFT/DNNE) package is used to provide the exported functions. | ||
|
||
## Prerequisites | ||
|
||
- .NET 8+ SDK | ||
- Windows 10+ OS | ||
|
||
## Build and Run | ||
|
||
### NativeAOT | ||
|
||
Build the Native AOT binaries by running `dotnet publish -r <RID>` where `<RID>` is the RuntimeIdentifier for your OS, for example `win-x64`. The projects will copy the binaries to the `OutputFiles\` directory. After publishing, use `regsvr32.exe` to register `Server.dll` with COM by running run `regsvr.exe .\OutputFiles\Server\Server.dll`. Then, run the client application `.\OutputFiles\Client\Client.exe` and observe the output as it activates and uses a COM instance from `Server.dll`. | ||
|
||
### CoreCLR | ||
|
||
Build the projects by running `dotnet publish`. The projects will copy the binaries to the `OutputFiles\` directory. After publishing, use `regsvr32.exe` to register the native binary produced by DNNE, `ServerNE.dll` by running `regsvr.exe .\OutputFiles\Server\ServerNE.dll`. `ServerNE.dll` will start the .NET runtime and call the exported methods in the managed `Server.dll` which register the server with COM. Then, run the client application `.\OutputFiles\Client\Client.exe` and observe the output as it activates and uses a COM instance from `ServerNE.dll`. | ||
|
||
## See also | ||
|
||
- [ComWrappers source generation](https://learn.microsoft.com/dotnet/standard/native-interop/comwrappers-source-generation) | ||
- [Native exports in NativeAOT](https://learn.microsoft.com/dotnet/core/deploying/native-aot/interop#native-exports) | ||
- [COM interop in .NET](https://learn.microsoft.com/dotnet/standard/native-interop/cominterop) |
15 changes: 15 additions & 0 deletions
15
core/interop/source-generation/ComWrappersGeneration/Server/Calculator.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,15 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.Marshalling; | ||
using Microsoft.Win32; | ||
|
||
namespace Tutorial; | ||
|
||
[GeneratedComClass] | ||
[Guid(Clsid)] | ||
internal partial class Calculator : ICalculator | ||
{ | ||
public int Add(int a, int b) => a + b; | ||
public int Subtract(int a, int b) => a - b; | ||
internal const string Clsid = Clsids.Calculator; | ||
} |
Oops, something went wrong.