C
C#16mo 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;
}
}
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;
}
}
}
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]
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
5 Replies
Jimmacle
Jimmacle16mo ago
Span? Or actual pointers
Pillow
PillowOP16mo ago
I'm not entirely bothered what type it is, but as long as it's a 'string' and something ctypes will let me use is ideal
Jimmacle
Jimmacle16mo ago
I'm assuming you need something pretty close to plain pointer and length
Pillow
PillowOP16mo ago
probably yes so it seems IntPtr works, but I don't know how to make it return something, because python can't use pointers
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server