Setup safer function loading so the bindings can be used on older versions of Gtk

This commit is contained in:
cra0zy 2018-01-18 18:44:33 +01:00
parent af97186201
commit 8b5cb38bcb
5 changed files with 13 additions and 5 deletions

View File

@ -61,8 +61,16 @@ class FuncLoader
ret = Linux.dlsym(library, function); ret = Linux.dlsym(library, function);
if (ret == IntPtr.Zero) if (ret == IntPtr.Zero)
throw new EntryPointNotFoundException(function); Console.WriteLine("[WARNING] Function not found: " + function);
return ret; return ret;
} }
public static T LoadFunction<T>(IntPtr procaddress)
{
if (procaddress == IntPtr.Zero)
return default(T);
return Marshal.GetDelegateForFunctionPointer<T>(procaddress);
}
} }

View File

@ -66,7 +66,7 @@ namespace GtkSharp.Generation {
void GenerateImport (StreamWriter sw) void GenerateImport (StreamWriter sw)
{ {
sw.WriteLine("\t\tdelegate IntPtr d_{0}({1});", CName, Parameters.ImportSignature); sw.WriteLine("\t\tdelegate IntPtr d_{0}({1});", CName, Parameters.ImportSignature);
sw.WriteLine("\t\tstatic d_{0} {0} = Marshal.GetDelegateForFunctionPointer<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", CName, LibraryName); sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", CName, LibraryName);
sw.WriteLine(); sw.WriteLine();
} }

View File

@ -119,7 +119,7 @@ namespace GtkSharp.Generation {
sw.WriteLine ("\tinternal class " + Name + "GType {"); sw.WriteLine ("\tinternal class " + Name + "GType {");
var funcname = Elem.GetAttribute("gtype"); var funcname = Elem.GetAttribute("gtype");
sw.WriteLine ("\t\tdelegate IntPtr d_" + funcname + "();"); sw.WriteLine ("\t\tdelegate IntPtr d_" + funcname + "();");
sw.WriteLine ("\t\tstatic d_" + funcname + " " + funcname + " = Marshal.GetDelegateForFunctionPointer<d_" + funcname + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + funcname + "\"));"); sw.WriteLine ("\t\tstatic d_" + funcname + " " + funcname + " = FuncLoader.LoadFunction<d_" + funcname + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + funcname + "\"));");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t\tpublic static GLib.GType GType {"); sw.WriteLine ("\t\tpublic static GLib.GType GType {");
sw.WriteLine ("\t\t\tget {"); sw.WriteLine ("\t\t\tget {");

View File

@ -196,7 +196,7 @@ namespace GtkSharp.Generation {
sw.WriteLine("\t\tdelegate " + retval.CSType + " d_" + CName + "(" + import_sig + ");"); sw.WriteLine("\t\tdelegate " + retval.CSType + " d_" + CName + "(" + import_sig + ");");
else else
sw.WriteLine("\t\tdelegate " + retval.MarshalType + " d_" + CName + "(" + import_sig + ");"); sw.WriteLine("\t\tdelegate " + retval.MarshalType + " d_" + CName + "(" + import_sig + ");");
sw.WriteLine("\t\tstatic d_" + CName + " " + CName + " = Marshal.GetDelegateForFunctionPointer<d_" + CName + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + CName + "\"));"); sw.WriteLine("\t\tstatic d_" + CName + " " + CName + " = FuncLoader.LoadFunction<d_" + CName + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + CName + "\"));");
sw.WriteLine(); sw.WriteLine();
} }

View File

@ -186,7 +186,7 @@ namespace GtkSharp.Generation {
#endif #endif
if (set_gvalue != null) { if (set_gvalue != null) {
sw.WriteLine("\t\tdelegate IntPtr d_{0}(ref GLib.Value val, IntPtr obj);", set_gvalue.CName); sw.WriteLine("\t\tdelegate IntPtr d_{0}(ref GLib.Value val, IntPtr obj);", set_gvalue.CName);
sw.WriteLine("\t\tstatic d_{0} {0} = Marshal.GetDelegateForFunctionPointer<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", set_gvalue.CName, LibraryName); sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", set_gvalue.CName, LibraryName);
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t\tpublic void SetGValue (ref GLib.Value val)"); sw.WriteLine ("\t\tpublic void SetGValue (ref GLib.Value val)");
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");