public unsafe void LibraryNames() { var methods = typeof(LibC).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in methods) { DllImportAttribute dllImportAttribute = method.GetCustomAttribute <DllImportAttribute>(); if (dllImportAttribute != null) { string functionName = dllImportAttribute.EntryPoint; string libName = dllImportAttribute.Value; IntPtr libHandle = IntPtr.Zero; lock (s_dlopened) { if (!s_dlopened.TryGetValue(libName, out libHandle)) { fixed(byte *filename = Encoding.UTF8.GetBytes(libName)) { libHandle = new IntPtr(LibC.dlopen(filename, LibC.RTLD_LAZY)); } Assert.True(IntPtr.Zero != libHandle, $"Library not found: {libName}"); s_dlopened.Add(libName, libHandle); } } fixed(byte *symbol = Encoding.UTF8.GetBytes(functionName)) { IntPtr symbolHandle = new IntPtr(LibC.dlsym(libHandle.ToPointer(), symbol)); Assert.True(IntPtr.Zero != symbolHandle, $"Function symbol not found in {libName}: {functionName}"); } } } }
private static IntPtr dlopen(string libraryName, int flags) { byte[] libNameBytes = Encoding.UTF8.GetBytes(libraryName); fixed(byte *pName = libNameBytes) { return(new IntPtr(LibC.dlopen(pName, flags))); } }
private static IntPtr dlopen(string libraryName, int flags) { Console.WriteLine($"Open Library: {libraryName} {flags}"); byte[] libNameBytes = Encoding.UTF8.GetBytes(libraryName); fixed(byte *pName = libNameBytes) { Console.WriteLine($"Found dlopen: {LibraryName} {flags}"); return(new IntPtr(LibC.dlopen(pName, flags))); } }