__declspec(dllexport)與.def文件
在VC++中,如果生成DLL可以不使用.def文件。只需要在VC++的函數定義前要加__declspec(dllexport)修飾就可以了。但是使用__declspec(dllexport)和使用.def文件是有區別的。如果DLL是提供給VC++用戶使用的,只需要把編譯DLL時產生的.lib提供給用戶,它可以很輕松地調用你的DLL。但是如果DLL是供VB、PB、Delphi用戶使用的,那么會產生一個小麻煩。因為VC++對于__declspec(dllexport)聲明的函數會進行名稱轉換,如下面的函數:
__declspec(dllexport) int __stdcall IsWinNT()
會轉換為IsWinNT@0,這樣你在VB中必須這樣聲明:
Declare Function IsWinNT Lib "my.dll" Alias "IsWinNT@0" () As Long
@的后面的數由于參數類型不同而可能不同。這顯然不太方便。所以如果要想避免這種轉換,就要使用.def文件方式。
EXPORTS后面的數可以不給,系統會自動分配一個數。對于VB、PB、Delphi用戶,通常使用按名稱進行調用的方式,這個數關系不大,但是對于使用.lib鏈接的VC程序來說,不是按名稱進行調用,而是按照這個數進行調用的,所以最好給出。如:
EXPORTS
test @1
vc的dll,delphi調用的方法:
////K9RtExpr.h
extern "C" __DECDLL
unsigned int _stdcall K9RtSysInterrupt(int nInterruptTag);
extern "C" __DECDLL
unsigned int _stdcall K9RtSysExprm(unsigned int nModuleTag,
float fTimeout,
void * pTestPar,
void * pResultPar
);
// K9RtExpr.cpp
#include "K9RtExpr.h"
#include "RtCtrl.h"
extern "C" __DECDLL
unsigned int _stdcall K9RtSysInterrupt(int nInterruptTag)
{
return CRelayTestControl::Instance()->InterruptTest(nInterruptTag);
}
extern "C" __DECDLL
unsigned int _stdcall K9RtSysExprm(unsigned int nModuleTag,
float fTimeout,
void * pTestPar,
void * pResultPar
)
{
return CRelayTestControl::Instance()->RelayTest(
nModuleTag, fTimeout, pTestPar, pResultPar);
}
///K9RtExpr.def
LIBRARY "K9RtExpr"
DESCRIPTION K9RtExpr Windows Dynamic Link Library'
EXPORTS
K9RtSysInterrupt
K9RtSysExprm
; Explicit exports can go here
//delphi 調用文件
unit InterfaceFunc;
interface
uses
UnitData,Types;
function K9RtSysExprm(nModuleTag:integer ;
uTimeout:integer;
pTestPar:Pointer;
pResultPar:Pointer) : Integer; stdcall;
function K9RtSysInterrupt(nInterruptType:Integer) : Integer; stdcall;
implementation
const DLLPATH = 'RtBsExpr.dll';
function K9RtSysExprm; external DLLPATH name 'ExperimentRelayTest';
function K9RtSysInterrupt; external DLLPATH name 'InterruptRelayTest';
end.