TJSONObject ToString方法访问冲突

时间:2015-08-05 14:10:59

标签: json delphi dll tostring stdcall

我调用一个返回TJSONObject的dll函数。问题是当我尝试将此值添加到TStringList时,会引发访问冲突异常。这是我的代码:

function GetSentMessages(AIdSender: Integer; APageNumber: Integer; AResultsCount: Integer; 
  AQuery: TFDQuery): TJSONObject; stdcall; external 'Messages.dll';
function SendMessage(AMessageJson: TJSONObject; AQuery: TFDQuery): TJSONObject;
  stdcall; external 'Messages.dll';

function Test(AQuery: TFDQuery): TJSONObject;
var
  resp, msg: TJSONObject;
  loader: TStringList;
  tmp: string;
begin
  loader := TStringList.Create;

  try
    msg := TJSONObject.Create();
    msg.AddPair(TJSONPair.Create('idsender', '10'));
    msg.AddPair(TJSONPair.Create('idreceiver', '20'));
    msg.AddPair(TJSONPair.Create('content' , 'some other content'));

    resp := SendMessage(msg, AQuery);
    tmp := resp.ToString; // '{"status":"success"}'

    resp := TJSONObject.Create();
    resp.AddPair(TJSONPair.Create('idsender', '10'));
    resp.AddPair(TJSONPair.Create('idreceiver', '20'));
    resp.AddPair(TJSONPair.Create('content', 'some content'));
    loader.Add('Test: ' + resp.ToString); 
    // loader.Text = 'Test: {"idsender":"10","idreceiver":"20","content":"some content"}'#$D#$A

    resp := GetSentMessages(475, 0, 5, AQuery); // this function I call from a dll
    // resp.ToString = '{"messages":
    // [{"idmessage":"4","idsender":"475","idreceiver":"488","content":"\"Some message\"","datesent":"2015-07-29 19:12:38","readed":"1"},
    // {"idmessage":"5","idsender":"475","idreceiver":"488","content":"Some message","datesent":"2015-07-29 19:18:45","readed":"0"},
    // {"idmessage":"6","idsender":"475","idreceiver":"488","content":"Some message","datesent":"2015-07-30 11:23:45","readed":"1"}]}'
    loader.Text := resp.ToString; // here the exception is raised
    loader.SaveToFile('output.txt');
  finally
    loader.Free;
  end;
end;

这是显示的错误:Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'

0 个答案:

没有答案