LoadLibrary文件路径及windows API相关的文件路径问题
LoadLibrary
HMODULE WINAPI LoadLibrary(
_In_ LPCTSTR lpFileName
);
所以 Window 用来定位DLL的搜寻路径 是这样的:
通过隐式和显式链接,Windows 首先搜索“已知 DLL”,如 Kernel32.dll 和 User32.dll。Windows 然后按下列顺序搜索 DLL:
当前进程的可执行模块所在的目录。
当前目录。
Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。
Windows 目录。GetWindowsDirectory 函数检索此目录的路径。
PATH 环境变量中列出的目录。
Fully Qualified vs. Relative Paths
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相关的文件路径问题的更多相关文章
- 学习:Windows API核心DLL文件
在 Windows 的系统目录中,存在着很多的动态链接库文件(DLL 文件).这些 DLL 文件中包括了 Windows API 函数可执行程序. DLL 将各函数"导出",这样应 ...
- C++ Windows API 读写INI文件
BOOL WritePrivateProfileString( LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名 LPCTSTR lpKeyName, // ...
- Java项目生成可执行jar包、exe文件以及在Windows下的安装文件
1.如何通过eclipse将Java项目生成可执行jar包 首先把在eclipse下的java项目导出jar file 下一步 下一步 下一步 最后点击完成,便生成了可执行的jar文件.可以在刚刚选择 ...
- 在VBA中使用Windows API
VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...
- 【转载】 用 Windows API “GetAdaptersInfo” 获取 MAC 时遇到的问题
From:http://blog.csdn.net/weiyumingwww/article/details/17554461 前段时间有个项目需要获取客户端的 MAC 地址,用作统计去重的参考数据. ...
- 源码浅析:MySQL一条insert操作,会写哪些文件?包括UNDO相关的文件吗?
DML操作的大致流程 在解答上述疑惑之前,我们来梳理一下DML操作的大致流程: 1.语法解析.语义解析 2.生成执行计划 3.事务修改阶段 1) 激活事务,事务状态由not_active变为activ ...
- Windows下C++遍历文件夹中的文件
Windows下,在VS中开发,C++遍历文件夹下文件. 在Windows下,遍历文件所用到的函数和结构体,需要在程序中包含头文件#include <io.h>,在VS中,头文件io.h实 ...
- windows API实现用户选择文件路径的对话框
在编写应用程序时,有时需要用户选择某个文件,以供应用程序使用,比如在某些管理程序中需要打开某一个进程,这个时候需要弹出一个对话框来将文件路径以树形图的形式表示出来,以图形化的方式供用户选择文件路径,而 ...
- Windows API 进程相关笔记
0. 前言 最近做了一个进程信息相关的项目,整理了一下自己做项目时的笔记,分享给大家 1. 相关概念 1.1 HANDLE 概念 HANDLE(句柄)是Windows操作系统中的一个概念. 在Wind ...
随机推荐
- Python数据类型-集合(set)
1.创建集合 集合的创建不同于前两种数据结构. 集合通过set(iterable)方法创建,参数iterable为可迭代对象. 示例代码: s1 = set('好好学习天天想上') # 将字符串分解为 ...
- 图形管线之旅 Part2
原文:<A trip through the Graphics Pipeline 2011> 翻译:往昔之剑 转载请注明出处 还没那么快 在上一篇,讲述了渲染命令在被GPU处理 ...
- 【BZOJ 3136】 3136: [Baltic2013]brunhilda (数论?)
3136: [Baltic2013]brunhilda Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 238 Solved: 73[Submit][ ...
- BZOJ2754 SCOI2012喵星球上的点名
绝世好题. 正当我犹豫不决时,hzwer说:“MAP!!!” 没错这题大大的暴力,生猛的stl,贼基尔爽,,ԾㅂԾ,, 由于我们求点名在名字中的子串个数,所以将点名建AC自动机,记录节点属于哪次点名, ...
- Matrix-tree定理 spoj HIGH
Matrix-tree定理,给出一个无向图,问求出的生成树方案有多少种方案,利用Matrix-tree定理,主对角线第i行是i的度数,(i,j) 值为i和j之间边的数量,然后删去第一行第一列,利用初等 ...
- 理解HashSet及使用
(1) 为啥要用HahSet? 假如我们现在想要在一大堆数据中查找X数据.LinkedList的数据结构就不说了,查找效率低的可怕.ArrayList哪,如果我们不知道X的位置序号,还是一样要全 ...
- 【8.23校内测试】【贪心】【线段树优化DP】
$m$的数据范围看起来非常有问题??仔细多列几个例子可以发现,在$m<=5$的时候,只要找到有两行状态按位$&$起来等于$0$,就是可行方案,如果没有就不行. #include<i ...
- 2349 Arctic Network(中文版)
试题描述: 国防部希望通过无线网络连接几个北方前哨基地. 在建立网络时将使用两种不同的通信技术:每个前哨基站都将拥有无线电收发器,另外还有一些前哨卫星通道. 任何带卫星频道的两个前哨都可以通过卫星进行 ...
- 密码加密_md5
md5加密 package com.fh.util; import java.security.MessageDigest; public class MD5 { public static Stri ...
- [原]Android Studio导入外部项目找不到对应的sdk解决办法
示例项目:JPushExample(349872) 打开项目的文件夹目录,找到:JPushExample(349872)\app\build.gradle打开,将里面的 compileSdkVersi ...