如果有xxx.h
,xxx.lib,xxx.dll,这样就可以隐式链接加载了。
在所要引用的.cpp文件中加入
#include“xxx.h”
//包含静态链接库的头文件
#pragma
comment(lib,"xxx")
//隐式调用库文件
这样就可以使用dll中封装的函数了。
其中语句“#pragma
comment(lib,"xxx")”可以用工程设置对话框的设置来代替唤衡。
2.动态加载动和陆做态链接库
如果只有xxx.dll,只能动态加载,主要通过loadlibrary和getprocaddress两个函数来实悉枯现。
举个小例子:int
irestypedef
int
(_cdecl
*myfunc)(int,int)
hmodule
hmodule
=
::loadlibrary("mydynamiclib.dll")
//加载动态链接库
此提供函数getmaxcommdiv(int,int)计算最大公约数
myfinc
getvalue
=
(myfunc)getprocaddress(hmodule,"getmaxcommdiv")
首没碧先,这是一个Win32的API,必须使用Win32的编译器,用VC++还行,TC就算运州了。然后,包含windows.h头文枯悄举件,函数原型如下:
DWORD GetModuleFileName(
HMODULE hModule,// handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer to receive module path
DWORD nSize // size of buffer, in characters
)
Parameters
hModule
Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
lpFilename
Pointer to a buffer that is filled in with the path and filename of the given module.
nSize
Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
1 LIB文件直接加入到工程文件列表中搜搜轿 在VC中打开File View一页,选中工程名,单击漏枣鼠标右键,然后选中"Add Files to Project"菜单,在d出的文件对话框中选中要加入DLL的LIB文件。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
2 设置工程的 Project Settings来加载DLL的LIB文件
打开工程的 Project Settings菜单,选中Link,然后在Object/library modules下的文本框中输入DLL的LIB文件,如you.lib(或者lib文件的路径,包括文件名)。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
3 通过程序代码的方式
加入预编译指令#pragma comment (lib,"*.lib"),这种方法优点是可以利用条件预编译指令链接不世肆同版本的LIB文件。因为,在Debug方式下,产生的LIB文件是Debug版本,如Regd.lib;在Release方式下,产生的LIB文件是Release版本,如Regr.lib。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
当应用程序对DLL的LIB文件加载后,还需要把DLL对应的头文件(*.h)包含到其中,在这个头文件中给出了DLL中定义的函数原型,然后声明
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)