예제 #1
0
        ///////////////////////////////////////////////////////////////////////

        private static bool UnloadNativeLibrary(
            Interpreter interpreter /* NOT USED */
            )
        {
            lock (syncRoot) /* TRANSACTIONAL */
            {
                if (nativeModule == IntPtr.Zero)
                {
                    return(true);
                }

                try
                {
                    UnsetNativeDelegates();

                    int lastError;

                    if (NativeOps.FreeLibrary(
                            nativeModule, out lastError)) /* throw */
                    {
                        nativeModule   = IntPtr.Zero;
                        nativeFileName = null;

                        TraceOps.DebugTrace(
                            "UnloadNativeLibrary: successfully unloaded",
                            typeof(NativeUtility).Name,
                            TracePriority.NativeDebug);

                        return(true);
                    }
                    else
                    {
                        TraceOps.DebugTrace(String.Format(
                                                "FreeLibrary(0x{1:X}) failed with error {0}: {2}",
                                                lastError, nativeModule,
                                                NativeOps.GetDynamicLoadingError(lastError).Trim()),
                                            typeof(NativeUtility).Name,
                                            TracePriority.NativeError);
                    }
                }
                catch (Exception e)
                {
                    TraceOps.DebugTrace(
                        e, typeof(NativeUtility).Name,
                        TracePriority.NativeError);
                }

                return(false);
            }
        }
예제 #2
0
        ///////////////////////////////////////////////////////////////////////

        #region Public Methods (Internal Use Only)
        public ReturnCode UnloadNoThrow( /* EXEMPT: object-15.11 */
            ref int loaded,
            ref Result error
            )
        {
            // CheckDisposed(); /* EXEMPT */

            lock (syncRoot) /* TRANSACTIONAL */
            {
                //
                // NOTE: If the module was already loaded previously,
                //       do nothing.
                //
                if (module == IntPtr.Zero)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If there are still outstanding references to
                //       the native module, do nothing.
                //
                if (Interlocked.Decrement(ref referenceCount) > 0)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If the native module has been locked in place
                //       (because it cannot be cleanly unloaded?), then
                //       leave it alone.
                //
                if (FlagOps.HasFlags(
                        flags, ModuleFlags.NoUnload, true))
                {
                    return(ReturnCode.Ok);
                }

                try
                {
                    int lastError;

                    if (NativeOps.FreeLibrary(
                            module, out lastError)) /* throw */
                    {
                        Interlocked.Decrement(ref loaded);

                        module = IntPtr.Zero;
                        return(ReturnCode.Ok);
                    }
                    else
                    {
                        error = String.Format(
                            "FreeLibrary(0x{1:X}) failed with error {0}: {2}",
                            lastError, module, NativeOps.GetDynamicLoadingError(
                                lastError));
                    }
                }
                catch (Exception e)
                {
                    error = e;
                }
            }

            return(ReturnCode.Error);
        }