C++/CLI Library Demo
This sample demonstrates usage of a C++/CLI library in .NET. It contains the following projects:
ManagedApp: managed (.NET) application - usesMixedLibraryManagedLibrary: managed (.NET) libraryMixedLibrary: C++/CLI library - depends onManagedLibraryNativeApp: native (C++) application - usesMixedLibrary
The ManagedApp consumes MixedLibrary by directly referencing it to instantiate a managed class and by p/invoking into a native entry point.
The NativeApp consumes MixedLibrary via an import library and by explicitly loading it. This starts the .NET runtime and loads the assembly. Documentation on the details for activation of C++/CLI assemblies can be found here.
Prerequisites
This sample will only build and run on the Windows platform.
- .NET 5.0 SDK or later
- Visual Studio 2019 16.8 or later
Build and Run
- Open a Developer Command Prompt for Visual Studio.
- Navigate to the root directory and build the solution:
msbuild CPP-CLI.sln -restore.
- Run the app:
- Managed: run the
ManagedApp.exebinary- Example:
bin\Debug\x64\ManagedApp.exe
- Example:
- Native: run the
NativeApp.exebinary- Example:
bin\Debug\x64\NativeApp.exe
- Example:
- Managed: run the
The expected output will come from classes in the C++/CLI library and include a message passed from the app to the library:
For ManagedApp:
=== Managed class ===
Hello from ManagedClass in MixedLibrary
Hello from NativeClass in MixedLibrary
-- message: from managed app!
=== P/Invoke ===
Hello from NativeEntryPoint_CallNative in MixedLibrary
Hello from NativeClass in MixedLibrary
-- message: from managed app!
For NativeApp:
=== Import library ===
Hello from NativeEntryPoint_CallManaged in MixedLibrary
Hello from ManagedLibrary.Greet
-- message: from native app!
=== LoadLibrary ===
Hello from NativeEntryPoint_CallManaged in MixedLibrary
Hello from ManagedLibrary.Greet
-- message: from native app!