Exemple #1
0
        /// <summary>
        /// gets the address of a symbol (function or global variable) from an unmanaged library
        /// </summary>
        /// <param name="SymbName"></param>
        /// <summary>
        /// releases a dynamic library
        /// </summary>
        /// <param name="libHandle">
        /// handle, which was acquired by <see cref="LoadDynLib"/>
        /// </param>
        /// <returns>
        /// a function/symbol pointer;<br/>
        /// using the function <see cref="System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer"/>
        /// it can be converted to a .NET delegate.
        /// </returns>
        /// <remarks>
        /// On Windows, this function redirects to the 'GetProcAddress'-function in kernel32.dll;<br/>
        /// On Unix, this function redirects to the 'dlsym'-function in libdl.so;
        /// </remarks>
        /// <param name="errstr">
        /// on success, null;
        /// if call failed (return value is null), an error information provided by the operating system
        /// </param>
        public static IntPtr LoadSymbol(DynLibHandle libHandle, string SymbName, out string errstr)
        {
            PlatformID plattid = System.Environment.OSVersion.Platform;
            IntPtr     ret;

            errstr = null;

            Debug.Assert(Marshal.SizeOf(typeof(DynLibHandle)) == IntPtr.Size, "runtime created struct of wrong size.");

            switch (plattid)
            {
            case PlatformID.Win32NT:
                ret = GetProcAddress(libHandle, SymbName);
                if (ret == IntPtr.Zero)
                {
                    errstr = GetLastWin32Error();
                }
                return(ret);

            case PlatformID.Unix:
                //Console.WriteLine("Trying to load >" + SymbName + "< form lib >" + libHandle.val + "< ...");
                ret = dlsym(libHandle, SymbName);
                if (ret == IntPtr.Zero)
                {
                    errstr = _dlerror();
                }
                return(ret);

            default:
                throw new NotImplementedException("Symbol Loading for " + plattid + " is not supported.");
            }
        }
Exemple #2
0
        /// <summary>
        /// releases a dynamic library
        /// </summary>
        /// <param name="libHandle">
        /// handle, which was acquired by <see cref="LoadDynLib"/>
        /// </param>
        /// <remarks>
        /// On Windows, this function calls the 'FreeLibrary'-function in kernel32.dll;<br/>
        /// On Unix, this function calls the 'dlclose'-function in libdl.so;
        /// </remarks>
        public static void UnloadDynLib(DynLibHandle libHandle)
        {
            PlatformID plattid = System.Environment.OSVersion.Platform;

            switch (plattid)
            {
            case PlatformID.Win32NT:
                FreeLibrary(libHandle);
                break;

            case PlatformID.Unix:
                dlclose(libHandle);
                break;

            default:
                throw new NotImplementedException("Dynamic Library Release for " + plattid + " is not supported.");
            }
        }
Exemple #3
0
 static extern int dlclose(DynLibHandle handle);
Exemple #4
0
 static extern IntPtr GetProcAddress(DynLibHandle hModule, [MarshalAsAttribute(UnmanagedType.LPStr)] string lpProcName);
Exemple #5
0
 unsafe static extern IntPtr dlsym(DynLibHandle handle, string symbol);
Exemple #6
0
 static extern bool FreeLibrary([InAttribute()] DynLibHandle hModule);