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. Js数组的常用的方法概述

    学习JS的同学们,也曾对数组进行学习掌握,所以我也把数组中常用的方法列举下来,相互学习 不多废话,直接上正文 .                 快乐的分割线... 一.对象继承的方法 数组是一种特殊 ...

  2. CentOS7 Nodejs布署环境

    一.服务器设置:https://www.youtube.com/watch?v=1OU5ngq-WyM&list=PLQlWzK5tU-gDyxC1JTpyC2avvJlt3hrIh& ...

  3. thinkphp5.0配置加载

    ThinkPHP支持多种格式的配置格式,但最终都是解析为PHP数组的方式. PHP数组定义 返回PHP数组的方式是默认的配置定义格式,例如: //项目配置文件 return [ // 默认模块名 'd ...

  4. 2017-2018-1 20179202《Linux内核原理与分析》第五周作业

    一.系统调用实验: 1.知识准备: (1)系统调用的三层皮:xyz()(API).system_call(所有系统调用的入口) . sys_xyz()(中断服务程序) (2)API是程序员在用户空间下 ...

  5. EOJ 3263 丽娃河的狼人传说

    差分约束系统,$spfa$. 首先判断无解,若某个约束的$t$大于区间长度,则一定无解. 否则一定有解,可以得到一系列的不等式: 最终区间和大于等于目前的区间和:$S[R]-S[L-1]≥val$, ...

  6. eclipse的一些部署

    1. Eclipse导入一个项目Package Exporer-------右键------import------General------Existing Project into Workspa ...

  7. 【洛谷比赛】[LnOI2019]长脖子鹿省选模拟赛 T1 题解

    今天是[LnOI2019]长脖子鹿省选模拟赛的时间,小编表示考的不怎么样,改了半天也只会改第一题,那也先呈上题解吧. T1:P5248 [LnOI2019SP]快速多项式变换(FPT) 一看这题就很手 ...

  8. 我的OI生涯 第二章

    第二章 很快就开学了,我们几个到了16班,开哥茂嵩到了15班,MJX和ZZJ去了四班,YST去了九班. 竞赛课改成了一周两节,每周都学习一点新的语言,这期间来了一些新同学例如于北,LTY,他们刚来感觉 ...

  9. [BZOJ 4719] 天天爱跑步

    Link: BZOJ 4719 传送门 Solution: 感觉求LCA又有了新姿势啊:$Tarjan$离线$O(n+m)$ 每次递归返回时将子树和父节点合并,如果询问节点已访问过则LCA就是已合并的 ...

  10. [CodeForces-178F]Representative Sampling

    题目大意: 给你n个字符串,要求从中选出k个字符串,使得字符串两两lcp之和最大. 思路: 动态规划. 首先将所有的字符串排序,求出相邻两个字符串的lcp长度(很显然,对于某一个字符串,和它lcp最长 ...