- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
动态链接库的建立与调用
姓名:蒙吉 学号:20072411603
实验名称:动态链接库的建立与调用
实验目的:1) 理解动态链接库的实现原理;
掌握WINDOWS 系统动态链接库的建立方法;
掌握WINDOWS 环境下动态链接库的调用方法。实验准备知识: 1) 动态链接库基础知识;
动态链接库入口函数(DllMain);
动态链接库导入/导出函数:
声明导出函数的代码:_declspec(dllexport) MyDllFunction(int x,int y);
声明导入函数的代码:_ declspec(dllexport) MyDllAdd(int x,int y);
:隐式连接和显式连接;
函数调用参数传递约定:(1)_stdcall 调用约定;(2)C 调用约定;(3)
_fastcall 调用约定。
实验内容:1) 在WINDOWS 环境下建立一个动态链接库;
2) 使用隐式调用法调用动态链接库;
3) 使用显式调用法调用动态链接库;
实验要求:掌握动态链接库建立和调用方法。在WINDOWS XP+VC++6.0 环境下建立一个动态链接库,并分别使用隐式和显式将其调用,从而体会使用动态链接库的优点。
参考源代码:
// SimpleDll.cpp:Defines the entry point for the DLL application. #include stdafx.h
externC_declspec(dllexport) int Add(int x,int y); externC_declspec(dllexport) int Sub(int x,int y); BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call, LPVOID lpReserved
)
{
return TRUE;
}
int Add(int x,int y)
{
int z; z=x+y; return z;
}
int Sub(int x,int y)
{
int z; z=x-y; return z;
}
//隐式调用动态链接库的程序
//CallDll.cpp:Defines the entry point for the consile application. #includestdafx.h
externC_declspec(dllimport) int Add(int x,int y); externC_declspec(dllimport) int Sub(int x,int y); int main(int argc,char* argv[])
{
int x=7; int x=6; int add=0; int sub=0;
printf(Call Dll Now!\n);
//调用动态链接库add=Add(x,y);
sub=Sub(x,y);
printf( 7+6=%d,7-6=%n,add,sub); return 0;
}
//显示调用动态链接库的程序
//CallDllAddress.cpp:Defines the entry point for the console application. #includestdafx.h
#includeCallDllAddress.h
#ifdaf_DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS FLLE[]= FILE ; #endif
///////////////////////////////////////////////////////////////
//The one and only application object CWinApp theApp;
using namespace std;
int_tmain(int argc,TCHAR* argv[], TCHAR* envp[])
{
int s;
int nRetCode=0;
typedef int (*pAdd) (int x,int y); typedef int (*pSub) (int x,int y);
HMODULE hDll;
pAdd add; pSub sub;
hDll=LoadLibrary(SimpleDll.dll); //加载动态链接库文件 SimpleDll.dll if(hDll==NULL)
{
printf(LoadLobrary Error. \n);
return nRetCode;
}
else printf(LoadLibrary Success. \n);
add=(pAdd)Ge
文档评论(0)