调用一个dll,是通过回调函数返回信息的。对方给的是用delphi调用,没有问题。现在在VC下调用,运行时出现错误:
the value of esp was not properly saved across a function call.this is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention
编译通过,运行时不进入回调函数,而且在调用函数那句不出错,在函数}返回时才报错。
从网上查这个错误是因为类型不匹配,不过没发现有不匹配的。delphi中的声明
type TSearchCallBackEx = function (buf:PSearchResponse; bufsize:integer;UserData:Pointer):boolean;stdcall;function PQNVR_GetFileList(LoginID:Integer;aDate: TDateTime; FileType: byte;
                             CHID: Integer; SearchCallBackEx: TSearchCallBackEx;
                             UserData: Pointer):boolean;stdcall;external 'PQNVR_Play.dll';
delphi中的定义
 function SearchCB(buf:PSearchResponse; bufsize:integer;UserData:Pointer):boolean;stdcall;
var
  Res:TSearchResponse;
begin
  if buf<>nil then
  begin
    Move(buf^,Res,sizeof(Res));
    Form1.ListBox11.Items.Add(Res.afile);
  end
  else
    application.MessageBox('find file end','');
end;procedure TForm1.Button41Click(Sender: TObject);
var
  yy,mm,dd:word;
begin
  Listbox11.Items.Clear ;
  //yy:=strtoint(copy(ComboBox26.Text,1,4));
  //mm:=strtoint(copy(ComboBox26.Text ,6,2));
  //dd:=strtoint(copy(ComboBox26.Text ,9,2));
   yy:=2012;mm:=7;dd:=2;
  PQNVR_GetFileList(LoginId, EncodeDate(yy,mm,dd),255,strtoint(Edit34.Text ),SearchCB,self);end;VC中的声明
typedef bool (WINAPI *TSearchCallBackEx)(TSearchResponse* buf,int bufsize,LPVOID UserData);
typedef int (WINAPI *PKNVRGetFileList)(int LoginID,CTime aDate,byte FileType,int CHID,TSearchCallBackEx SearchCallBackEx,LPVOID UserData);VC中的定义
bool WINAPI SearchCB(TSearchResponse* buf,int bufsize,LPVOID UserData)
{
  /*CString filename,filetime;
  if (buf!=NULL)
  {
    filename=buf->afile;
    byte c=buf->head;
int index=filename.Find("192.168.1.160/0",0);
filetime=filename.Mid(index+17,6);
CTime t_user(2000,1,1,8,58,00);
CTime t_file(2000,1,1,atoi(filetime.Mid(0,2)),atoi(filetime.Mid(2,2)),atoi(filetime.Mid(4,2))); 
if(((t_user-t_file)>0)&&((t_user-t_file)<3600))
{
    return true;
}
    //Form1.ListBox11.Items.Add(Res.afile);
  }*/
  return true;
}
void CMFCPKDlg::OnButton2() 
{
// TODO: Add your control notification handler code here
char pBuf[MAX_PATH];                                  
        GetSystemDirectory(pBuf,MAX_PATH);                   
strcat(pBuf,"\\PQNVR_Play.dll");
    DLLInst=LoadLibrary(pBuf); //获取系统文件夹下的dll
PKNVRGetFileList NVRGetFileList=NULL;
NVRGetFileList=(PKNVRGetFileList)GetProcAddress(DLLInst,"PQNVR_GetFileList");
CTime t_user(2012,3,2);
    LPVOID p;
NVRGetFileList(NLoginID, t_user,0,0,SearchCB,this);
//ss(userid,t_user,0,0,SearchCB,p);
   //test1(test,3);
}亟待解答,谢谢

解决方案 »

  1.   

    参数尽量用pchar 对应VC中的char*
    其他的类型是否兼容没有研究过。
      

  2.   

    TSearchResponse
    声明或定义在哪?
      

  3.   

    TSearchResponse是个结构体,忘贴了
    delphi中
    type
      TSearchResponse = record
        head : Byte;
        afile : Array[0..69] of char;
    end;
    PSearchResponse=^TSearchResponse;VC中typedef struct _TSearchResponse
    {
        byte head;
        CString afile;
    }TSearchResponse;
      

  4.   

    PChar这么转换应该是对的。因为还调用了别的函数是这么转换的,结果正确
    怀疑是CTime的错误,但不知道怎么改,也不确认