怎么让系统自己找到DLL??

几罗星人 2013-08-22 01:12:29
一般的标准DLL直接放到Exe目录下就可以使用。但是例如我自己设计一套库,里面的DLL都统一放在某个文件夹里。多个程序都使用这套库。问题是如果DLL不放到Exe目录下,系统怎么知道DLL在哪呢?我并不想写代码的时候指定绝对路径来说明DLL在哪,怎么能让DLL像系统的DLL一样只写个文件名系统就知道在哪去调用它?

假设不存在重复文件名的DLL,也不考虑不同版本的DLL共存的问题,这样一个文件名就是唯一的一个DLL文件了。
...全文
249 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
几罗星人 2013-08-26
  • 打赏
  • 举报
回复
凡是提到了PATH 环境变量中列出目录的都给了5分,基本上只有这个符合要求了
baichi4141 2013-08-25
  • 打赏
  • 举报
回复
1、当前进程的可执行模块所在的目录。 2、当前目录。 3、Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。 4、Windows 目录。GetWindowsDirectory 函数检索此目录的路径。 5、PATH 环境变量中列出的目录。 楼主不想放到第一条里面,第二条大概也用不上,那就用后三条好了 winxp的话,要么复制到system32文件夹里,要么放到某个文件夹里再把该文件夹路径加入到path环境变量中
几罗星人 2013-08-23
  • 打赏
  • 举报
回复
引用 16 楼 ananluowei 的回复:
昏过去了。你自己用的时候当然是用你自己的那个工具库目录,你自己执行的时候不用拷贝。 给客户是让客户放在exe下面的字目录中。你不需要改cpp重新编译,在你的机器和客户的机器一样都能找的到dll
这样跟你解释吧。你单独安装过C++可再发行包吧?或者单独装过FrameWork吧?他们安装的是一个路径。而是用C++可再发行包的程序却多数不在可再发行包安装的目录下,系统却一样可以找到。我要的是这种效果。
大尾巴猫 2013-08-22
  • 打赏
  • 举报
回复
自己所有的那些工具库和dll平时可以放在一个地方,便于管理 做1个exe的时候,要把对应的dll拷贝出来吧。在头文件中不能用你工具库的目录。 要不然,exe给用户的时候怎么办,要求他也建立一个和你一模一样的工具库的目录?
flyrack 2013-08-22
  • 打赏
  • 举报
回复
这么简单的问题 微软这么多年都没有给出一次解决方案么 难道对操作系统实现很困难么
wjjll123 2013-08-22
  • 打赏
  • 举报
回复
加环境变量或者 放在系统目录里面或者 放在exe同一个目录。 别的方法真的不知道了
ztenv 版主 2013-08-22
  • 打赏
  • 举报
回复
将你的目录加到系统环境变量
大尾巴猫 2013-08-22
  • 打赏
  • 举报
回复
引用 15 楼 JiLuoXingRen 的回复:
[quote=引用 14 楼 ananluowei 的回复:] 要么放在exe一个目录,要么放在exe下面建立的子目录,include的时候用相对路径
正是因为不想这样做,我只是想让DLL呆在一起,个人设计的程序共用这些库,而不是每一个程序都将DLL复制一次。就像单独安装的运行库一样[/quote] 昏过去了。你自己用的时候当然是用你自己的那个工具库目录,你自己执行的时候不用拷贝。 给客户是让客户放在exe下面的字目录中。你不需要改cpp重新编译,在你的机器和客户的机器一样都能找的到dll
几罗星人 2013-08-22
  • 打赏
  • 举报
回复
引用 14 楼 ananluowei 的回复:
要么放在exe一个目录,要么放在exe下面建立的子目录,include的时候用相对路径
正是因为不想这样做,我只是想让DLL呆在一起,个人设计的程序共用这些库,而不是每一个程序都将DLL复制一次。就像单独安装的运行库一样
大尾巴猫 2013-08-22
  • 打赏
  • 举报
回复
引用 13 楼 JiLuoXingRen 的回复:
[quote=引用 12 楼 ananluowei 的回复:] 是你自己在自己电脑用,还是生成exe了以后给别人装在别人机器用?
当然是发布出去啊,自己用随便怎么整都可以[/quote] 要么放在exe一个目录,要么放在exe下面建立的子目录,include的时候用相对路径
几罗星人 2013-08-22
  • 打赏
  • 举报
回复
引用 12 楼 ananluowei 的回复:
是你自己在自己电脑用,还是生成exe了以后给别人装在别人机器用?
当然是发布出去啊,自己用随便怎么整都可以
大尾巴猫 2013-08-22
  • 打赏
  • 举报
回复
引用 11 楼 JiLuoXingRen 的回复:
[quote=引用 10 楼 ananluowei 的回复:] 用vs的话可以为每个项目设定include目录和dll目录,就可以直接include了
即便如此,那也只是在设计的时候,一样存在找不到DLL的问题啊[/quote] 真搞不懂你的意图了。 是你自己在自己电脑用,还是生成exe了以后给别人装在别人机器用?
赵4老师 2013-08-22
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vccore/html/84bfb380-ad7b-4962-b2d0-51b19a45f1bb.htm Visual C++ Windows 用来定位 DLL 的搜索路径 请参见 发送反馈意见 通过隐式和显式链接,Windows 首先搜索“已知 DLL”,如 Kernel32.dll 和 User32.dll。Windows 然后按下列顺序搜索 DLL: 当前进程的可执行模块所在的目录。 当前目录。 Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。 Windows 目录。GetWindowsDirectory 函数检索此目录的路径。 PATH 环境变量中列出的目录。 注意: 未使用 LIBPATH 环境变量。 您希望做什么? 隐式链接 显式链接 确定要使用的链接方法 请参见 概念 DLL 发送反馈意见,就此主题向 Microsoft 发送反馈意见。
几罗星人 2013-08-22
  • 打赏
  • 举报
