簡體   English   中英

如何解決從 Delphi 調用 C# dll 方法時的錯誤外部異常 E0434352

[英]How to resolve Error external exception E0434352 on calling a C# dll method from Delphi

我需要解壓縮大型 gzip 文件(超過 4.5 Go)。 我在使用 TDecompressionStream 使用 Delphi Seattle 執行此操作時遇到了一些麻煩(結果文件被截斷)。

為了避免這個問題,我選擇在 C# dll 中執行此任務並從 Delphi 調用它。

我的 C# 代碼正在運行,我使用控制台應用程序對其進行了測試。 我添加了 nugget 包 UnmanagedExports 並以 32 位編譯了 dll。

當我從 Delphi 調用我的 dll 方法時,出現此錯誤:“外部異常 E0434352”

我遵循此鏈接的建議: How to use a DLL created with C# in Delphi

但我已經有這個問題了

我的 C# 代碼

    static public class UnZip
    {
        [DllExport("UngzipFile", CallingConvention.StdCall)]
        public static int UngzipFile(string aFile)
        {
            int result = 0;
            FileInfo fileInfo = new FileInfo(aFile);
            using (FileStream fileToDecompress = fileInfo.OpenRead())
            {
                string decompressedFileName = Path.Combine(Path.GetDirectoryName(aFile), "temp.sql");
                using (FileStream decompressedStream = File.Create(decompressedFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
                    {
                        try
                        {
                            decompressionStream.CopyTo(decompressedStream);
                        }
                        catch
                        {
                            result = 1;                            
                        }
                    }
                }
            }
            return result;
        }
    }

我的德爾福代碼

function UngzipFile(aFile : string) : Integer; stdcall; external 'UnCompress.dll';

procedure TForm1.UnzipFile(aFileName: String);
var
  UnZipFileName : string;
  Return : integer;
  DllZipFile : PWideChar;
begin
  UnZipFileName := ExtractFilePath(aFileName)+'Temp.sql';

  if FileExists(UnZipFileName) then
    DeleteFile(UnZipFileName);

  DllZipFile := PWideChar(aFileName);
  Return := UngzipFile(DllZipFile);
  if Return > 0 then
    raise Exception.Create('Error while uncompressing file');
end;

目前,當我從 Delphi 調用 UngzipFile 時,_我有外部異常 E0434352。

我希望 result = 0 並且我的文件被解壓縮。

謝謝你的幫助。

感謝魯迪,我找到了解決方案。

由於字符串參數,我的 DLL 中出現異常。 我在 dll 中添加日志,我發現只有我的參數的第一個字符被 dll 占用。

這篇文章在 Delphi 中使用 C# DLL 僅使用第一個函數參數幫助我糾正我的代碼。

新的 C# 代碼

    static public class UnZip
    {
        [DllExport("UngzipFile", CallingConvention.StdCall)]
        public static int UngzipFile([MarshalAs(UnmanagedType.LPWStr)] string aFile)
        {
            if (!File.Exists(aFile))
                return 3;

            FileInfo fileInfo;

            string logFile = @"D:\Temp\logDll.log";            
            try
            {
                File.AppendAllText(logFile, aFile);
                fileInfo = new FileInfo(aFile);
            }
            catch(Exception ex)
            {
                File.AppendAllText(logFile, String.Format("File : {0} || Exception : {1}",aFile,ex.Message));
                return 2;
            }

            int result = 0;
            using (FileStream fileToDecompress = fileInfo.OpenRead())
            {
                string decompressedFileName = Path.Combine(Path.GetDirectoryName(aFile), "temp.sql");
                using (FileStream decompressedStream = File.Create(decompressedFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
                    {
                        try
                        {
                            decompressionStream.CopyTo(decompressedStream);
                        }
                        catch
                        {
                            result = 1;                            
                        }
                    }
                }
            }
            return result;
        }
    }

通過在我的參數聲明中添加“[MarshalAs(UnmanagedType.LPWStr)]”,可以解決問題。

感謝魯迪的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM