Explanation for unusable struct string for delegate function pointer ( delegate* ) if you use AppWithPlugin for NativeAot or Gtk3/Gtk4 Wrapper for everything.
Introduction
I saw problems with ClangSharpPInvokeGenerator
and class UnmanagedCallerOnlyAttribute
, they can't pass with struct
string
to static
function with UnmanagedCallerOnly
or your application tries to communicate with loading shared libraries.
Background
Since I had an idea, I wrote AppWithPlugin
into NativeAot
and I expect sbyte*
for string
and sbyte**
for string[]
pass in C/C++ as const char*
and char**
.
You can check out my repository at https://github.com/DeafMan1983/AppWithPluginForNativeAot.
Using the Code
Example for SDL2 Wrapper (DeafMan1983.Interop.SDL2
):
For string
to sbyte*
(sbyte*
from string
):
sbyte* title = SBytePointerFromString("HelloSDL");
...
SDL_Window* window = SDL_CreateWindow(title, ... );
Or sbyte*
to string
(string
from sbyte*
).
[DllImport("c")]
private static extern int printf(sbyte* fmt);
public static int Printf(sbyte* fmt, params object[] args)
{
string result = StringFromSBytePointer(fmt);
if (result == null)
return 1;
return printf(SBytePointerFromString(string.Format(result, args));
}
Two nice ways for sbyte*
and string
, you can use like this.
And nice example with Gtk3/Gtk4:
string[]
to sbyte**
(sbyte**
from string[]
)
static int Main(string[] args)
{
GtkApplication* app = gtk_application_new(SBytePointerFromString("org.gtk.example"),
GApplicationFlags.G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, ACTIVATE, G_CALLBACK
((delegate* <gtkapplication*, void*, void>)&Activate), null);
int argc = args.Length;
sbyte** argv = SByteDoublePointersFromStringArray(args);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
That is tricky and you can develop with C/C++ like main
function with int
and char*
or char*[]
, etc. into C#.
sbyte**
to string[]
(string[]
from sbyte**
)
No code available. You know it just works like arguments/parameters from shared library example: Half-Life Sharp (still work in process).
Points of Interest
I find it very funny because ClangSharpPInvokeGenerator
made const char*
or char**
into sbyte*
or sbyte**
because C/C++ <-> C# are not the same but for WinAPI is very sensitive because WinAPI doesn't support for sbyte*
/ sbyte** just uses char*
/ char**
like in C/C++.
I will look for char*
/char**
in future. Because WinApi shows impossible language as Chinese like this:
Since it is sensitive, I need to fix for UTF8 or ANSI format in char*
/char**
.
History
I started with SDL2-CS by Ethan Lee. But his SDL2-CS cannot use NativeAot
. That is why I reworked into DeafMan1983.Interop.SDL2
with full pointers like in C/C++ because it works fine for NativeAot
support.
Nuget Gallery
dotnet add package DeafMan1983.Conversion
It uses sbyte*<->string
and sbyte**<->string[]
.
If you want to check my Nuget packages, you can visit my packages.
Have fun and happy coding!
I am deaf man, I developed any SDL2(100 %)/3(WIP), GLFW3, Cairo and SByte/Byte Conversions Wrappers/PureCodes for C#/Net.
If you visit my Nuget's Gallary ( https://www.nuget.org/profiles/DeafMan1983 )