GetJSON 在 pascal 库中抛出异常
GetJSON throws exception in pascal library
我有一个要在 C# .NET 应用程序中调用的 Pascal 库 (.dll)。
每次库尝试调用 GetJSON
时都会抛出异常。
这是帕斯卡代码:
procedure CalcCarreau(const jsonInput: PChar); cdecl;
var
jData: TJSONData;
{ for debugging purposes }
fileOut: TextFile;
begin
AssignFile(fileOut, 'debug.txt');
rewrite(fileOut);
writeln(fileOut, 'JSON Input:');
writeln(fileOut, jsonInput);
writeln(fileOut, 'Now trying to parse JSON input');
CloseFile(fileOut);
// here it crashes
jData := GetJSON(jsonInput);
jData.Free;
CloseFile(fileOut);
end;
我也为 jsonInput (WideString, string, ..) 尝试了不同的类型,但据我所知,PChar 是最适合 .NET 字符串的类型。如果我错了,请纠正我。文本文件中的输出如下所示:
JSON Input:
{"countallsvt":8,"svtnumberslist":[{"shearrates": [12.9103,33.4452,77.8971,167.578,362.618,865.61,1466.25,2199.38],"viscosities":[5694.83,2965.14,1851.09,1163.16,683.705,399.006,294.249,250.844],"temperature":205.0}]}
Now trying to parse JSON input
调用 C# 代码如下所示:
[HandleProcessCorruptedStateExceptions]
[DllImport("foo.dll", EntryPoint = "CalcCarreau", CallingConvention = CallingConvention.Cdecl)]
public static extern void CalcCarreau(string jsonInput);
public void Foo()
{
try
{
CalcCarreau("{\"countallsvt\":8,\"svtnumberslist\":[{\"shearrates\":[12.9103,33.4452,77.8971,167.578,362.618,865.61,1466.25,2199.38],\"viscosities\":[5694.83,2965.14,1851.09,1163.16,683.705,399.006,294.249,250.844],\"temperature\":205.0}]}");
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}
此外,我尝试更改 DllImport 参数,如 CharSet
、CallingConvention
等,或者我也尝试 [MarshalAs(UnmanagedType.BStr)]
用于字符串 jsonInput,但没有帮忙。
正如您在文本文件输出中看到的那样,传递的字符串数据就在那里。当我将 pascal 代码编译成程序并将 json 数据复制到其中时,它运行良好,json 得到解析。
当我将它编译成一个库时,它不再工作了。
pascal 代码是在 64 位系统上编译的,将 .NET 项目更改为编译为 64 位会导致相同的错误。我还想提一下,我是从 ASP.NET Core 调用 pascal 库的,我认为这与错误没有任何关系。
我注意到两件事:
- 赋值;您可以写入当前目录吗?尝试固定路径。
- 不要 const pchar 参数,它可能会添加一个额外的间接寻址
我有一个要在 C# .NET 应用程序中调用的 Pascal 库 (.dll)。
每次库尝试调用 GetJSON
时都会抛出异常。
这是帕斯卡代码:
procedure CalcCarreau(const jsonInput: PChar); cdecl;
var
jData: TJSONData;
{ for debugging purposes }
fileOut: TextFile;
begin
AssignFile(fileOut, 'debug.txt');
rewrite(fileOut);
writeln(fileOut, 'JSON Input:');
writeln(fileOut, jsonInput);
writeln(fileOut, 'Now trying to parse JSON input');
CloseFile(fileOut);
// here it crashes
jData := GetJSON(jsonInput);
jData.Free;
CloseFile(fileOut);
end;
我也为 jsonInput (WideString, string, ..) 尝试了不同的类型,但据我所知,PChar 是最适合 .NET 字符串的类型。如果我错了,请纠正我。文本文件中的输出如下所示:
JSON Input:
{"countallsvt":8,"svtnumberslist":[{"shearrates": [12.9103,33.4452,77.8971,167.578,362.618,865.61,1466.25,2199.38],"viscosities":[5694.83,2965.14,1851.09,1163.16,683.705,399.006,294.249,250.844],"temperature":205.0}]}
Now trying to parse JSON input
调用 C# 代码如下所示:
[HandleProcessCorruptedStateExceptions]
[DllImport("foo.dll", EntryPoint = "CalcCarreau", CallingConvention = CallingConvention.Cdecl)]
public static extern void CalcCarreau(string jsonInput);
public void Foo()
{
try
{
CalcCarreau("{\"countallsvt\":8,\"svtnumberslist\":[{\"shearrates\":[12.9103,33.4452,77.8971,167.578,362.618,865.61,1466.25,2199.38],\"viscosities\":[5694.83,2965.14,1851.09,1163.16,683.705,399.006,294.249,250.844],\"temperature\":205.0}]}");
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}
此外,我尝试更改 DllImport 参数,如 CharSet
、CallingConvention
等,或者我也尝试 [MarshalAs(UnmanagedType.BStr)]
用于字符串 jsonInput,但没有帮忙。
正如您在文本文件输出中看到的那样,传递的字符串数据就在那里。当我将 pascal 代码编译成程序并将 json 数据复制到其中时,它运行良好,json 得到解析。 当我将它编译成一个库时,它不再工作了。 pascal 代码是在 64 位系统上编译的,将 .NET 项目更改为编译为 64 位会导致相同的错误。我还想提一下,我是从 ASP.NET Core 调用 pascal 库的,我认为这与错误没有任何关系。
我注意到两件事:
- 赋值;您可以写入当前目录吗?尝试固定路径。
- 不要 const pchar 参数,它可能会添加一个额外的间接寻址