检查项目引用(dll)是否存在 [英] Check if project reference (dll) is existent

查看:66
本文介绍了检查项目引用(dll)是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我已经为这个问题苦苦挣扎了两天:

我正在使用Visual Studio C ++ 2005,并且已在我的项目中添加了引用(在项目属性中).此参考是我用C#编译的dll,其中包含一些类.到目前为止一切正常.我可以按预期访问类.因此,dll是除可执行程序文件之外的单个文件.
如果我在运行时(或之前)删除dll,则当我尝试访问dll时程序崩溃.我想知道的第一件事是为什么dotnet框架在程序启动时不检查所有引用是否可用?

无论如何,在每次访问dll之前,我都尝试执行自己的检查dll是否存在.因此,我编写了以下代码:

Hi!

I''ve been struggling for two days with this problem:

I''m working with Visual Studio C++ 2005 and have added a reference (in the project properties) to my project. This reference is a dll which I compiled in C# and contains some classes. Everything works fine so far. I can access to the classes as intended. The dll thereby is a single file besides the executable program file.
If I remove the dll during runtime (or before) the program crashes when I try to access the dll. The first thing I''m wondering about is why the dotnet framework doesn''t check at program startup if all references are available?

Anyways, i tried to implement my own check if the dll is existent before each access to the dll. Therefore I wrote this code:

private: System::Void Button_Click(System::Object^  sender, System::EventArgs^  e)
             {
                 if(!(System::IO::File::Exists("LiveDocs.dll")))
                 {
                    MsgBox_Error("Dll not found!");
                    return;
                 }

                 //return;

                 LiveDocs doc();
                 doc.Show(LiveDocs::liveDocType::Manual);
             }



LiveDocs是我在C#dll中的类.现在,如果缺少dll,我将得到一个我不理解的奇怪行为.如果我取消第二个return语句的注释,文件检查的结果是正确的,并且错误消息将正确抛出.但是,如果我尝试通过第一个return语句(第二个注释已注释)离开该函数,则程序将崩溃,并显示未找到dll的错误.
因此在我看来,即使没有执行相应的代码,LiveDocs的对象也已实例化.我认为这是在执行函数并且编译器知道可以执行代码时发生的.对于第二个return语句,它根本无法执行,因此将被忽略.我说的对吗?

但是,如何在运行时检查dll是否存在?有什么想法吗?

谢谢!
Mathias



Where LiveDocs is my class inside the C# dll. Now if the dll is missing I get a strange behaviour which I don''t understand. The result of the file check is correct and the error message is thrown correctly if I uncomment the second return statement. But if I try to leave the function by the first return statement (second one is commented) the programm crashes with an unhandled exeption saying the dll was not found.
So it seems to me as the object of LiveDocs is instantiated even if the according code is not executed. I think this happens when the function is executed and the compiler knows that the code MAY be executed. In case of the second return statement it cannot be executed at all so it is ignored. Am I right?

But how can I check if the dll is existent during runtime?? Any ideas?

Thank you!
Mathias

推荐答案

为什么不将代码放入try/catch块中并正确处理异常?

Why don''t you just put your code into a try/catch block and handle the exception properly?

private: System::Void Button_Click(System::Object^  sender, System::EventArgs^  e)
{
    try
    {
        LiveDocs doc();
        doc.Show(LiveDocs::liveDocType::Manual);
    }
    catch (SpecialExceptionThatIndicatesDllIsntPresent ex)
    {
        // display appropriate error message
    }
    catch (Exception ex)
    {
        // for any exception that you don't want to handle in a special way
        // display appropriate error message
    }
}


仅需注意:即使您在Visual Studio中使用项目引用(这是设置引用的最佳方法),在运行时的工作原理也类似于组装.

要在运行时测试引用,请对任何给定的程序集实例使用System.Reflection.Assembly.GetReferencedAssemblies.对于每个返回的System.Reflection.AssemblyName实例,您还可以选中System.Reflection.AssemblyName.CodeBase来与您拥有的可执行模块文件进行比较.

特别是,您可以在运行时将其与以下程序集一起使用:System.Reflection.Assembly.GetEntryAssembly()GetCallingAssembly()GetExecutingAssembly().

请参阅 http://msdn.microsoft.com/en-us/library/system.reflection .assembly.aspx [ ^ ].

如果不检查动态加载的程序集和本机P/调用的代码.有关本机代码,请使用Microsoft Dependency Walker ,请参见 http://en.wikipedia.org/wiki/Dependency_Walker [ ^ ],http://www.dependencywalker.com/ [ ^ ].

—SA
Just a note: even if you use project reference in Visual Studio (which is the best way to set references), during run-time is works like a reference to assembly.

To test references during run time, use System.Reflection.Assembly.GetReferencedAssemblies for any given assembly instance. For each returned instance of System.Reflection.AssemblyName you can also check System.Reflection.AssemblyName.CodeBase to compare with the executable module files you have.

In particular, you can use during run time it with the following assemblies: System.Reflection.Assembly.GetEntryAssembly(), GetCallingAssembly() or GetExecutingAssembly().

See http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

If does not check up for dynamically loaded assemblies and native P/Invoked code. For native code, use Microsoft Dependency Walker, see http://en.wikipedia.org/wiki/Dependency_Walker[^], http://www.dependencywalker.com/[^].

—SA


这篇关于检查项目引用(dll)是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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