Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e634d0

Browse files
authored
Update to .NET 7 paths and add error reporting (#5627)
1 parent 764ea35 commit 3e634d0

File tree

3 files changed

+82
-72
lines changed

3 files changed

+82
-72
lines changed

core/nativeaot/NativeLibrary/Class1.cs

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace NativeLibrary;
8+
9+
public class LibraryFunctions
10+
{
11+
[UnmanagedCallersOnly(EntryPoint = "add")]
12+
public static int Add(int a, int b)
13+
{
14+
return a + b;
15+
}
16+
17+
[UnmanagedCallersOnly(EntryPoint = "write_line")]
18+
public static int WriteLine(IntPtr pString)
19+
{
20+
// The marshalling code is typically auto-generated by a custom tool in larger projects.
21+
try
22+
{
23+
// UnmanagedCallersOnly methods only accept primitive arguments. The primitive arguments
24+
// have to be marshalled manually if necessary.
25+
string str = Marshal.PtrToStringAnsi(pString);
26+
27+
Console.WriteLine(str);
28+
}
29+
catch
30+
{
31+
// Exceptions escaping out of UnmanagedCallersOnly methods are treated as unhandled exceptions.
32+
// The errors have to be marshalled manually if necessary.
33+
return -1;
34+
}
35+
return 0;
36+
}
37+
38+
[UnmanagedCallersOnly(EntryPoint = "sumstring")]
39+
public static IntPtr sumstring(IntPtr first, IntPtr second)
40+
{
41+
// Parse strings from the passed pointers
42+
string my1String = Marshal.PtrToStringAnsi(first);
43+
string my2String = Marshal.PtrToStringAnsi(second);
44+
45+
// Concatenate strings
46+
string sum = my1String + my2String;
47+
48+
// Assign pointer of the concatenated string to sumPointer
49+
IntPtr sumPointer = Marshal.StringToHGlobalAnsi(sum);
50+
51+
// Return pointer
52+
return sumPointer;
53+
}
54+
}

core/nativeaot/NativeLibrary/LoadLibrary.c

+28-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
int callSumFunc(char *path, char *funcName, int a, int b);
3434
char *callSumStringFunc(char *path, char *funcName, char *a, char *b);
3535

36+
void* loadSymbol(char *path, char *funcName);
37+
3638
int main()
3739
{
3840
// Check if the library file exists
@@ -54,43 +56,53 @@ int main()
5456
CoTaskMemFree(sumstring);
5557
}
5658

57-
int callSumFunc(char *path, char *funcName, int firstInt, int secondInt)
59+
void *loadSymbol(char *path, char *funcName)
5860
{
59-
// Call sum function defined in C# shared library
61+
// Library loading
6062
#ifdef _WIN32
6163
HINSTANCE handle = LoadLibraryA(path);
6264
#else
6365
void *handle = dlopen(path, RTLD_LAZY);
6466
#endif
67+
if (!handle)
68+
{
69+
#ifdef _WIN32
70+
int errorCode = GetLastError();
71+
printf("Failed to load library at specified path. Error code: %d\n", errorCode);
72+
#else
73+
puts("Failed to load library at specified path");
74+
#endif
75+
return NULL;
76+
}
77+
78+
// Declare a typedef
79+
typedef char *(*myFunc)(char*,char*);
80+
81+
// Import Symbol named funcName
82+
83+
// NativeAOT libraries do not support unloading
84+
// See https://github.com/dotnet/corert/issues/7887
85+
return symLoad(handle, funcName);
86+
}
6587

88+
int callSumFunc(char *path, char *funcName, int firstInt, int secondInt)
89+
{
6690
typedef int(*myFunc)(int,int);
67-
myFunc MyImport = (myFunc)symLoad(handle, funcName);
91+
myFunc MyImport = (myFunc)loadSymbol(path, funcName);
6892

6993
int result = MyImport(firstInt, secondInt);
70-
71-
// NOTE: Native AOT libraries do not support unloading
7294
return result;
7395
}
7496

7597
char *callSumStringFunc(char *path, char *funcName, char *firstString, char *secondString)
7698
{
77-
// Library loading
78-
#ifdef _WIN32
79-
HINSTANCE handle = LoadLibraryA(path);
80-
#else
81-
void *handle = dlopen(path, RTLD_LAZY);
82-
#endif
83-
8499
// Declare a typedef
85100
typedef char *(*myFunc)(char*,char*);
86101

87102
// Import Symbol named funcName
88-
myFunc MyImport = (myFunc)symLoad(handle, funcName);
103+
myFunc MyImport = (myFunc)loadSymbol(path, funcName);
89104

90105
// The C# function will return a pointer
91106
char *result = MyImport(firstString, secondString);
92-
93-
// CoreRT libraries do not support unloading
94-
// See https://github.com/dotnet/corert/issues/7887
95107
return result;
96108
}

0 commit comments

Comments
 (0)