I have a pascal library (.dll) which i'm calling in a C# .NET application.
Everytime the library tries to call GetJSON
it throws an exception.
Here is the pascal code:
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;
I've also tried different types for jsonInput (WideString, string, ..) but as far as i know PChar is the one which fits .NET string the best. Please correct me if i'm wrong. The output in the textfile looks like this:
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
The calling C# code looks like this:
[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);
}
}
Furthermore i've tried to change the DllImport Parameters like CharSet
, CallingConvention
etc. or i also tried [MarshalAs(UnmanagedType.BStr)]
for string jsonInput but that did not help either.
As you can see in the textfile output, the passed string data is there. When i compile the pascal code into a program and copy the json data into it, it runs fine, the json gets parsed. When i compile it into a library, it doesn't work anymore. The pascal code is compiled on a 64bit system, changing the .NET Project to compile to 64bit results in the same error. Also i want to mention that im calling the pascal library from ASP.NET Core which in my opinion shouldn't be related to the error in any way.
Two things that I notice: