访问冲突从Delphi XE6调用C#dll函数

时间:2016-09-09 13:52:06

标签: c# delphi dll

将我们的项目从Delphi 2007迁移到XE6之后,我们再也无法在我们构建的C#dll中正确调用导出的函数。 C#代码似乎执行正常,并执行它应该执行的操作,但在完成后,它会抛出访问冲突。

代码:

Result := False;

  lib := LoadLibrary('KJPDFExport.dll');
  if lib = 0 then RaiseLastOSError;
  try
    try
      @prc := GetProcAddress(lib, 'ExportOffice');
      if Assigned(prc) then
      begin
        Result := prc(sourceFile,
            ChangeFileExt(destinationFile, ''),
            pdfBackgroundHeadFile,
            pdfBackgroundSubFile);
      end
      else
        ShowMessage('ExportOffice not found in KJPDFExport.dll');
    except
      on e:Exception do
      begin
        ShowMessage(e.Message);
      end;
    end;
  finally
    FreeLibrary(lib);
  end;

我传递给C#函数的字符串都是AnsiStrings。它们曾经是2007版项目中的“字符串”,因此我将其更改为AnsiString。

我很困惑,我一直在找几个小时而且找不到问题。

2 个答案:

答案 0 :(得分:0)

事实证明它一直都有用.. Delphi调试器不知道如何处理C#异常。即使您在C#中使用try / catch语句,Delphi调试器仍会将其视为访问冲突。只需按下继续即可。崩溃的原因是另一段代码必须转换为使用AnsiString。

答案 1 :(得分:-1)

您尝试使用本地ansiString变量吗? ChangeFileExt()返回String值。

这样的事情:

var sIn, sOut : ansiString;
  Result := False;

  sIn  := ansiString(sourceFile);
  sOut := ansistring(ChangeFileExt(destinationFile, ''));

  lib := LoadLibrary('KJPDFExport.dll');
  if lib = 0 then RaiseLastOSError;
  try
    try
      @prc := GetProcAddress(lib, 'ExportOffice');
      if Assigned(prc) then
      begin
        Result := prc(Sin,
                      sOut,
            pdfBackgroundHeadFile,
            pdfBackgroundSubFile);
      end
      else
        ShowMessage('ExportOffice not found in KJPDFExport.dll');
    except
      on e:Exception do
      begin
        ShowMessage(e.Message);
      end;
    end;
  finally
    FreeLibrary(lib);
  end;