Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#
Tip/Trick

Explanation with SByte*/Byte* and SByte**/Byte** Conversion

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Jan 2024CPOL2 min read 1.8K   1   2
SByte*/Byte* and SByte**/Byte** Conversion
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):

C#
// Example for title in SDL_CreateWindow()
sbyte* title = SBytePointerFromString("HelloSDL");
...
SDL_Window* window = SDL_CreateWindow(title, ... );

Or sbyte* to string (string from sbyte*).

C#
// Example for printf()

[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[])

C#
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:

 

Image 1

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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
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 )

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA18-Jan-24 5:30
professionalȘtefan-Mihai MOGA18-Jan-24 5:30 
GeneralRe: My vote of 5 Pin
Jens Eckervogt18-Jan-24 12:08
Jens Eckervogt18-Jan-24 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.