Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to .NET 7 paths and add error reporting #5627

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions core/nativeaot/NativeLibrary/Class1.cs

This file was deleted.

54 changes: 54 additions & 0 deletions core/nativeaot/NativeLibrary/LibraryFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

namespace NativeLibrary;

public class LibraryFunctions
{
[UnmanagedCallersOnly(EntryPoint = "add")]
public static int Add(int a, int b)
{
return a + b;
}

[UnmanagedCallersOnly(EntryPoint = "write_line")]
public static int WriteLine(IntPtr pString)
{
// The marshalling code is typically auto-generated by a custom tool in larger projects.
try
{
// UnmanagedCallersOnly methods only accept primitive arguments. The primitive arguments
// have to be marshalled manually if necessary.
string str = Marshal.PtrToStringAnsi(pString);

Console.WriteLine(str);
}
catch
{
// Exceptions escaping out of UnmanagedCallersOnly methods are treated as unhandled exceptions.
// The errors have to be marshalled manually if necessary.
return -1;
}
return 0;
}

[UnmanagedCallersOnly(EntryPoint = "sumstring")]
public static IntPtr sumstring(IntPtr first, IntPtr second)
{
// Parse strings from the passed pointers
string my1String = Marshal.PtrToStringAnsi(first);
string my2String = Marshal.PtrToStringAnsi(second);

// Concatenate strings
string sum = my1String + my2String;

// Assign pointer of the concatenated string to sumPointer
IntPtr sumPointer = Marshal.StringToHGlobalAnsi(sum);

// Return pointer
return sumPointer;
}
}
51 changes: 31 additions & 20 deletions core/nativeaot/NativeLibrary/LoadLibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

//Set this value accordingly to your workspace settings
#if defined(_WIN32)
#define PathToLibrary "bin\\Debug\\net6.0\\win-x64\\native\\NativeLibrary.dll"
#define PathToLibrary "bin\\Debug\\net7.0\\win-x64\\native\\NativeLibrary.dll"
#elif defined(__APPLE__)
#define PathToLibrary "./bin/Debug/net6.0/osx-x64/native/NativeLibrary.dylib"
#define PathToLibrary "./bin/Debug/net7.0/osx-x64/native/NativeLibrary.dylib"
#else
#define PathToLibrary "./bin/Debug/net6.0/linux-x64/native/NativeLibrary.so"
#define PathToLibrary "./bin/Debug/net7.0/linux-x64/native/NativeLibrary.so"
#endif

#ifdef _WIN32
Expand All @@ -31,6 +31,8 @@
int callSumFunc(char *path, char *funcName, int a, int b);
char *callSumStringFunc(char *path, char *funcName, char *a, char *b);

void* loadSymbol(char *path, char *funcName);

int main()
{
// Check if the library file exists
Expand All @@ -52,44 +54,53 @@ int main()
free(sumstring);
}

int callSumFunc(char *path, char *funcName, int firstInt, int secondInt)
void *loadSymbol(char *path, char *funcName)
{
// Call sum function defined in C# shared library
// Library loading
#ifdef _WIN32
HINSTANCE handle = LoadLibraryA(path);
#else
void *handle = dlopen(path, RTLD_LAZY);
#endif
if (!handle)
gewarren marked this conversation as resolved.
Show resolved Hide resolved
{
#ifdef _WIN32
int errorCode = GetLastError();
printf("Failed to load library at specified path. Error code: %d\n", errorCode);
#else
puts("Failed to load library at specified path");
gewarren marked this conversation as resolved.
Show resolved Hide resolved
#endif
return NULL;
}

typedef int(*myFunc)(int,int);
myFunc MyImport = (myFunc)symLoad(handle, funcName);
// Declare a typedef
typedef char *(*myFunc)(char*,char*);

int result = MyImport(firstInt, secondInt);
// Import Symbol named funcName

// CoreRT libraries do not support unloading
// NativeAOT libraries do not support unloading
// See https://github.com/dotnet/corert/issues/7887
return symLoad(handle, funcName);
}

int callSumFunc(char *path, char *funcName, int firstInt, int secondInt)
{
typedef int(*myFunc)(int,int);
myFunc MyImport = (myFunc)loadSymbol(path, funcName);

int result = MyImport(firstInt, secondInt);
return result;
}

char *callSumStringFunc(char *path, char *funcName, char *firstString, char *secondString)
{
// Library loading
#ifdef _WIN32
HINSTANCE handle = LoadLibraryA(path);
#else
void *handle = dlopen(path, RTLD_LAZY);
#endif

// Declare a typedef
typedef char *(*myFunc)(char*,char*);

// Import Symbol named funcName
myFunc MyImport = (myFunc)symLoad(handle, funcName);
myFunc MyImport = (myFunc)loadSymbol(path, funcName);

// The C# function will return a pointer
char *result = MyImport(firstString, secondString);

// CoreRT libraries do not support unloading
// See https://github.com/dotnet/corert/issues/7887
return result;
}