Skip to content

Commit 6b32561

Browse files
authored
Merge pull request #563 from dotnet/master
Update live with current master
2 parents 3297a0f + 0c2d6a7 commit 6b32561

File tree

19 files changed

+721
-155
lines changed

19 files changed

+721
-155
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.NET Core Hosting Sample
2+
========================
3+
4+
This sample demonstrates a simple .NET Core host using the hosting APIs from [CoreClrHost.h](https://github.com/dotnet/coreclr/blob/master/src/coreclr/hosts/inc/coreclrhost.h). The sample host loads and starts the .NET Core CLR (including starting the default AppDomain), loads managed code, calls into a managed method, and provides a function pointer for the managed code to call back into the host.
5+
6+
This sample is part of the [.NET Core hosting tutorial](https://docs.microsoft.com/dotnet/core/tutorials/netcore-hosting). See that topic for a more detailed explanation of this sample. There are also alternative hosting APIs (the `ICLRRuntimeHost2` interface in [mscoree.h](https://github.com/dotnet/coreclr/tree/master/src/pal/prebuilt/inc/mscoree.h)) that are demonstrated in the [HostWithMscoree folder](../HostWithMsCoree) of this repository.
7+
8+
About .NET Core Hosts
9+
---------------------
10+
11+
.NET Core applications are always run by a host. In most cases, the default dotnet.exe host is used.
12+
13+
It is possible to create your own host, though, to enable starting and running .NET Core code from a native application, or to enable a high degree of control over how the runtime operates. More complex, real-world hosts can be found in the [dotnet/coreclr](https://github.com/dotnet/coreclr/tree/master/src/coreclr/hosts) repository.
14+
15+
Build and Run
16+
-------------
17+
18+
To build this sample, just use the included build scripts *build.bat*, *build.sh*, or *buildOsx.sh*. These scripts build both the managed target assembly (ManagedLibrary.dll) and the host (SampleHost.exe). The build scripts are just simple wrappers around two build calls (`dotnet publish` for the managed component of the sample and cl.exe/g++ for the host), so it's also easy to build the two components directly if you prefer. The build scripts build for Windows 10 (x64), Linux (x64), and OSX (x64), respectively, and assume that both the dotnet CLI and the C++ compiler (cl.exe or g++) are available on the path (as will be the case on Windows if building from a Visual Studio Developer Command Prompt). The build scripts will need modified if you intend to target other platforms or use tools from other paths. Be sure that the bitness of the host and sample app match. By default, the build scripts build ManagedLibrary.dll for x64, so you will need to build the host for 64-bit.
19+
20+
To run the host, just execute SampleHost from the bin/{{OS}} directory.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@echo off
2+
3+
REM This script builds the sample for x64 Windows 10. If using Win7, adjust the 'dotnet publish' command.
4+
REM It assumes that both the dotnet CLI and cl.exe compiler are available on the path.
5+
REM A Visual Studio Developer Command Prompt will already have cl.exe on its path.
6+
7+
SET SRCDIR=%~dp0src
8+
SET OUTDIR=%~dp0bin\windows
9+
10+
mkdir %OUTDIR%
11+
12+
REM Build managed component
13+
echo Building Managed Library
14+
dotnet publish --self-contained -r win10-x64 %SRCDIR%\ManagedLibrary\ManagedLibrary.csproj -o %OUTDIR%
15+
16+
REM Build native component
17+
cl.exe %SRCDIR%\SampleHost.cpp /Fo%OUTDIR%\ /Fd%OUTDIR%\SampleHost.pdb /EHsc /Od /GS /sdl /Zi /D "WINDOWS" /link ole32.lib /out:%OUTDIR%\SampleHost.exe
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This script builds the sample for x64 Linux.
2+
# It assumes that both the dotnet CLI and g++ compiler are available on the path.
3+
4+
SCRIPTPATH=$(readlink -f "$0")
5+
BASEDIR=$(dirname $SCRIPTPATH)
6+
SRCDIR=${BASEDIR}/src
7+
OUTDIR=${BASEDIR}/bin/linux
8+
9+
# Make output directory, if needed
10+
if [ ! -d "${OUTDIR}" ]; then
11+
mkdir -p ${OUTDIR}
12+
fi
13+
14+
# Build managed component
15+
echo Building Managed Library
16+
dotnet publish --self-contained -r linux-x64 ${SRCDIR}/ManagedLibrary/ManagedLibrary.csproj -o ${OUTDIR}
17+
18+
# Build native component
19+
g++ -o ${OUTDIR}/SampleHost -D LINUX ${SRCDIR}/SampleHost.cpp -ldl
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This script builds the sample for x64 OSX.
2+
# It assumes that both the dotnet CLI and g++ compiler are available on the path.
3+
4+
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5+
SRCDIR=${BASEDIR}/src
6+
OUTDIR=${BASEDIR}/bin/osx
7+
8+
# Make output directory, if needed
9+
if [ ! -d "${OUTDIR}" ]; then
10+
mkdir -p ${OUTDIR}
11+
fi
12+
13+
# Build managed component
14+
echo Building Managed Library
15+
echo dotnet publish --self-contained -r osx-x64 ${SRCDIR}/ManagedLibrary/ManagedLibrary.csproj -o ${OUTDIR}
16+
dotnet publish --self-contained -r osx-x64 ${SRCDIR}/ManagedLibrary/ManagedLibrary.csproj -o ${OUTDIR}
17+
18+
# Build native component
19+
# -D both LINUX and OSX since most LINUX code paths apply to OSX also
20+
g++ -o ${OUTDIR}/SampleHost -D LINUX -D OSX ${SRCDIR}/SampleHost.cpp -ldl
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!-- Although this project is not used as an exe (it's not the process's entry point),
5+
it is built as an exe so that publishing it will include the .NET Core runtime and
6+
framework libraries for use by the host -->
7+
<OutputType>Exe</OutputType>
8+
<TargetFramework>netcoreapp2.1</TargetFramework>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Linq;
3+
using System.Runtime.InteropServices;
4+
using System.Threading;
5+
6+
namespace ManagedLibrary
7+
{
8+
// Sample managed code for the host to call
9+
public class ManagedWorker
10+
{
11+
// This assembly is being built as an exe as a simple way to
12+
// get .NET Core runtime libraries deployed (`dotnet publish` will
13+
// publish .NET Core libraries for exes). Therefore, this assembly
14+
// requires an entry point method even though it is unused.
15+
public static void Main()
16+
{
17+
Console.WriteLine("This assembly is not meant to be run directly.");
18+
Console.WriteLine("Instead, please use the SampleHost process to load this assembly.");
19+
}
20+
21+
public delegate int ReportProgressFunction(int progress);
22+
23+
// This test method doesn't actually do anything, it just takes some input parameters,
24+
// waits (in a loop) for a bit, invoking the callback function periodically, and
25+
// then returns a string version of the double[] passed in.
26+
[return: MarshalAs(UnmanagedType.LPStr)]
27+
public static string DoWork(
28+
[MarshalAs(UnmanagedType.LPStr)] string jobName,
29+
int iterations,
30+
int dataSize,
31+
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] double[] data,
32+
ReportProgressFunction reportProgressFunction)
33+
{
34+
for (int i = 1; i <= iterations; i++)
35+
{
36+
Console.ForegroundColor = ConsoleColor.Cyan;
37+
Console.WriteLine($"Beginning work iteration {i}");
38+
Console.ResetColor();
39+
40+
// Pause as if doing work
41+
Thread.Sleep(1000);
42+
43+
// Call the native callback and write its return value to the console
44+
var progressResponse = reportProgressFunction(i);
45+
Console.WriteLine($"Received response [{progressResponse}] from progress function");
46+
}
47+
48+
Console.ForegroundColor = ConsoleColor.Green;
49+
Console.WriteLine($"Work completed");
50+
Console.ResetColor();
51+
52+
return $"Data received: {string.Join(", ", data.Select(d => d.ToString()))}";
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)