实验环境
- windows xp sp2
- vc6.0
创建dll
步骤
- 创建win32 dynamic-Link Liarary 工程
- 添加代码
wlj_dll.h
__declspec(dllexport) int wljadd(int i, int j);
wlj_dll.c
#include "wlj_dll.h"
int wljadd(int i, int j)
{
return i+j;
}
-
编译生成
在工程debug目录下会生成一个wlj_dll.dll文件
使用vc6创建dll步骤
动态方式调用dll
创建一个win32 Console Application工程
添加代码
main.c
#include <windows.h>
#include <stdio.h>
typedef int (*pFunc)(int x,int y);//函数原型指针
int main()
{
HMODULE hdll=LoadLibraryA("wlj_dll.dll"); //加载dll
if(hdll!=NULL)
{
pFunc proc=(pFunc)GetProcAddress(hdll,"wljadd");
if(proc!=NULL)
{
printf("%d+%d=%d\n",1,2,proc(1,2));
}
}
FreeLibrary(hdll);
return 0;
}
将上面生成的wlj_dll.dll拷贝到debug目录下,程序就可以运行了
![](https://img.haomeiwen.com/i20766503/4268384f6e949d2e.png)
静态方式调用dll
将wlj_dll.h头文件和wlj_dll.lib拷贝到当前工程目录下
将wlj_dll.dll文件拷贝工程debug目录下,使用下面方式调用
#include "wlj_dll.h"
#include<stdio.h>
#pragma comment(lib,"wlj_dll.lib")
int main()
{
printf("%d + %d = %d\n",1,2,wljadd(1,2));
return 0;
}
网友评论