FreeLibrary无限期地返回true [英] FreeLibrary returns true indefinitely

查看:188
本文介绍了FreeLibrary无限期地返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FreeLibrary为何会重复返回true?

Is there any reason why FreeLibrary would repetitively return true?

我正在尝试从进程中卸载某些本机dll,所以我得到了它的句柄,然后调用FreeLibrary,直到引用计数变为零,因此FreeLibrary返回false ...,但从未这样做:

I'm trying to unload some native dll from my process, so I'm getting it's handle and then calling FreeLibrary until the ref count goes to zero and so FreeLibrary returns false... but it never does:

IntPtr pDll = DllLoadingImports.LoadLibrary(dllTounLoad);
//throw if pDll == IntPtr.Zero
while(DllLoadingImports.FreeLibrary(pDll));

代码运行并且永远不会返回. 进程浏览器还会显示仍在加载的dll.

The code runs and never returns. Also the process explorer shows the dll still loaded.

更多背景:

我正在尝试卸载使用DllImport加载的本机库,并且正在使用此处介绍的技巧: https://stackoverflow .com/a/2445558/2308106 它只是用于原型设计,因此我不必担心可能的后果...但是我很困惑为什么图书馆不会卸载

I'm trying to unload native library loaded by use of DllImport and I'm using trick described here: https://stackoverflow.com/a/2445558/2308106 It's for prototyping purposes so I don't have to care about possible consequnces... but I'm puzzled why the library won't unload

编辑1 : 我发现可以通过在 GetModuleHandleEx 函数(可以在dll加载时从DllMain内部调用).

Edit 1: I found that similar behavior can be achieved by specifying GET_MODULE_HANDLE_EX_FLAG_PIN flag in GetModuleHandleEx function (which can be called from within DllMain on dll load).

我要卸载的dll是python.dll(更确切地说是python36.dll).但是尚未在python源代码中找到此标志的用法. DllMain本身为空.

The dll I'm trying to unload is python.dll (more precisely python36.dll). But haven't found usage of this flag within python source code. DllMain itself is empty.

编辑2 : 我被要求提供最小的可执行文件示例-因此,它去了: 它使用pythonnet库(版本2.3.0)-这是PythonEngine调用.

Edit 2: I was asked for minimum executable example - so here it goes: It uses pythonnet library (version 2.3.0) - that's the PythonEngine calls.

[TestFixture]
public class PythonUnloadTest
{
    public static class DllImports
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        [DllImport("kernel32.dll")]
        public static extern bool FreeLibrary(IntPtr hModule);
    }

    [Test]
    public void PythonLoadUnload()
    {
        const string PythonDll = @"PythonEngine\python36";

        PythonEngine.Initialize();
        //locking etc not included for simplicity

        //Replace module with 'sys' (or some others) and dll can be unloaded
        var module = PythonEngine.ImportModule("numpy");
        module.Dispose();

        IntPtr pythonDllHandle = DllImports.LoadLibrary(PythonDll);
        if (pythonDllHandle == IntPtr.Zero)
        {
            throw new Exception("Dll not loaded");
        }

        for (int i = 0; i < 100000; i++)
        {
            if (!DllImports.FreeLibrary(pythonDllHandle))
            {
                return;
            }
        }

        Assert.Fail("Python not unloaded");
    }
}

不管具体情况如何(python和pythonnet以及numpy的加载)-仍然需要某种现象来阻止进程通过调用FreeLibrary来卸载dll.我怀疑安装了某些挂钩,或使用上述标志调用GetModuleHandleEx ...我将尝试检查numpy源.但是,如果有任何具体提示,我应该寻找什么-我会很感激

Regardless of this specific case (python and pythonnet and loading of numpy) - there still needs to be some phenomenon that prevents process from being able to unload dlls by calling FreeLibrary. I'm suspecting instalment of some hooks or calling GetModuleHandleEx with above mentioned flag... I'll try to inspect numpy source. But if there are any specific tips what should I be looking for - I'd appreciate those

推荐答案

FreeLibrary为何会重复返回true?

Is there any reason why FreeLibrary would repetitively return true?

正如我已经进行的编辑一样-可能有几个原因:

As I already put in edits - there can be few reasons:

  • Specifying GET_MODULE_HANDLE_EX_FLAG_PIN flag in GetModuleHandleEx function. Module can even call this on itself in it's DllMain - rendering self unloadable.
  • Installing hooks. Module M, that installs hooks won't be unloaded untill process exits. Sources - e.g.: https://msdn.microsoft.com/en-us/library/ms644960(v=VS.85).aspx#system_events or https://blogs.msmvps.com/vandooren/2006/10/09/preventing-a-dll-from-being-unloaded-by-the-app-that-uses-it/

这篇关于FreeLibrary无限期地返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