Skip to content

Commit

Permalink
Update hosting sample to also show hostfxr_run_app and hdt_get_functi…
Browse files Browse the repository at this point in the history
…on_pointer (#6145)
  • Loading branch information
elinor-fung committed Sep 7, 2023
1 parent 86b2e72 commit ebc21d3
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 86 deletions.
1 change: 1 addition & 0 deletions core/hosting/build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="src/NativeHost/*.csproj" />
<ProjectReference Include="src/DotNetLib/*.csproj" />
<ProjectReference Include="src/App/*.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions core/hosting/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ Hello, world! from CustomEntryPointUnmanagedCallersOnly in Lib
-- number: -1
```

To make this sample run a managed app instead of loading a class library, launch the `nativehost` passing `app` as a command line argument. The expected output will come from the `App` application:

```console
App started - args = [ app_arg_1, app_arg_2 ]
Hello, world! from App [count: 1]
-- message: from host!
Hello, world! from App [count: 2]
-- message: from host!
Hello, world! from App [count: 3]
-- message: from host!
Signaling app to close
```

Note: The way the sample is built is relatively complicated. The goal is that it's possible to build and run the sample with simple `dotnet run` with minimal requirements on pre-installed tools. Typically, real-world projects that have both managed and native components will use different build systems for each; for example, msbuild/dotnet for managed and CMake for native.

## Visual Studio support
Expand Down
32 changes: 32 additions & 0 deletions core/hosting/src/App/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;

public class App
{
private static byte isWaiting = 0;
private static int s_CallCount = 0;
private static ManualResetEvent mre = new ManualResetEvent(false);

public static void Main(string[] args)
{
Console.WriteLine($"{nameof(App)} started - args = [ {string.Join(", ", args)} ]");
isWaiting = 1;
mre.WaitOne();
}

[UnmanagedCallersOnly]
public static byte IsWaiting() => isWaiting;

[UnmanagedCallersOnly]
public static void Hello(IntPtr message)
{
Console.WriteLine($"Hello, world! from {nameof(App)} [count: {++s_CallCount}]");
Console.WriteLine($"-- message: {Marshal.PtrToStringUTF8(message)}");
if (s_CallCount >= 3)
{
Console.WriteLine("Signaling app to close");
mre.Set();
}
}
}
14 changes: 14 additions & 0 deletions core/hosting/src/App/App.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<UseAppHost>false</UseAppHost>
</PropertyGroup>

<PropertyGroup>
<OutputPath>$(BinRoot)/$(Configuration)/</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

</Project>
18 changes: 16 additions & 2 deletions core/hosting/src/HostWithHostFxr.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29102.190
# Visual Studio Version 17
VisualStudioVersion = 17.8.34030.414
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NativeHost.vs", "NativeHost\NativeHost.vs.vcxproj", "{B99BC289-621D-4DB6-A964-2D869F8E0DD3}"
EndProject
Expand All @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{91651AB7
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeHost", "NativeHost\NativeHost.csproj", "{0A829E27-48E8-4A1E-BD69-74A2DCFC87C7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "App\App.csproj", "{7E9BD819-E143-4347-9430-68B583BEFDB7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,6 +55,18 @@ Global
{0A829E27-48E8-4A1E-BD69-74A2DCFC87C7}.Release|x64.Build.0 = Release|Any CPU
{0A829E27-48E8-4A1E-BD69-74A2DCFC87C7}.Release|x86.ActiveCfg = Release|Any CPU
{0A829E27-48E8-4A1E-BD69-74A2DCFC87C7}.Release|x86.Build.0 = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|x64.ActiveCfg = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|x64.Build.0 = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|x86.ActiveCfg = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Debug|x86.Build.0 = Debug|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|Any CPU.Build.0 = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|x64.ActiveCfg = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|x64.Build.0 = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|x86.ActiveCfg = Release|Any CPU
{7E9BD819-E143-4347-9430-68B583BEFDB7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit ebc21d3

Please sign in to comment.