Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 未找到句柄dll异常_Vb.net_Dll_Exception Handling_Import - Fatal编程技术网

Vb.net 未找到句柄dll异常

Vb.net 未找到句柄dll异常,vb.net,dll,exception-handling,import,Vb.net,Dll,Exception Handling,Import,我目前的项目运作良好,所以像任何理智的人一样,我正试图故意打破它。一种可能是某些资源可能会丢失。当我忽略在run文件夹中放置dll时,我的应用程序崩溃得很厉害 是否可以“优雅地”处理因缺少资源而导致的异常? 我的一个类导入了一个引用“TcAdsDll.dll”的资源 在该类中,我能够捕获尝试使用此资源时生成的异常: Try Dim objAdsStateInfo As TwinCAT.Ads.StateInfo = Nothing Try

我目前的项目运作良好,所以像任何理智的人一样,我正试图故意打破它。一种可能是某些资源可能会丢失。当我忽略在run文件夹中放置dll时,我的应用程序崩溃得很厉害

是否可以“优雅地”处理因缺少资源而导致的异常?

我的一个类导入了一个引用“TcAdsDll.dll”的资源

在该类中,我能够捕获尝试使用此资源时生成的异常:

    Try
        Dim objAdsStateInfo As TwinCAT.Ads.StateInfo = Nothing
        Try
             'Do a bunch of fun stuff with Ethernet...
        Catch...  
        'catch Ethernet errors              
        End Try
    Catch ex As Exception
    'This catches the exception generated when I try to instantiate an object that uses the dll which is no longer present
    End Try
然而,在我处理了这个异常之后,程序仍然会崩溃。 一旦退出初始化阶段,将加载form main,我将一直到达该事件的结束子部分。我一执行'End Sub'语句(逐行调试),就会收到以下消息:

DllNotFoundException was unhandled
Unable to load 'tcadsdll.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)
我添加了一些代码,在对使用该.dll的资源执行任何操作之前检查该.dll是否存在,但由于它是通过imports语句链接到类中的,因此它仍会尝试对其进行处理并崩溃。我是否必须重建资源(Okuma.EthernetIO)以包含对dll文件的检查?或者,在我的应用程序中是否有一种优雅的方式可以轻松实现这一点,而我却不知道这一点

更新:跳入包含导入的类之前检查dll文件是否存在对我有效。它首先停止生成异常。然而,问题是:
有没有办法处理dll未找到异常?

您可以通过终止程序“优雅地”处理dll未找到异常:

Dim MissingDll as Boolean = False
Try
    'Do a bunch of fun stuff with Ethernet...
Catch ex As System.DllNotFoundException
    MissingDll = True
    MsgBox("Missing DLL", , "Fatal Error")
    Application.Exit()
End Try
在关闭代码中,使用MissingDll避免调用其他函数,这些函数也会导致“DllNotFoundException was unhandled”错误:


非常简单的启动程序应用程序,在启动前检查环境健全性的依赖性最小?也可以显示您的启动屏幕…或者在启动时和尝试使用之前测试所需文件的存在性。由于您将编写或已经编写了安装程序,因此您的应用程序可以知道在何处找到它们。@它正在启动时(初始化期间)进行检查。一个困难的问题是,我将功能划分为大约17个类。我的初始化例程是在Main表单的New()子表单中调用的。该初始化在另一个类中调用“InitEthernetIO()。这就是我在上面发布的代码。这个类包含有问题的资源的imports语句,而不是formmain语句。
Dim MissingDll as Boolean = False
Try
    'Do a bunch of fun stuff with Ethernet...
Catch ex As System.DllNotFoundException
    MissingDll = True
    MsgBox("Missing DLL", , "Fatal Error")
    Application.Exit()
End Try
If Not MissingDll Then EthernetIO.Close()  ' something like this