C#C
C#3y ago
Pillow

❔ [UnmanagedCallersOnly] with a function that takes a string

I'm trying to make a C# library, and had success with

using System.Runtime.InteropServices;

namespace ToasterMap
{
    static class ToasterMap
    {
        [UnmanagedCallersOnly(EntryPoint = "add")]
        public static int Add(int a, int b) => a + b;
    }
}
but once I made it
using System.Runtime.InteropServices;
using System;

namespace ToasterMap
{
    static class ToasterMap
    {
        public static String tbinPath;

        [UnmanagedCallersOnly(EntryPoint = "add")]
        public static int Add(int a, int b) => a + b;

        [UnmanagedCallersOnly(EntryPoint = "init")]
        public static void Init(string tbinPath)
        {
            if (!File.Exists(tbinPath))
            {
                throw new Exception("TBin path does not exist");
            }

            ToasterMap.tbinPath = tbinPath;
        }
    }
}
It now fails to compile with:
C:\Coding\C#\ToasterMap\ToasterMap.cs(14,33): error CS8894: Cannot use 'string' as a
 parameter type on a method attributed with 'UnmanagedCallersOnly'. [C:\Coding\C#\To
asterMap\ToasterMap.csproj]

I understand that it seems I can't use string as a type, but not sure what else to use?
char[]
and
byte[]
also fails
Was this page helpful?