首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Delphi调用DLL时出现的有关问题

2012-02-05 
Delphi调用DLL时出现的问题.VC++写的DLL中代码为:externC __declspec(dllexport)voidSetParameter(doubl

Delphi调用DLL时出现的问题.
VC++写的DLL中代码为:
extern   "C "   __declspec(   dllexport   )   void   SetParameter(   double   dVel,   double   dOffVel,   double   dLaserOn,   double   dPoly,  
double   dLaserOff,   double   dPos,   double   dDis,   long   lFreq,   double   dPower   )
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLsrEllipseApp   *pApp   =   (   CLsrEllipseApp*   )AfxGetApp();
pApp-> SetParameter(   dVel,   dOffVel,   dLaserOn,   dPoly,   dLaserOff,   dPos,   dDis,   lFreq,   dPower   );
}
extern   "C "   __declspec(   dllexport   )   void   MarkEllipse(   double   dX,   double   dY,   double   dWid,   double   dHei,   double   dRotAng   )  
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLsrEllipseApp   *pApp   =   (   CLsrEllipseApp*   )AfxGetApp();
pApp-> MarkEllipse(   dX,   dY,   dWid,   dHei,   dRotAng   );
}
我在Delphi中这样调用的:
procedure   TMainInterface.Button1Click(Sender:   TObject);
var
    Init:function:   Longint   ;stdcall;
    SetParameter:procedure   (dVel:double;dOffVel:double;dLaserOn:double;dPoly:double;
dLaserOff:double;dPos:double;dDis:double;lFreq:longint;dPower:double);stdcall;
    MarkEllipse:procedure   (dX:double;dY:double;dWid:double;dHei:double;dRot:double);stdcall;
    DllModule:THandle;

begin

      DllModule:=LoadLibrary( 'LsrEllipse.dll ');     //盢 "LsrEllipse.dll "ゅン琈禜琈甮秈秸ノ秈祘丁
      if   DllModule <> 0   then

      @Init:=GetProcAddress(DllModule, 'Init ');       //眔DLLいㄧ计Init()
      @SetParameter:=GetProcAddress(DllModule, 'SetParameter ');     //眔DLLいㄧ计SetParameter()
      @MarkEllipse:=GetProcAddress(DllModule, 'MarkEllipse ');         //眔DLLいㄧ计MarkEllipse()

    if   (@Init <> nil)then
    if   (@SetParameter <> nil)then
    if   (@MarkEllipse <> nil)then
begin
    Init();                       //秸ノㄧ计Init
    SetParameter(100,2000,0.0002,0.2,0.6,1,0.2,1000,0.6);       //秸ノㄧ计SetParameter
    MarkEllipse(10,10,2,5,45);           //秸ノㄧ计MarkEllipse
    FreeLibrary(DllModule);
    end;
  end;

运行时出现了错误:
Project   Project1.exe   raised   exception   class   EAccessViolation   with   message 'Access   violation   at   address   00000000.Read   of   address   00000000 '



[解决办法]
VC的函数声明中不加__stdcall,采用VC默认格式__cdecl,需要在Delphi中要注明调用格式为cdecl。

var
Init:function: Longint; cdecl;
SetParameter:procedure (dVel:double;dOffVel:double;dLaserOn:double;dPoly:double;
dLaserOff:double;dPos:double;dDis:double;lFreq:longint;dPower:double); cdecl;
MarkEllipse:procedure (dX:double;dY:double;dWid:double;dHei:double;dRot:double); cdecl;

热点排行