LoadLibrary

HMODULE WINAPI LoadLibrary(
_In_  LPCTSTR lpFileName
);

Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.

用此函数来加载动态链接库到内存。
LoadLibrary按照这样的方式来搜寻文件,先在应用程序所在目录去找,然后再去系统路径下找。

所以 Window 用来定位DLL的搜寻路径 是这样的:

通过隐式和显式链接,Windows 首先搜索“已知 DLL”,如 Kernel32.dll 和 User32.dll。Windows 然后按下列顺序搜索 DLL:

  1. 当前进程的可执行模块所在的目录。

  2. 当前目录。

  3. Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。

  4. Windows 目录。GetWindowsDirectory 函数检索此目录的路径。

  5. PATH 环境变量中列出的目录。


Fully Qualified vs. Relative Paths


完全限定路径 VS 相对路径

For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path. A file name is relative to the current directory if it does not begin with one of the following:

很多文件操作相关的Windows API中,文件名经常是相对当前目录来说的,而有些API需要完全限定路径的文件名。如果不想默认为当前目录,那么可以尝试下面的一些写法

  • A UNC name of any format, which always start with two backslash characters ("\\"). For more information, see the next section.
  • A disk designator with a backslash, for example "C:\" or "d:\".
  • A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk. Examples of this format are as follows:

  • "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
  • "C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.

对于由磁盘符C:这种形式组成的文件路径,系统会认为这个文件位于该磁盘下的当前目录,如C:temp.txt,temp位于C盘的当前目录下

A path is also said to be relative if it contains "double-dots"; that is, two periods together in one component of the path. This special specifier is used to denote the directory above the current directory, otherwise known as the "parent directory". Examples of this format are as follows:

  • "..\tmp.txt" specifies a file named tmp.txt located in the parent of the current directory.
  • "..\..\tmp.txt" specifies a file that is two directories above the current directory.
  • "..\tempdir\tmp.txt" specifies a file named tmp.txt located in a directory named tempdir that is a peer directory to the current directory.

我们经常用到点加反向斜杠这种写法,.\表示当前目录,即当前工程所在的文件目录;..\表示当面目录的上一个目录,即父目录。

Relative paths can combine both example types, for example "C:..\tmp.txt". This is useful because, although the system keeps track of the current drive along with the current directory of that drive, it also keeps track of the current directories in each of the different drive letters (if your system has more than one), regardless of which drive designator is set as the current drive.

磁盘符加点跟反向斜杠可以组合,如C:..\tmp.txt",我们虽然限定了磁盘符,但是系统也会去其他磁盘找当面目录名下的tmp.txt文件。

最后,传授一个定义一个文件所在目录的技巧,这里用到的是define宏。

