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. Docker入门到实战

    1.系统要求 Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存 ...

  2. Nearest Neighbor Search

    ## Nearest Neighbor Search ## Input file: standard input Output file: standard output Time limit: 1 ...

  3. Python入门基础知识(1) :locals() 和globals()

    Python有两个内置的函数,locals() 和globals(),它们提供了基于字典的访问局部和全局变量的方式. 首先,是关于名字空间的一个名词解释.是枯燥,但是很重要,所以要耐心些.Python ...

  4. 【BZOJ 2669】 2669: [cqoi2012]局部极小值 (状压DP+容斥原理)

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 667  Solved: 350 Description 有一 ...

  5. PHP 笔记——String 字符串

    1. 定义 单引号括起来的字符串被原样输出. 双引号字符串中的变量被PHP解析为变量值. 2. 获取字符串长度 strlen(string $string): int 在utf-8下,汉字占3个字符, ...

  6. 51nod1423 最大二"货" 单调栈

    枚举每个点作为次大值,用单调栈处理出左边 / 右边第一个比它大的数,直接回答 复杂度$O(n)$ #include <cstdio> #include <cstring> #i ...

  7. [BZOJ4651][NOI2016]网格(Tarjan)

    下面直接给出结论,相关证明见官方题解. 1.若跳蚤数不超过1或仅有两只跳蚤且相邻,则答案为-1. 2.若跳蚤形成的连通块个数大于1,则答案为0. 3.若跳蚤之间建图存在割点,则答案为1. 4.否则为2 ...

  8. 为什么java的构造方法中this()或者super()要放在第一行

    java的构造方法中如果自己显性的调用super()的时候一定要放在第一行,如不是的话就会报错. 为什么一定要在第一行? super()在第一行的原因就是: 子类有可能访问了父类对象, 比如在构造函数 ...

  9. Codeforces Round #304 (Div. 2) B. Soldier and Badges 水题

    B. Soldier and Badges Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  10. 配置nginx虚拟目录配置文件支持tp的pathinfo

    lnmp自带的包不好用, 经测试,在相应的conf文件加入这句话即可: location / { if (!-e $request_filename) { rewrite ^(.*)$ /index. ...