Skip to content

Latest commit

 

History

History
479 lines (309 loc) · 8.98 KB

README.source.md

File metadata and controls

479 lines (309 loc) · 8.98 KB

Embedded .NET 6 Runtime

Build and Test

This repo contains a project with examples for using a embedded .NET 6 runtime in a C++ application using dotnet_runtime.

Build and Run

Windows

Requirements

  • Visual Studio 2019
  • .NET 6 SDK

Steps

  1. Clone this repository recursively using git clone --recursive [email protected]:KevinGliewe/embedded_dotnet_runtime_examples.git
  2. Run build_run.bat

What happens during the build?

  • Imports VsDevCmd.bat
  • Installs dotnet tool runtimedl
  • Downloads the propper .NET 6 runtime for your system using runtimedl
  • Builds Lib and Host using MSBuild
  • Runs Host

Linux & MacOS

Requirements

  • CMake 3.16 or newer
  • .NET 6 SDK
  • C++ compiler

Steps

  1. Clone this repository recursively using git clone --recursive [email protected]:KevinGliewe/embedded_dotnet_runtime_examples.git
  2. Run sh build_run.sh

What happens during the build?

  • Installs dotnet tool runtimedl
  • Downloads the propper .NET 6 runtime for your system using runtimedl
  • Builds Lib using .NET 6 SDK
  • Creates makefile using CMake
  • Builds Host using make
  • Runs Host

Examples

Load and initialize .NET runtime

snippet: InitRuntimeAndLib


Managed Entrypoint

Entrypoints can only use blittable types

Managed component-entrypoint

Native

snippet: Test_ManagedEntryPoint_Args_CPP

snippet: Test_ManagedEntryPoint_ComponentEntryPoint_CPP

Managed

snippet: Test_ManagedEntryPoint_Args_CS

snippet: Test_ManagedEntryPoint_ComponentEntryPoint_CS

Managed custom-entrypoint

Entrypoints can only use blittable types

Native

snippet: Test_ManagedEntryPoint_Args_CPP

snippet: Test_ManagedEntryPoint_CustomEntryPoint_CPP

Managed

snippet: Test_ManagedEntryPoint_Args_CS

snippet: Test_ManagedEntryPoint_CustomEntryPoint_CS


Managed Strings

Return managed ASCII string

Native

snippet: Test_ManagedString_Ansi_CPP

Managed

snippet: Test_ManagedString_Ansi_CS

What does CString do?

  1. Apends a \0 character on the end of the string.
  2. Converts the string into ANSI encoding.
  3. Allocate memory for native access using Marshal.AllocHGlobal.
  4. Copy the encoded string into the allocated memory.

Return managed wide string

The encoding depends on the platform. For windows systems it is UTF16 and for posix systems it is UTF32.

Native

snippet: Test_ManagedString_Wide_CPP

Managed

What does CString do?

  1. Determins the correct encoding for the current platform. (UTF16 or UTF32)
  2. Apends a \0 character on the end of the string.
  3. Converts the string into the propper encoding.
  4. Allocate memory for native access using Marshal.AllocHGlobal.
  5. Copy the encoded string into the allocated memory.

snippet: Test_ManagedString_Wide_CS


Native Strings

Native ASCII string

Native

snippet: Test_NativeString_Ansi_CPP

Managed

snippet: Test_NativeString_Ansi_CS

Native Wide string

Native

snippet: Test_NativeString_Wide_CPP

Managed

snippet: Test_NativeString_Wide_CS

Native string to managed function pointer

Native

snippet: Test_NativeString_RetArgs_CPP

snippet: Test_NativeString_FunctionPointer_CPP

Managed

snippet: Test_NativeString_FunctionPointer_CS


Managed function-pointer

Managed function-pointer to instance method

Native

snippet: Test_ManagedFunctionPointer_Typedef_managed_callback_fn_CPP

snippet: Test_ManagedFunctionPointer_Instance_CPP

Managed

snippet: Test_ManagedFunctionPointer_FunctionPointerCallbackDelegate_CS

snippet: Test_ManagedFunctionPointer_Instance_CS

Managed function-pointer to static function

Native

snippet: Test_ManagedFunctionPointer_Typedef_managed_callback_fn_CPP

snippet: Test_ManagedFunctionPointer_Static_CPP

Managed

snippet: Test_ManagedFunctionPointer_FunctionPointerCallbackDelegate_CS

snippet: Test_ManagedFunctionPointer_Static_CS


Native function-pointer

Calling checked native function pointer

Native

snippet: Test_NativeFunctionPointer_CallbackFunc_CPP

snippet: Test_NativeFunctionPointer_CallbackPointer_CPP

snippet: Test_NativeFunctionPointer_CallbackFunc_Checked_CPP

Managed

snippet: Test_NativeFunctionPointer_Checked_CS

Calling unchecked native function pointer

Native

snippet: Test_NativeFunctionPointer_CallbackFunc_CPP

snippet: Test_NativeFunctionPointer_CallbackPointer_CPP

snippet: Test_NativeFunctionPointer_CallbackFunc_Unchecked_CPP

Managed

snippet: Test_NativeFunctionPointer_Unchecked_CS


Usage of unsafe managed code to access native objects

Native

snippet: Test_ManagedUnsafe_CPP

Managed

snippet: Test_ManagedUnsafe_CS


Native Arrays

Native arrays using fixed struct member

Native

snippet: Test_NativeArray_Args_CPP

snippet: Test_NativeArray_Args_Data_CPP

snippet: Test_NativeArray_StructFixed_CPP

Managed

snippet: Test_NativeArray_StructFixed_CS

Native arrays using pointer

Native

snippet: Test_NativeArray_ArgumentFixed_CPP

Managed

snippet: Test_NativeArray_ArgumentFixed_CS

Native arrays using ArrPointerX on function-pointer

Native

snippet: Test_NativeArray_ArgumentFixed_FunctionPointer_CPP

Managed

snippet: Test_NativeArray_ArgumentFixed_FunctionPointer_CS


Native Symbols

Calling native exported symbols using DllImport

Native

snippet: Test_DllImport_Export_CPP

snippet: Test_DllImport_Call_CPP

For posix systems, use the -export-dynamic flag for the linker.

Managed

snippet: Test_DllImport_CS

Calling native exported symbols using GetProcAddress/dlsym

Native

snippet: Test_NativeExport_Export_CPP

snippet: Test_NativeExport_Call_CPP

For posix systems, use the -export-dynamic flag for the linker.

Managed

snippet: Test_NativeExport_CS


Native VTable

VTable

Calling native VTable from managed code

Native

snippet: Test_NativeVTable_Class_CPP

snippet: Test_NativeVTable_Call_CPP

Managed

snippet: Test_NativeVTable_Class_CS

snippet: Test_NativeVTable_ManagedCall_CS

Overwriting native VTable with managed code

Native

snippet: Test_NativeVTable_Class_CPP

snippet: Test_NativeVTable_Call_CPP

Managed

snippet: Test_NativeVTable_Class_CS

snippet: Test_NativeVTable_Overwrite_CS

snippet: Test_NativeVTable_ManagedOverwrite_CS


LICENSE

dotnet_runtime_test is licensed under MIT license. See LICENSE for more details.