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**
.
UPDATE: char* to string, string to char* and length of char* released
WARNING: Package will move to "DeafMan198.Utilities"
UtilitiesForUTF16
-> char*
to string
, string
to char*
and length of char*
( char**
to string[]
and string[]
to char**
and more will come soon... )
UtilitiesForSByte
-> sbyte*
to string and string to sbyte*
( length of sbyte
comes soon )
UtilitiesForByte
-> byte*
to string
and string
to byte*
( length of byte
comes soon )
I have reworked hard with char* (UTF16) to string (UTF8)
Example: For WinAPI ( TerraFX.Interop.Windows )
How do I get successful with WinAPI under C#.
Example:
...
static string DesktopAppStr = "DesktopApp";
static string TitleStr = "Windows Desktop Guided Tour Application";
...
HWND hWnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
StringToCharPointer(DesktopAppStr),
StringToCharPointer(TitleStr),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
HWND.NULL,
HMENU.NULL,
hInstance,
null
);
...
That is all for WinAPI. I am very excited to see if Chinese Language won't show up. Yay!
Enjoy your reading if I release package for char* and string and length of char*.
For byte*
<-> string
:
byte* StringToBytePointer(string input)
string BytePointerToString(byte* ptr)
int BytePointerLength(byte* ptr)
byte** StringArrayToByteDoublePointer(string[] input)
string[] ByteDoublePointerToStringArray(byte** ptrArray)
Int ByteDoublePointerLength(byte** ptrArray)
sbyte**
<-> string
:
sbyte* StringToSBytePointer(string input)
string SBytePointerToString(sbyte* ptr)
int SBytePointerLength(sbyte* ptr)
sbyte** StringArrayToSByteDoublePointer(string[] input)
string[] SByteDoublePointerToStringArray(sbyte** ptrArray)
Int SByteDoublePointerLength(sbyte** ptrArray)
char*
<-> string
:
char* StringToCharPointer(string input)
string CharPointerToString(char* ptr)
int CharPointerLength(char* ptr)
char** StringArrayToCharDoublePointer(string[] input)
string[] CharDoublePointerToStringArray(char** ptrArray)
Int CharDoublePointerLength(char** ptrArray)
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. And new version comes with char*
and more new features. It works as well with TerraFX.Interop.Windows
.
Add missed explanation about functions.
Nuget Gallery
It will come soon. ( I am working currently. Sorry for hesitation! )
Check out Nuget Package
dotnet add package DeafMan1983.Utilities
It uses sbyte*<->string
,sbyte**<->string[],
and new char´*<->string
and more new ...
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 )