DLL初试

环境: VC++6.0

步骤:

1.建立一个WIN32 DYNAMIC-LINK LIBRARY工程,编写CPP文件,文件内容例如:

1 #include "stdafx.h"
2 #include "windows.h"
3 #include "dll.h" 
4 BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
5 {
6     return TRUE;
7 }
8 extern"C"_declspec(dllexport)int sum(int a, int b) { return a+b; }


2.记得编写头文件,在本次事例中,头文件为dll.h,内容:

 1 extern"C"_declspec(dllexport) int sum(int a,int b); 

3.进行编译连接,本过程可能回有点(相当)曲折.编译连接OK,生成*.dll *.lib 这两个文件便可用了.

(静态调用)测试:

创建一个WIN32 CONSOLE APPLICATION,编写CPP代码例如:

 1 // dllTest.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <stdio.h>
 6 extern"C"_declspec(dllexport) int sum(int a,int b);
 7 #pragma comment (lib,"dll")
 8 int main(int argc, char* argv[])
 9 {
10     printf("%d\r\n",sum(2,4));
11 
12     return 0;
13 }

编译连接即可.(记得将*.LIB *.DLL复制到测试的工程目录下)

(动态调用)测试:

 1 #include "stdafx.h"
 2 #include <Windows.h>
 3 #include <stdio.h>
 4 
 5 typedef int(*dllSum)(int,int);
 6 int main(int argc, char* argv[])
 7 {
 8     HMODULE hModule=LoadLibrary("dll.dll");
 9     dllSum sum=(dllSum)GetProcAddress(hModule,"sum");
10 
11     printf("%d\r\n",sum(2,22));
12     return 0;
13 }

 

posted @ 2017-03-16 17:24  马丁黄瓜啊  阅读(202)  评论(0编辑  收藏  举报