看到另外两篇关于dll写法的文章,虽然程式化的东西,但是还是要留个底,顺便转过来:)
主要是
#define DLL_DECL extern "C" __declspec( dllexport )
另外,写dll注意c文件,cpp文件的不同点
头文件的写法。def文件的编写,用.def文件定义要输出的资源
------------------------------
VC中DLL的创建与使用
1 DLL类型:有三种
Win32DLL: 不使用MFC库,可被任何程序使用。
MFC Regular DLL 使用MFC库,与MFC库静态或动态连接,可被任何程序使用。
MFC Extension DLL ,与MFC动态联合编译,只能被MFC程序使用。
三种DLL的异同:
2 DLL的创建与调用
2.1 Win32DLL的创建
两种方法:A通过.h文件定义要输出的资源 B通过.def文件定义要输出的资源
2.1.1用.h文件定义要输出的资源:
例子:
win32dll_h.h
extern " C " __declspec(dllexport) int MaxNum( int a, int b);
win32dll_h.cpp
DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
... {
switch(ul_reason_for_call)
...{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int MaxNum( int a, int b)
... {
if(a>b) return a;
else return b;
}
2.1.2用.def文件定义要输出的资源
w32dll_def.def
LIBRARY W32DLL_DEF
DESCRIPTION " W32DLL_DEF Windows Dynamic Link Library "
; Function name must equal the name in .cpp file and sensitive to 大小写
EXPORTS
MinNum
W32dll_def.cpp
int MaxNum( int a, int b)
... {
if(a<b) return a;
else return b;
}
2.1.3 隐式调用W32DLL中的函数
相同的部分:将.lib文件和.dll文件copy到要调用的程序的目录下或系统目录(/windows等)下不同的部分:是调用前的声明。
.h创建的dll:
extern "C" __declspec(dllimport) int MaxNum(int a, int b);
.def创建的dll:
__declspec(dllimport) int MaxNum(int a, int b);
不要前面的extern "C"
2.2 MFC Regular DLL的创建
------------------------------
dll的创建及使用(源码)
声明:
本文出自:http://blog.csdn.net/closeall2008
可以不经作者同意任意转载,但请保留文章作者及的出处,谢谢
dll的创建:
下面用代码实例简单的介绍一下如何创建dll以及如何使用dll。
1、首先创建一个dll的头文件。dll的源码文件(.cpp)需要包含这个头文件,而且使用这个动态连接库的可执行文件也需要这个头文件。这个头文件包含你想要从dll输出的函数的原型、结构和符号。下面是一个名为dll.h文件的的例子。
// dll.h
// author: clsoeall
// time : 2005.09
/**/ ////
#ifndef DLL_H_
#define DLL_H_
#define DLL_DECL extern "C" __declspec( dllexport )
DLL_DECL void ShowText() ;
#endif
2、然后创建dll.cpp.代码如下。
// dll.cpp
// author: closeall
// time : 2005.09
/**/ ///
#include < stdio.h >
#include " dll.h "
void ShowText()
... {
printf( "This is dll export show " ) ;
}
编译完成以后,就会生成一个dll.dll的文件和一个dll.lib 的文件。在使用dll.dll这个动态连接库的时候,应该把dll.h头文件包含在可执行文件中,而且还要把dll.lib连入程序。
下面的代码是使用这个动态连接库的测试程序。
// testdll.cpp
// author: closeall
// time : 2005.09
/**/ ///
#include < stdio.h >
#include < iostream.h >
#include " dll.h "
#pragma comment( lib, "dll.lib" )
int main()
... {
ShowText() ; //这个函数是从动态连接库dll.dll中调用的
printf( "This is execute show " ) ;
int i ;
cin>> i ;
return 1 ;
}