You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中调用Delphi 11 64位DLL?调用时程序无响应崩溃

C#调用Delphi 11 64位DLL崩溃问题

我尝试用C#调用Delphi 11 64位编写的DLL,调用后程序直接无响应并关闭,以下是测试代码:

示例1

Delphi 实现代码

function DBConnect1(inputStr,connStr:PWideChar):PWideChar;stdcall;
begin
  try
     result:=PWideChar('Hello from Delphi!');
  except
     result:=PWideChar('Exception');
  end;
end;

C# 调用代码

[DllImport("Project1.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string DBConnect1(string inputString, string connectionString);

string inputString = "Parker";
string connectionString = "MyComputer";
string dbStrObj1 = DBConnect1(inputString, connectionString);
MessageBox.Show(dbStrObj1);

示例2

Delphi 实现代码

function Test1(sFD,sVD,sINI,sCh,sSD: string): PWideChar;stdcall;
var
  tempStr:string;
  str:WideString;
begin
  tempStr:=sFD+sVD+sINI+sCh+sSD;
  try
   result:= PWideChar(tempStr);
  except
    str:='Error';
    result:=PWideChar(str);
  end;
    result:=PWideChar(str);
end;

C# 调用代码

[DllImport("Project1.dll", EntryPoint = "LoginLic", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string LoginLic(string s1, string s2, string s3, string s4, string s5);

string strId = "PCMS";
string strVersion = "AD19";
string strIni = @"";
string strCheck = "0";
string strSubDate = null;
string aa;
aa = UseDll.LoginLic(strId, strVersion, strIni, strCheck, strSubDate);

内容的提问来源于stack exchange,提问作者Parker Shiung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 22:15:36