检查系统中是否存在DLL

发布于 2024-08-21 17:17:12 字数 203 浏览 6 评论 0原文

快速提问。我想查明执行我的应用程序的系统中是否存在 DLL。

这在 C# 中可能吗? (以一种适用于所有 Windows 操作系统的方式?)

对于 DLL,我的意思是非 .NET 经典 DLL(Win32 DLL)

(基本上我想进行检查,因为我使用的 DLL 可能是也可能不是存在于用户系统上,但我不希望应用程序在不存在时崩溃而没有警告:P)

quick question. I want to find out if a DLL is present in the system where my application is executing.

Is this possible in C#? (in a way that would work on ALL Windows OS?)

For DLL i mean a non-.NET classic dll (a Win32 dll)

(Basically I want to make a check cause I'm using a DLL that may or may not be present on the user system, but I don't want the app to crash without warning when this is not present :P)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

香草可樂 2024-08-28 17:17:12

调用 LoadLibrary API函数:

[DllImport("kernel32", SetLastError=true)]
static extern IntPtr LoadLibrary(string lpFileName);

static bool CheckLibrary(string fileName) {
    return LoadLibrary(fileName) == IntPtr.Zero;
}

Call the LoadLibrary API function:

[DllImport("kernel32", SetLastError=true)]
static extern IntPtr LoadLibrary(string lpFileName);

static bool CheckLibrary(string fileName) {
    return LoadLibrary(fileName) == IntPtr.Zero;
}
↘紸啶 2024-08-28 17:17:12

在 .NET 中使用平台调用时,您可以使用 Marshal.PrelinkAll(Type) 方法:

设置任务提供早期初始化并执行
当目标方法被调用时自动执行。首次任务
包括以下内容:

验证平台调用元数据的格式是否正确。

验证所有托管类型是否都是平台的有效参数
调用函数。

定位非托管 DLL 并将其加载到进程中。

定位流程中的入口点。

正如您所看到的,除了 dll 是否存在之外,它还执行其他检查,例如定位入口点(例如 SomeMethod()SomeMethod2() 是否实际存在于进程中就像下面的代码一样)。

using System.Runtime.InteropServices;

public class MY_PINVOKES
{
    [DllImport("some.dll")]
    private static void SomeMethod();

    [DllImport("some.dll")]
    private static void SomeMethod2();
}

然后使用 try/catch 策略来执行检查:

try
{
    // MY_PINVOKES class where P/Invokes are
    Marshal.PrelinkAll( typeof( MY_PINVOKES) );
}
catch
{
    // Handle error, DLL or Method may not exist
}

When using platform invoke calls in .NET, you could use the Marshal.PrelinkAll(Type) method:

Setup tasks provide early initialization and are performed
automatically when the target method is invoked. First-time tasks
include the following:

Verifying that the platform invoke metadata is correctly formatted.

Verifying that all the managed types are valid parameters of platform
invoke functions.

Locating and loading the unmanaged DLL into the process.

Locating the entry point in the process.

As you can see, it performs additional checks other than if the dll exists, like locating the entry points (e.g if SomeMethod() and SomeMethod2() actually exist in the process like in the following code).

using System.Runtime.InteropServices;

public class MY_PINVOKES
{
    [DllImport("some.dll")]
    private static void SomeMethod();

    [DllImport("some.dll")]
    private static void SomeMethod2();
}

Then use a try/catch strategy to perform your check:

try
{
    // MY_PINVOKES class where P/Invokes are
    Marshal.PrelinkAll( typeof( MY_PINVOKES) );
}
catch
{
    // Handle error, DLL or Method may not exist
}
葬花如无物 2024-08-28 17:17:12

实际上它不会抛出FileNotFoundException

此外,还需要在多个位置检查路径,对于 LoadLibrary,

.net 中有一个标准异常,它派生自 TypeLoadException,即 DllNotFoundException。 。

最好的方法是在 try..catch 中包装方法/PInvoke 调用并处理 DllNotFoundException,因为 .net 将检查应用程序路径以及设置为 PATH OS 环境变量一部分的任何其他路径。

[DllImport("some.dll")]
private static void SomeMethod();

public static void SomeMethodWrapper() {
try {
      SomeMethod();
    } catch (DllNotFoundException) {
    // Handle your logic here
  }
}

Actually it does not throw FileNotFoundException.

Also for that one needs to check in multiple places for path, for the LoadLibrary

There is a standard exception in .net the is derived from TypeLoadException, that is DllNotFoundException.

Best way is to wrap a method/PInvoke call in try..catch and handle the DllNotFoundException since .net will check for application path as well as any other paths set as part of PATH OS Environment variable.

[DllImport("some.dll")]
private static void SomeMethod();

public static void SomeMethodWrapper() {
try {
      SomeMethod();
    } catch (DllNotFoundException) {
    // Handle your logic here
  }
}
初见 2024-08-28 17:17:12

我假设这是一个 PInvoke 调用?

如果是这样,确定文件是否存在的最简单方法是进行调用并捕获文件不存在时产生的异常。

[DllImport("some.dll")]
private static void SomeMethod();

public static void SomeMethodWrapper() {
  try {
    SomeMethod();
  } catch (DllNotFoundException) {
    // Do Nothing 
  }
}

I'm assuming this is a PInvoke call?

If so the easiest way to make this determine if it's present is to make the call and catch the exception that results if the file does not exist.

[DllImport("some.dll")]
private static void SomeMethod();

public static void SomeMethodWrapper() {
  try {
    SomeMethod();
  } catch (DllNotFoundException) {
    // Do Nothing 
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文