本文介绍了C#FreeLibrary()ArithmeticException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2012和C#WPF应用程序。我实际上正在尝试加载3个DLL,然后卸载它们。我必须这样做,因为我不能添加这些DLL作为我的C#项目的引用。
问题是我不断得到一个ArithmeticException与那些DLL。我需要所有这些,因为dll1需要dll2和dll3正常运行。
我已经执行了我的代码,这是发生了什么:
我的LoadLibrary ()确实找到所有的DLL。一旦我到达FreeLibrary(),没有错误,但是在我的代码结尾(完全是出门MainWindow()),我得到一个ArithmeticException。



我已经尝试将每个FreeLibrary()分开的,如果,看看其中一个是否没有正确执行,没有任何成功。

我也把每个FreeLibrary()放在一个中,以确保引用计数为0,但是我有另一个有趣的事情:我的应用程序锁在我最后的Dll上。它正确地执行了第一个和第二个,但是被困在最后一个。我必须手动停止应用程序,我无法继续我的一步一步,并没有一个单一的错误或任何在线程堆栈。



这是我的代码:

  public partial class MainWindow:Window 
{
[DllImport(kernel32 .dll)]
static extern IntPtr LoadLibrary(string dllToLoad);

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

public MainWindow()
{
InitializeComponent();
try
{
string dll1Path =dll / dll1.dll;
string dll2Path =dll / dll2.dll;
string dll3Path =dll / dll3.dll;

IntPtr dll3Link = LoadLibrary(dll3Path);
IntPtr dll2Link = LoadLibrary(dll2Path);
IntPtr dll1Link = LoadLibrary(dll1Path);

if(FreeLibrary(dll3Link)== true&& FreeLibrary(dll2Link)== true&& FreeLibrary(dll1Link)== true)
Console.WriteLine DLL已卸载);
else
Console.WriteLine(一个或多个DLL未正确卸载);

}
catch(Exception ex)
{
Console.WriteLine(ERROR:+ ex.Message);
}

}

}


解决方案

你的代码工作很好,对我来说你在哪里dll / dll1.dll,他们在你的bin / debug?


I am using Visual Studio 2012 and a C# WPF Application.
I am actually trying to load 3 DLLs and then unload them. I have to do this because I cannot add those DLL as a reference to my C# project.
The problem is that I keep getting an ArithmeticException with those DLL. I need all of them, because dll1 needs dll2 and dll3 to run properly.
I have been executed my code step by step, and here is what happend :
My LoadLibrary() do found all the DLLs. Once I get to the FreeLibrary(), there is no error, but at the end of my code (exactly, when going out of MainWindow()) I get an ArithmeticException.

I have already tried putting each of my FreeLibrary() in separated if to see if one of them were not executed correctly, without any success.
I have also put each of my FreeLibrary() in a while to be sure that the ref count goes to 0, but there I am having another interesting thing : my application get lock on my last Dll. It does the first and second while correctly, but get stuck to the last one. I have to manually stop the application, and I am not able to continue my step by step, and there is not a single error or whatever on the stacks of the threads.

Here is my code :

  public partial class MainWindow : Window
  {
    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string dllToLoad);

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

    public MainWindow()
    {
        InitializeComponent();
        try
        {
            string dll1Path = "dll/dll1.dll";
            string dll2Path = "dll/dll2.dll";
            string dll3Path = "dll/dll3.dll";

            IntPtr dll3Link = LoadLibrary(dll3Path);
            IntPtr dll2Link = LoadLibrary(dll2Path);
            IntPtr dll1Link = LoadLibrary(dll1Path);

            if(FreeLibrary(dll3Link) == true && FreeLibrary(dll2Link) == true && FreeLibrary(dll1Link) == true)
                 Console.WriteLine("All DLL have been unloaded");
            else
                 Console.WriteLine("One or more DLL were not unloaded properly");

            }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR : " + ex.Message);
        }

    }

}
解决方案

You code works just fine, for me. where is you dll/dll1.dll, are they in your bin/debug?

这篇关于C#FreeLibrary()ArithmeticException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:50