VC DLL接口函数:
   BOOL WriteFile(WORD nFileNo, BYTE* pFile, DWORD nFileLen, char* pCode);转换为Delphi接口
function WriteFile(nFileNo:Word;pFile:PByte;nFileLen:Word;pCode:PChar):integer;stdcall;请教下转换申明是否正确,下面的调用方法执行时报错,请大家指点
var
  pFile,pCode :PByte;
  s :String;
  iLen :Integer;
begin
  s :='test file';
  iLen := Length(s);
  pCode :='code';
  GetMem(pFile,iLen);
  move(s,pFile^,iLen);
  WriteFile(1,pFile,iLen,pCode)
  FreeMem(pFile);
end;

解决方案 »

  1.   

    C++ 不改的话, 默认调用约定用的 cdecl ,而不是 stdcall
      

  2.   

    Delphi接口修改为 function WriteFile(nFileNo:Word;pFile:PByte;nFileLen:Word;pCode:PChar):integer;吗?
    下面的调用是否正确,还是会报错。
      

  3.   

    C++ 的函数原型是你给出的那个?
    如果没给错的话, 应该是 DELPHI后加 cdecl 
      

  4.   

    你下边代码也有问题
    应该是
    move(s[1],  pFile^, iLen);
      

  5.   

    对方提供的C++原型函数
    BOOL WriteFile(WORD nFileNo, BYTE* pFile, DWORD nFileLen, BYTE* pCode);
    我用Delphi调用对方DLL实现,现在用stdcall或cdecl都报错。

    TWriteFileFunc = function(nFileNo:Word;pFile:PByte;nLen:Word;SecurityCode:PByte):integer;cdecl;//stdcall;function GetFunc(FuncName: string): Pointer;
    var
      dllhandle: Cardinal;
      dllname: string;
    begin
      dllhandle := GetModuleHandle(UKEYDLL);
      if dllhandle = 0 then
      begin
        dllname := ExtractFilePath(ParamStr(0)) + UKEYDLL;
        if not FileExists(dllname) then
          raise Exception.Create('没找到DLL<' + UKEYDLL + '>');
        dllhandle := LoadLibrary(PChar(dllname));
      end;
      if dllhandle = 0 then
        raise Exception.Create('加载DLL<' + UKEYDLL + '>失败');
      Result := GetProcAddress(dllhandle, PChar(FuncName));
      if Result = nil then
        raise Exception.Create('加载DLL函数<' + FuncName + '>失败');
    end;function WriteFile(nFileNo:Word;pFile:PByte;nLen:Word;SecurityCode:PByte):integer;
    var
      DllFunc: WriteFileFunc;
    begin
      try
        @DllFunc := GetFunc('WriteFile');
        if @DllFunc = nil then
          ShowMessage('Error');
        Result := DllFunc(nFileNo,pFile,nLen,SecurityCode);
      except
        on e: Exception do
        begin
          Result := 0;
          Application.MessageBox(PChar('WriteFile Fail'+E.Message),'提示');
        end;
      end;
    end;//调用
    var
      pFile,pCode :PByte;
      s,pass:String;
      nFileNo:word;
      iLen :Integer;
    begin
      FileNo := 1001;
      
      pass := 'password';
      iLen := Length(pass);
      GetMem(pCode,iLen);
      Move(pass[1],pCode^,iLen);
      
      s :='test file content';
      iLen := Length(s);
      GetMem(pFile,iLen);
      move(s[1],pFile^,iLen);
      
      if WriteFile(nFileNo,pFile,iLen,pCode) =1 then
        ShowMessage('File Writed');
      FreeMem(pFile);
      FreeMem(pCode);
    end;
      

  6.   

    VC DLL接口函数:
      BOOL WriteFile(WORD nFileNo, BYTE* pFile, DWORD nFileLen, char* pCode);转换为Delphi接口
    function WriteFile(nFileNo:Word;pFile:PByte;nFileLen:Word;pCode:PChar):integer;stdcall;
    --------------------------
    你第3个参数都写错了