Skip to content

Commit 70a6f01

Browse files
AaronRobinsonMSFTRon Petrusha
authored andcommitted
Managed COM sample for .NET Core 3.0 (#857)
* Managed COM sample for .NET Core 3.0 * Feedback
1 parent b4be2c3 commit 70a6f01

File tree

10 files changed

+178
-0
lines changed

10 files changed

+178
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<!-- Used in lieu of a Primary Interop Assembly (PIA) -->
10+
<Compile Include="../COMContract/*.cs" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace COMClient
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var server = new Activation.Server();
11+
12+
var pi = server.ComputePi();
13+
Console.WriteLine($"\u03C0 = {pi}");
14+
}
15+
}
16+
17+
// The following classes are typically defined in a PIA, but for this example
18+
// are being defined here to simplify the scenario.
19+
namespace Activation
20+
{
21+
/// <summary>
22+
/// Managed definition of CoClass
23+
/// </summary>
24+
[ComImport]
25+
[CoClass(typeof(ServerClass))]
26+
[Guid(ContractGuids.ServerInterface)] // By TlbImp convention, set this to the GUID of the parent interface
27+
internal interface Server : IServer
28+
{
29+
}
30+
31+
/// <summary>
32+
/// Managed activation for CoClass
33+
/// </summary>
34+
[ComImport]
35+
[Guid(ContractGuids.ServerClass)]
36+
internal class ServerClass
37+
{
38+
}
39+
}
40+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
internal sealed class ContractGuids
2+
{
3+
public const string ServerClass = "DB1797F5-7198-4411-8563-D05F4E904956";
4+
public const string ServerInterface = "BA9AC84B-C7FC-41CF-8B2F-1764EB773D4B";
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
[ComVisible(true)]
5+
[Guid(ContractGuids.ServerInterface)]
6+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
7+
public interface IServer
8+
{
9+
/// <summary>
10+
/// Compute the value of the constant Pi.
11+
/// </summary>
12+
double ComputePi();
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!--
5+
COM servers must define a framework to use in situations where a CLR instance is not already present in the process.
6+
Note that since the COM server may be activated in a process where the CLR must be activated, both the projects
7+
*.runtimeconfig.json and *.deps.json files must be bundled with the server itself.
8+
9+
In the following example, the following files are needed to deploy the COM server:
10+
COMServer.comhost.dll
11+
COMServer.dll
12+
COMServer.deps.json
13+
COMServer.runtimeconfig.json
14+
-->
15+
<TargetFramework>netcoreapp3.0</TargetFramework>
16+
17+
<!-- Indicate the assembly is providing a COM server -->
18+
<UseComHost>True</UseComHost>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<!-- Used in lieu of a Primary Interop Assembly (PIA) -->
23+
<Compile Include="../COMContract/*.cs" />
24+
</ItemGroup>
25+
26+
<Target Name="ServerUsage"
27+
AfterTargets="Build">
28+
<Message Importance="High" Text="%0a************************************%0a*** $(MSBuildProjectName) usage instructions ***%0a************************************" />
29+
<Message Importance="High" Text="The server must be COM registered in order to activate.%0aThe following commands must be executed from an elevated command prompt." />
30+
<Message Importance="High" Text="Register:%0a regsvr32.exe &quot;$(ProjectDir)$(OutputPath)$(ComHostFileName)&quot;" />
31+
<Message Importance="High" Text="Unregister:%0a regsvr32.exe /u &quot;$(ProjectDir)$(OutputPath)$(ComHostFileName)&quot;" />
32+
</Target>
33+
34+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace COMServer
5+
{
6+
[ComVisible(true)]
7+
[Guid(ContractGuids.ServerClass)]
8+
public class Server : IServer
9+
{
10+
double IServer.ComputePi()
11+
{
12+
double sum = 0.0;
13+
int sign = 1;
14+
for (int i = 0; i < 1024; ++i)
15+
{
16+
sum += sign / (2.0 * i + 1.0);
17+
sign *= -1;
18+
}
19+
20+
return 4.0 * sum;
21+
}
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageRestore>
4+
<add key="enabled" value="True" />
5+
</packageRestore>
6+
<packageSources>
7+
<clear />
8+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
9+
</packageSources>
10+
<activePackageSource>
11+
<add key="All" value="(Aggregate source)" />
12+
</activePackageSource>
13+
</configuration>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
COM Server Demo
2+
================
3+
4+
This is a basic example of providing a managed COM server in .NET Core 3.0. Documentation on the inner workings of activation can be found [here](https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/COM-activation.md).
5+
6+
Key Features
7+
------------
8+
9+
Demonstrates how to provide a COM server in .NET Core 3.0 Preview 5 or later.
10+
11+
Addtional comments are contained in source and project files.
12+
13+
Build and Run
14+
-------------
15+
16+
The project will only build and run on the Windows platform.
17+
18+
1) Install .NET Core 3.0 Preview 5 or later.
19+
20+
1) Navigate to the root directory and run `dotnet.exe build`.
21+
22+
1) Follow the instructions for COM server registration that were emitted during the build.
23+
24+
1) Navigate to `COMClient/` and run `dotnet.exe run`.
25+
26+
**Note** Remember to unregister the COM server when the demo is complete.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.Build.Traversal">
2+
<ItemGroup>
3+
<ProjectReference Include="COMClient/*.csproj" />
4+
<ProjectReference Include="COMServer/*.csproj" />
5+
</ItemGroup>
6+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"msbuild-sdks": {
3+
"Microsoft.Build.Traversal": "1.0.52"
4+
}
5+
}

0 commit comments

Comments
 (0)