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

在Delphi中调用VC动态库,回调函数怎么定义

2012-05-13 
在Delphi中调用VC动态库,回调函数如何定义一个VC的dll,需要在delphi调用,其中有个回调函数的参数,不知道该

在Delphi中调用VC动态库,回调函数如何定义
一个VC的dll,需要在delphi调用,其中有个回调函数的参数,不知道该怎么写,请大家帮忙看看
先看VC的声明

1、回调函数
  typedef long (__stdcall *VOX_CVSA_CALLBACK_OnReportSwitchVideoResult)(int nMonNo,int nCamNo,long nResult,
const char *errInfo, long lParam); 
  typedef long (__stdcall *VOX_CVSA_CALLBACK_OnReportVideoMapping)(int nMonNo,int nCamNo, long lParam);

2、回调函数结构体
  struct CVSA_NOTIFY{
VOX_CVSA_CALLBACK_OnReportSwitchVideoResult OnReportSwitchVideoResult;
long lOnReportSwitchVideoResultParam;

VOX_CVSA_CALLBACK_OnReportVideoMapping OnReportVideoMapping;
long lOnReportVideoMappingParam;
  };

3、动态库函数
  CLIENT_VIDEO_SERV_API long __stdcall VOX_CVSA_SetNotify(long hInstance, struct CVSA_NOTIFY *pNotify);


下面是根据上面的部分改的Delphi代码
1、回调函数
  VOX_CVSA_CALLBACK_OnReportSwitchVideoResult =
  Function (nMonNo: Integer; nCamNo: Integer; nResult: Integer; errInfo: PChar; lParam: Integer): Integer;

  VOX_CVSA_CALLBACK_OnReportVideoMapping =
  Function (nMonNo : Integer; nCamNo: Integer; lParam: Integer): Integer;  

2、回调函数结构体
  PCVSA_NOTIFY = ^TCVSA_NOTIFY; 
  TCVSA_NOTIFY = Record
  OnReportSwitchVideoResult: VOX_CVSA_CALLBACK_OnReportSwitchVideoResult;
  lOnReportSwitchVideoResultParam: Integer;

  OnReportVideoMapping: VOX_CVSA_CALLBACK_OnReportVideoMapping;
  lOnReportVideoMappingParam: Integer;
  end;

3、动态库函数
 Function VOX_CVSA_SetNotify(hInstance: Integer; pNotify: PCVSA_NOTIFY): Integer; stdcall; external 'VoxCVSA_SDK.dll' ;

-----------------------------------------------
我的实现如下:

1、先声明一个测试类
  TVoxImpl = class
  private
  FCVSA_NOTIFY : PCVSA_NOTIFY ;
  Function VideoResult(nMonNo: Integer; nCamNo: Integer; nResult: Integer; errInfo: PChar; lParam: Integer): Integer;
  Function VideoMapping(nMonNo : Integer; nCamNo: Integer; lParam: Integer): Integer;
  public
  Constructor Create;
  end;

2、在Create中注册回调函数
  Procedrue TVoxImpl.Create()
  begin
  New(FCVSA_NOTIFY);
   
  FCVSA_NOTIFY^.lOnReportSwitchVideoResultParam := 0;
  FCVSA_NOTIFY^.lOnReportVideoMappingParam := 0;
   
  ****** 编译到下面这就提示错误了,[Incompatible types: regular procedure and method pointer]*******************
  FCVSA_NOTIFY^.OnReportSwitchVideoResult := VideoResult;  
  FCVSA_NOTIFY^.OnReportVideoMapping := VideoMapping;

  end;
   




请帮忙看看,这个回调函数到底该怎么弄啊!






[解决办法]
给你一段我自己在用的代码:
dll接口文件:
type
TAlarmCallBack = procedure(ResourceID: PChar; AlarmType: Integer; AlarmDetail: PChar; AlarmLevel: Integer; AlarmTime: PChar; Action: Integer; User: Pointer); cdecl;

function Plat_SubscribeAlarm(AlarmCallBack: TAlarmCallBack; User: Pointer; UserHandle: Integer): Integer; cdecl; external 'Platform.dll'; //这里用到回调函数

调用的地方:
interface
procedure AlarmCallBack(ResourceID: PChar; AlarmType: Integer; AlarmDetail: PChar;
AlarmLevel: Integer; AlarmTime: PChar; Action: Integer; User: Pointer);

implementation

procedure AlarmCallBack(ResourceID: PChar; AlarmType: Integer; AlarmDetail: PChar;
AlarmLevel: Integer; AlarmTime: PChar; Action: Integer; User: Pointer);
begin
HKOperator.RaiseAlarm(1, StrToInt(StrPas(ResourceID)), AlarmType, StrPas(AlarmDetail),
AlarmLevel, StrPas(AlarmTime), Action);
end;


function THKOperator.SubscribeAlarm: Boolean;
begin
Result := (Plat_SubscribeAlarm(@AlarmCallBack, nil, FUserHandle) = 0);
if Result then
WriteLogInfo(1, NOTICE, '告警订阅成功!')
else
WriteLogInfo(1, ERROR, '告警订阅失败:' + GetLastErrorInfo);
end;

热点排行