在程序启动时从其他目录加载一个DLL [英] Load a DLL from another directory at program start

查看:552
本文介绍了在程序启动时从其他目录加载一个DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本问题是这样的:我的程序(MyProgram.exe)对另一个程序(OtherProgram)的DLL有依赖关系,我试图避免每次OtherProgram更新时重新打包一个新的DLL。我想在OtherProgram的DLL中启动MyProgram.exe链接,但我并不完全确定Windows允许这样做。所以如果有某种解决方法也是可以接受的。

My basic issue is this: my program (MyProgram.exe) has a dependency on a DLL from another program (OtherProgram), and I'm trying to avoid repackaging a new DLL every time OtherProgram updates. I'd like to have MyProgram.exe link in OtherProgram's DLL when it launches, but I'm not completely sure that Windows allows for this. So if there is some kind of workaround that would also be acceptable.

只是为了一些背景,平台是Windows 7 x64,当MyProgram.exe运行正常,当我在MyProgram.exe项目目录中创建一个符号链接到OtherProgram的安装目录中的DLL。当我尝试运行它没有符号链接,我得到程序无法启动,因为OtherProgramDLL.dll是从您的计算机缺少错误。

And just for some background, the platform is Windows 7 x64, and MyProgram.exe runs fine when I create a symlink in the MyProgram.exe project directory to the DLL in OtherProgram's install directory. When I try to run it without the symlink, I get the "program can't start because OtherProgramDLL.dll is missing from your computer" error.

任何建议或链接相关信息非常感谢!

Any advice or links to relevant info is greatly appreciated!

编辑:澄清:DLL在编译时没有链接,这个问题在运行时崩溃

Clarification: the DLL is not linked at compile-time, this issue crops up at runtime

推荐答案

Windows世界中有两种类型的动态链接:

There are two types of dynamic linking in the Windows world:


  1. 加载时间链接是当程序启动时自动加载DLL。 Windows使用我将在下面讨论的特定算法找到此DLL。

  2. 运行时链接是当您通过调用 LoadLibrary来加载DLL 在你的代码。类似的规则适用于图书馆如何找到,但您可以指定一个完全限定或相对有限的路径来控制搜索。

  1. Load-Time linking is when a DLL is loaded automatically when your program starts up. Windows finds this DLL using a specific algorithm I'll discuss below.
  2. Run-Time linking is when you specifically load a DLL by calling LoadLibrary in your code. Similar rules apply as to how the library is found, but you can specify a fully-qualified or relatively-qualified path to control the search.

在加载时间链接的情况下,MS建议您的程序的DLL存储在加载应用程序的同一目录中并从其加载。如果这是可行的,这可能是你最好的选择。

In the case of Load-Time linking, MS recommends that your program's DLLs are stored in and loaded from the same directory where your application is loaded from. If this is at all workable, this is probably your best option.

如果这不起作用,还有其他几个选项,概述在这里。一个是通过将DLL放在工作目录或应用程序加载目录中来利用搜索顺序。

If that doesn't work, there are several other options, outlined here. One is to leverage the search order by putting the DLL in either the working directory or the directory where the application was loaded from.

您可以通过以下方式更改应用程序的工作目录:

You can change the working directory of an application by:


  1. 创建一个

  2. 打开快捷方式的属性

  3. 使用DLL所在的目录编辑开始属性。 li>
  1. Create a shortcut to your application.
  2. Bring up the shortcut's properties
  3. Edit the "Start in" property with the directory where the DLL is located.

当您使用快捷方式启动应用程序时,它将加载正确的DLL。

When you launch your application using the shortcut, it will load the right DLL.

加载时链接的其他选项包括:

Other options for load-time linking include:


  • 向您的应用程序添加一个清单,指定您的依赖关系程序集是,或,

  • 设置 PATH

  • Adding a manifest to your application which specifies where your dependent assemblies are, or,
  • Setting the PATH.

这篇关于在程序启动时从其他目录加载一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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