DLL 中 DEF 文件的使用
2012-03-25 11:18
323 查看
原文出自:www.fishc.com
DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明;另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被链接程序的导出、属性及其他方面的信息。
首先创建 一个DLL程序,.cpp中
int __stdcall Add(int numa, int numb)
{
return (numa + numb);
}
int __stdcall Sub(int numa, int numb)
{
return (numa - numb);
}
然后创建一个.def的文件,在里面加上
LIBRARY DllTestDef
EXPORTS
Add @ 1
Sub @ 2
最后创建一个测试程序:.cpp文件如下:
#include <iostream>
#include <windows.h>
using namespace std;
typedef int (__stdcall *FUN)(int, int);
HINSTANCE hInstance;
FUN fun;
int main()
{
hInstance = LoadLibrary("DLLTestDef.dll");
if(!hInstance)
cout << "Not Find this Dll" << endl;
fun = (FUN)GetProcAddress(hInstance, MAKEINTRESOURCE(1));
if (!fun)
{
cout << "not find this fun" << endl;
}
cout << fun(1, 2) << endl;
FreeLibrary(hInstance);
return 0;
}
说明:
.def文件的规则为:
(1)LIBRARY语句说明.def文件相应的DLL;
(2)EXPORTS语句后列出要导出函数的名称。可以在.def文件中的导出函数名后加@n,表示要导出函数的序号为n(在进行函数调用时,这个序号将发挥其作用);
(3).def 文件中的注释由每个注释行开始处的分号 (;) 指定,且注释不能与语句共享一行。
DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明;另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被链接程序的导出、属性及其他方面的信息。
首先创建 一个DLL程序,.cpp中
int __stdcall Add(int numa, int numb)
{
return (numa + numb);
}
int __stdcall Sub(int numa, int numb)
{
return (numa - numb);
}
然后创建一个.def的文件,在里面加上
LIBRARY DllTestDef
EXPORTS
Add @ 1
Sub @ 2
最后创建一个测试程序:.cpp文件如下:
#include <iostream>
#include <windows.h>
using namespace std;
typedef int (__stdcall *FUN)(int, int);
HINSTANCE hInstance;
FUN fun;
int main()
{
hInstance = LoadLibrary("DLLTestDef.dll");
if(!hInstance)
cout << "Not Find this Dll" << endl;
fun = (FUN)GetProcAddress(hInstance, MAKEINTRESOURCE(1));
if (!fun)
{
cout << "not find this fun" << endl;
}
cout << fun(1, 2) << endl;
FreeLibrary(hInstance);
return 0;
}
说明:
.def文件的规则为:
(1)LIBRARY语句说明.def文件相应的DLL;
(2)EXPORTS语句后列出要导出函数的名称。可以在.def文件中的导出函数名后加@n,表示要导出函数的序号为n(在进行函数调用时,这个序号将发挥其作用);
(3).def 文件中的注释由每个注释行开始处的分号 (;) 指定,且注释不能与语句共享一行。
相关文章推荐
- 基于VS2008使用def文件创建DLL
- 动态库dll使用module.def文件导出函数(像静态库一样使用)
- DLL 中 DEF 文件的使用
- Eclipse for C/C++ (三)编写DLL文件之def文件的使用
- VS2005使用DEF文件来生成DLL与Lib
- DLL 中 .DEF文件的使用
- VS2005使用DEF文件来生成DLL与Lib(转载)
- 使用Def文件导出Dll文件
- DLL 中 .def文件的使用
- dll使用def文件导出类
- VS2005使用DEF文件来生成DLL与Lib
- vs2005中Def文件的使用方法(DLL编写无法找到函数名称)
- VS20XX使用DEF文件来生成DLL与Lib
- DLL中.def文件的使用
- DLL中.def文件的使用
- VS2005使用DEF文件来生成DLL与Lib
- 使用def文件简化dll导出
- [DLL]使用def文件从dll导出
- DLL中.def文件的使用
- 使用DEF文件来生成DLL与Lib