DLL中导出函数的声明有两种方式
?本文引用自:VC編程時DLL中導(dǎo)出函數(shù)的聲明有兩種方式
一種方式是:在函數(shù)聲明中加上__declspec(dllexport);
另外一種方式是:采用模塊定義(.def)文件聲明,(.def)文件為鏈接器提供了有關(guān)被鏈接程序的導(dǎo)出、屬性及其他方面的信息。
方式一:在函數(shù)聲明中加上__declspec(dllexport)
/// 在動態(tài)鏈接庫程序中
/// 聲明動態(tài)鏈接庫(**.dll)的對外接口函數(shù)TestFuction
extern "C" __declspec(dllexport) int TestFuction(int nType,char *strPath,std::vector<string> &vecData)
{
?? do anything here
?? return 0;
}
/// 在外部希望調(diào)用動態(tài)鏈接庫的程序中
/// 加載動態(tài)鏈接庫(**.dll)并調(diào)用其對外接口TestFuction
void func()
{
? //typedef與函數(shù)TestFuction類型相同的函數(shù)指針為TESTDLL
? typedef int (_cdecl * TESTDLL)(int nType,char *strPath,std::vector<string> &vecData);
? HINSTANCE hmod;
? //加載動態(tài)鏈接庫**.dll
? hmod =::LoadLibrary(_TEXT("dll相對路徑\\**.dll"));
? if(NULL == hmod)
? {
???? TRACE("加載**.dll失敗");
? }
? //定義一個與函數(shù)TestFuction類型相同的函數(shù)指針lpproc
? TESTDLL lpproc;
? //搜索**.dll中函數(shù)名為TestFuction的對外接口
? lpproc = (TESTDLL)GetProcAddress (hmod,"TestFuction");
? //如果搜索成功
? if(NULL != lpproc)
? {
???? int nType = 0;
???? char* strPath = "Data";
???? std::vector<string> vecData;
???? //通過函數(shù)指針lpproc調(diào)用**.dll的接口函數(shù)TestFuction
???? int nResult = (*lpproc)(nType,strPath,vecData);
? }
? //...
? //在恰當?shù)臅r候釋放動態(tài)鏈接庫**.dll
? FreeLibrary(hmod);
}
方式二:采用模塊定義(.def)文件聲明
首先創(chuàng)建 一個DLL程序(DllTestDef)
在*.cpp中
int __stdcall Add(int numa, int numb)
{
???? return (numa + numb);
}
int __stdcall Sub(int numa, int numb)
{
???? return (numa - numb);
}
然后創(chuàng)建一個.def的文件,在里面加上
;DllTestDef.lib : 導(dǎo)出DLL函數(shù)
;作者:----
LIBRARY DllTestDef
EXPORTS
Add @ 1
Sub @ 2
最后創(chuàng)建一個測試程序:.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文件的規(guī)則為:
(1)LIBRARY語句說明.def文件相應(yīng)的DLL;
(2)EXPORTS語句后列出要導(dǎo)出函數(shù)的名稱。可以在.def文件中的導(dǎo)出函數(shù)名后加@n,表示要導(dǎo)出函數(shù)的序號為n(在進行函數(shù)調(diào)用時,這個序號將發(fā)揮其作用);
(3).def 文件中的注釋由每個注釋行開始處的分號 (;) 指定,且注釋不能與語句共享一行。
(4)使用__declspec(dllexport)和使用.def文件是有區(qū)別的。
如果你的DLL是提供給VC用戶使用的,你只需要把編譯DLL時產(chǎn)生的.lib提供給用戶,
它可以很輕松地調(diào)用你的DLL。但是如果你的DLL是供VB、PB、Delphi用戶使用的,那么會產(chǎn)生一個小麻煩。
因為VC++編譯器對于__declspec(dllexport)聲明的函數(shù)會進行名稱轉(zhuǎn)換,如下面的函數(shù):
__declspec(dllexport) int __stdcall Add()
會轉(zhuǎn)換為Add@0,這樣你在VB中必須這樣聲明:
Declare Function Add Lib "DLLTestDef.dll" Alias "Add@0" () As Long
@后面的數(shù)由于參數(shù)類型不同而可能不同。這顯然不太方便。所以如果要想避免這種轉(zhuǎn)換,就要使用.def文件方式導(dǎo)出函數(shù)了。
總結(jié)
以上是生活随笔為你收集整理的DLL中导出函数的声明有两种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DLL+ ActiveX控件+WEB页面
- 下一篇: VC获取其他进程ListCtrl内容