#define __LIBC_SUFFIX	_T("")
#define __DBG_SUFFIX _T("_rd") #define __DLL_PREFIX _T("")
#define __DLL_SUFFIX _T(".dll") // 声明DLL文件名常量
#define DECLARE_DLL_FILE(module) \
extern "C" const TCHAR* module; #define MAKE_LIB_NAME(module)\
_T(#module)_T("")__LIBC_SUFFIX _T("") // 定义DLL文件所在目录
#define DEFINE_DLL_FILE(module) \
extern "C" const TCHAR* module = _T("./")_T("")__DLL_PREFIX _T("")MAKE_LIB_NAME(module)_T("")__DLL_SUFFIX; DECLARE_DLL_FILE(dllthree);
DEFINE_DLL_FILE(dllthree);

上面的define展开后,文件名为  ./dllthree.dll  系统会去应用程序所在目录去找这个dll文件。 这样以后,LoadLibrary(dllthree)就可以直接加载文件名为dllthree.dll的动态链接库了。


尝试后,有下面几种方式

//HMODULE m_h = ::LoadLibrary(_T("..//bin//dllthree.dll"));

//HMODULE m_h = ::LoadLibrary(_T("..\\bin\\dllthree.dll"));

//HMODULE m_h = ::LoadLibrary(_T("../bin/dllthree.dll"));

//HMODULE m_h = ::LoadLibrary(_T("..\bin\dllthree.dll")); //wrong

//HMODULE m_h = ::LoadLibrary(_T(".\dllthree.dll")); //wrong

后面两种搜寻不到。暂时对这几种路径表示方式表示观望态度。

LoadLibrary文件路径及windows API相关的文件路径问题的更多相关文章

  1. 学习:Windows API核心DLL文件

    在 Windows 的系统目录中,存在着很多的动态链接库文件(DLL 文件).这些 DLL 文件中包括了 Windows API 函数可执行程序. DLL 将各函数"导出",这样应 ...

  2. C++ Windows API 读写INI文件

    BOOL WritePrivateProfileString( LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名 LPCTSTR lpKeyName, // ...

  3. Java项目生成可执行jar包、exe文件以及在Windows下的安装文件

    1.如何通过eclipse将Java项目生成可执行jar包 首先把在eclipse下的java项目导出jar file 下一步 下一步 下一步 最后点击完成,便生成了可执行的jar文件.可以在刚刚选择 ...

  4. 在VBA中使用Windows API

    VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...

  5. 【转载】 用 Windows API “GetAdaptersInfo” 获取 MAC 时遇到的问题

    From:http://blog.csdn.net/weiyumingwww/article/details/17554461 前段时间有个项目需要获取客户端的 MAC 地址,用作统计去重的参考数据. ...

  6. 源码浅析:MySQL一条insert操作,会写哪些文件?包括UNDO相关的文件吗?

    DML操作的大致流程 在解答上述疑惑之前,我们来梳理一下DML操作的大致流程: 1.语法解析.语义解析 2.生成执行计划 3.事务修改阶段 1) 激活事务,事务状态由not_active变为activ ...

  7. Windows下C++遍历文件夹中的文件

    Windows下,在VS中开发,C++遍历文件夹下文件. 在Windows下,遍历文件所用到的函数和结构体,需要在程序中包含头文件#include <io.h>,在VS中,头文件io.h实 ...

  8. windows API实现用户选择文件路径的对话框

    在编写应用程序时,有时需要用户选择某个文件,以供应用程序使用,比如在某些管理程序中需要打开某一个进程,这个时候需要弹出一个对话框来将文件路径以树形图的形式表示出来,以图形化的方式供用户选择文件路径,而 ...

  9. Windows API 进程相关笔记

    0. 前言 最近做了一个进程信息相关的项目,整理了一下自己做项目时的笔记,分享给大家 1. 相关概念 1.1 HANDLE 概念 HANDLE(句柄)是Windows操作系统中的一个概念. 在Wind ...

随机推荐

  1. 《Android虚拟机》----Android系统的结构

    No1: 操作系统层包括各种驱动程序:显示.Flash内存.照相机.音频.WiFi.键盘.蓝牙.Binder IPC.能源管理. 各种库和Android运行环境大多是用C和C++实现的. Androi ...

  2. 【BZOJ 2671】 2671: Calc (数论,莫比乌斯反演)

    2671: Calc Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 303  Solved: 157 Description 给出N,统计满足下面条件 ...

  3. 51nod1380 夹克老爷的逢三抽一

    问题等价于选出$n / 3$个不相邻元素是权值和最大 这是一个经典贪心问题,同种树,拿堆维护即可,复杂度$O(n \log n)$ #include <queue> #include &l ...

  4. 莫队p2 【bzoj3809】Gty的二逼妹子序列

    发现一篇已经够长了...所以就放在这里吧... http://hzwer.com/5749.html ↑依然是看大牛题解过的   袜子那道题太简单了.... 然后被这道题超时卡了一段时间....... ...

  5. hibernate核心及常用技术

    一.hibernate介绍 1.hibernate概述 hibernate是轻量级Java EE持久层解决方案,管理java类到数据库表的映射(ORM:对象关系型数据映射),并提供数据查询获取的方法. ...

  6. bzoj 3585: mex && 3339: Rmq Problem -- 主席树

    3585: mex Time Limit: 20 Sec  Memory Limit: 128 MB Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区 ...

  7. mui中a标签的跳转问题

    一.脑补 快速响应是mobile App实现的重中之重,研究表明,当延迟超过100毫秒,用户就能感受到界面的卡顿,然而手机浏览器的click点击存在300毫秒延迟(至于为何会延迟,及300毫秒的来龙去 ...

  8. VS2010安装HTML5插件

    步骤: 1. 下载 插件 2.拷贝文件里面德尔html_5.xsd到 D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packa ...

  9. PWM DAC Low Pass Filtering

    [TI博客大赛][原创]LM3S811之基于PWM的DAC http://bbs.ednchina.com/BLOG_ARTICLE_3005301.HTM http://www.fpga4fun.c ...

  10. Delphi XE 4,Rad Studio XE 4 官方下载,更新Update 1(附破解)

    http://blog.csdn.net/maxwoods/article/details/8842889 XE4 Update1 下载: http://altd.embarcadero.com/do ...