Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
setup initial C# CoreWrapper class and utilize it in sample cli app
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Apr 15, 2024
1 parent 2fe1d80 commit beb2e8f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Empty file removed bindings/c#/.gitignore
Empty file.
28 changes: 28 additions & 0 deletions bindings/c#/Mgtt.CoreWrapper/CoreWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Runtime.InteropServices;

namespace Mgtt.CoreWrapper
{
public class CoreWrapper
{
public static string _libraryPath {get; set;}

[DllImport("{LibraryPath}", EntryPoint = "add")]
public static extern int Add(int a, int b);

[DllImport("{LibraryPath}", EntryPoint = "subtract")]
public static extern int Subtract(int a, int b);

[DllImport("{LibraryPath}", EntryPoint = "multiply")]
public static extern int Multiply(int a, int b);

[DllImport("{LibraryPath}", EntryPoint = "divide")]
public static extern float Divide(float a, float b);

[DllImport("{LibraryPath}", EntryPoint = "getCircleArea")]
public static extern float GetCircleArea(float radius);

[DllImport("{LibraryPath}", EntryPoint = "getCircleCircumference")]
public static extern float GetCircleCircumference(float radius);
}
}
14 changes: 14 additions & 0 deletions bindings/c#/Mgtt.CoreWrapper/Mgtt.CoreWrapper.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>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions bindings/c#/Mgtt.CoreWrapper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CommandLine;

namespace Mgtt.CoreWrapper;
class Program
{
static int Main(string[] args)
{
Options options = null;
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o => options = o);

if (options == null)
{
Console.WriteLine("Usage: dotnet run --coreLibraryPath <coreLibraryPath>");
return 1;
}

CoreWrapper._libraryPath = options.CoreLibraryPath;

int resultAdd = CoreWrapper.Add(10, 5);
Console.WriteLine("Addition result: " + resultAdd);

int resultSubtract = CoreWrapper.Subtract(10, 5);
Console.WriteLine("Subtraction result: " + resultSubtract);

int resultMultiply = CoreWrapper.Multiply(10, 5);
Console.WriteLine("Multiplication result: " + resultMultiply);

float resultDivide = CoreWrapper.Divide(10.0f, 2.0f);
Console.WriteLine("Division result: " + resultDivide);

float radius = 5.0f;

float area = CoreWrapper.GetCircleArea(radius);
Console.WriteLine("Circle area: " + area);

float circumference = CoreWrapper.GetCircleCircumference(radius);
Console.WriteLine("Circle circumference: " + circumference);

return 0;
}
}

public class Options
{
[Option("coreLibraryPath", Required = true, HelpText = "Path to the core library.")]
public string CoreLibraryPath { get; set; }
}

0 comments on commit beb2e8f

Please sign in to comment.