Beispiel #1
0
        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}");
                    }
                }
            }
        }