C ++从子目录加载DLL? [英] C++ Load DLL From a Subdirectory?

查看:128
本文介绍了C ++从子目录加载DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的隐藏/黑暗场所"的新手,我想知道如何从运行当前可执行文件的目录中的其他目录或子目录中加载.dll文件

I'm new to the "hidden/dark places" of C++ and I was wondering how to load a .dll file from a different directory or a sub-directory inside the one where my current executable is running

例如:

./MyAppDirectory
  /MyApp.exe
  /SomeDLL.dll
  /AnotherDLL.dll
  /SubDirectory
    /SomeDLL2.dll
    /AnotherDLL2.dll
    /YetAnotherDLL.dll
    /...

因此,"MyApp.exe"会自动从其根文件夹"MyAppDirectory"中加载"SomeDLL.dll"和"AnotherDLL.dll",但是我也希望能够加载"SomeDLL2.dll","AnotherDLL2.dll", "MyAppDirectory"文件夹内"SubDirectory"文件夹中的"YetAnotherDLL.dll"等.

So "MyApp.exe" automatically loads "SomeDLL.dll" and "AnotherDLL.dll" from it's root folder "MyAppDirectory" however I also want to be able to load "SomeDLL2.dll", "AnotherDLL2.dll", "YetAnotherDLL.dll" etc. from the "SubDirectory" folder inside the "MyAppDirectory" folder.

我一直在进行搜索,从中我发现唯一的解决方法是:

I've been doing some searches and from what I've found the only solutions are:

  • 1)修改可执行文件的工作目录.
  • 2)将DLL文件放在Windows根目录下.
  • 3)修改PATH环境变量.

但是他们所有人都有一些不好的一面(在这里不值得一提),这并不是我真正需要的.另一种解决方案是通过应用程序专用路径!".这涉及使用Windows注册表,并且似乎比前面提到的要好一些.

But all of them have some bad sides (not worth mentioning here) and it's not what I actually need. Also another solution is through "Application Specific Paths!" which involves working with Windows registry and seems the be slightly better then the ones mentioned before.

但是我需要能够使用C ++在"MyApp.exe"内部执行此操作,而无需使用外部方法.

However I need to be able to do this inside "MyApp.exe" using C++ without the need to use external methods.

我正在使用MinGW 4.7.2,我的IDE是Code :: Blocks 12.11,我的操作系统是Windows XP SP3 Pro x86.

I'm using MinGW 4.7.2 and my IDE is Code::Blocks 12.11 also my OS is Windows XP SP3 Pro x86.

任何参考资料,教程,文档,示例等均被接受,感谢您的宝贵时间:D

Any reference, tutorial, documentation, example etc. is accepted and thank you for your time :D

推荐答案

如果未显式加载DLL(手动",在代码中使用LoadLibrary(...)),则必须将.dll放在一个位置Windows将寻找DLL的对象,这在很大程度上意味着您正在谈论的三个选项之一.

If you are NOT explicitly loading the DLL ("manually", in your code using LoadLibrary(...)), then you HAVE to have the .dll in a place that Windows will look for DLL's, whihc pretty much means one of the three options you are talking about in your question.

使用LoadLibrary时,可以指定DLL的相对或绝对路径.

When using LoadLibrary, you can specify a relative or absolute path to the DLL.

请注意,显式和隐式加载DLL完全不同-在显式情况下,必须使用LoadLibrary,然后使用GetProcAddress查找函数的地址,并且必须使用函数调用函数的指针-通常仅用于DLL或提供少量函数的插件或类似功能(很多情况下,这只是一个工厂函数,用于创建对象以执行具有通用接口类的操作,而每个DLL具有相同类型的功能,可创建对象以执行应做的事情.

Note that it's completely different to load DLL's explicitly and implicitly - in the explicit case, you have to use the LoadLibrary, and then use GetProcAddress to find the address of the function, and you will have to use function pointers to call the functions - this is typically only used for plug-ins or similar functionality where the DLL provides a small number of functions (quite often just a factory function to create a objects to do something that has a generic interface class, and each DLL has the same type of function to create an object to do whatever it is supposed to do).

在隐式加载中,您无需在代码中进行任何操作即可使用DLL,并且DLL中的功能似乎就在那里,就好像它们已硬链接到应用程序一样.

In the implicit load, you don't need to do anything in your code to use the DLL, and the functions from the DLL just appear to be there as if they were hard-linked into the application.

这篇关于C ++从子目录加载DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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