回复
引用 10 楼 ananluowei 的回复:
用vs的话可以为每个项目设定include目录和dll目录,就可以直接include了
即便如此,那也只是在设计的时候,一样存在找不到DLL的问题啊
大尾巴猫 2013-08-22
  • 打赏
  • 举报
回复
引用 9 楼 JiLuoXingRen 的回复:
[quote=引用 7 楼 ananluowei 的回复:] 自己所有的那些工具库和dll平时可以放在一个地方,便于管理 做1个exe的时候,要把对应的dll拷贝出来吧。在头文件中不能用你工具库的目录。 要不然,exe给用户的时候怎么办,要求他也建立一个和你一模一样的工具库的目录?
要把对应的dll拷贝出来。正是因为不想这样做,我只是想让DLL呆在一起,个人设计的程序共用这些库,而不是每一个程序都将DLL复制一次。就像单独安装的运行库一样[/quote] 用vs的话可以为每个项目设定include目录和dll目录,就可以直接include了
几罗星人 2013-08-22
  • 打赏
  • 举报
回复
引用 7 楼 ananluowei 的回复:
自己所有的那些工具库和dll平时可以放在一个地方,便于管理 做1个exe的时候,要把对应的dll拷贝出来吧。在头文件中不能用你工具库的目录。 要不然,exe给用户的时候怎么办,要求他也建立一个和你一模一样的工具库的目录?
要把对应的dll拷贝出来。正是因为不想这样做,我只是想让DLL呆在一起,个人设计的程序共用这些库,而不是每一个程序都将DLL复制一次。就像单独安装的运行库一样
木头菇 2013-08-22
  • 打赏
  • 举报
回复
Dynamic-Link Library Search Order A system can contain multiple versions of the same dynamic-link library (DLL). Applications can control the location from which a DLL is loaded by specifying a full path, using DLL redirection, or by using a manifest. If none of these methods are used, the system searches for the DLL at load time as described in this topic. Standard Search Order The dynamic-link library (DLL) search order used by the system depends on whether safe DLL search mode is enabled or disabled. Windows Vista, Windows Server 2003, and Windows XP SP2: Safe DLL search mode is enabled by default. To disable this feature, create the HKLM\System\CurrentControlSet\Control\Session Manager\SafeDllSearchMode registry value and set it to 0. Calling the SetDllDirectory function effectively disables SafeDllSearchMode while the specified directory is in the search path and changes the search order as described in this topic. Windows XP and Windows 2000 SP4: Safe DLL search mode is disabled by default. To enable this feature, create the SafeDllSearchMode registry value and set it to 1. If SafeDllSearchMode is enabled, the search order is as follows: The directory from which the application loaded. The system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The current directory. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. If SafeDllSearchMode is disabled, the search order is as follows: The directory from which the application loaded. The current directory. The system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. Note that versions of Windows prior to the ones listed at the beginning of this section do not support SafeDllSearchMode. For more information, see Legacy Search Order below. Alternate Search Order The standard search order used by the system can be changed by calling the LoadLibraryEx function with LOAD_WITH_ALTERED_SEARCH_PATH. The standard search order can also be changed by calling the SetDllDirectory function. Windows XP/2000 and Windows 2000 Server: Changing the standard search order by calling SetDllDirectory is not supported until Windows XP SP1 and Windows Server 2003. If you specify an alternate search strategy, its behavior continues until all associated executable modules have been located. After the system starts processing DLL initialization routines, the system reverts to the standard search strategy. The LoadLibraryEx function supports an alternate search order if the call specifies LOAD_WITH_ALTERED_SEARCH_PATH and the lpFileName parameter specifies an absolute path. Note that the standard search strategy and the alternate search strategy specified by LoadLibraryEx with LOAD_WITH_ALTERED_SEARCH_PATH differ in just one way: The standard search begins in the calling application's directory, and the alternate search begins in the directory of the executable module that LoadLibraryEx is loading. If SafeDllSearchMode is enabled, the alternate search order is as follows: The directory specified by lpFileName. The system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The current directory. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. If SafeDllSearchMode is disabled, the alternate search order is as follows: The directory specified by lpFileName. The current directory. The system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The SetDllDirectory function supports an alternate search order if the lpPathName parameter specifies a path. The alternate search order is as follows: The directory from which the application loaded. The directory specified by lpPathName. The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The directories that are listed in the PATH environment variable. If the lpPathName parameter is an empty string, the call removes the current directory from the search order. SetDllDirectory effectively disables safe DLL search mode while the specified directory is in the search path. To restore safe DLL search mode based on the SafeDllSearchMode registry value and restore the current directory to the search order, call SetDllDirectory with lpPathName as NULL. Legacy Search Order Versions of Windows earlier than the ones listed under Standard Search Order do not support the SafeDllSearchMode value. The DLL search order is as follows: The directory from which the application loaded. The current directory. The system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key.
fishion 2013-08-22
  • 打赏
  • 举报
回复
可以放到系统文件夹里,你可以了解下调用dll的规则,或者就是用非标准的dll,Atl方式,然后用调用组件的方式调用即可
赵4老师 2013-08-22
  • 打赏
  • 举报
回复
用depends.exe查看exe依赖的dll

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