首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传递给函数时,Delphi PChar字符串为空

传递给函数时,Delphi PChar字符串为空
EN

Stack Overflow用户
提问于 2015-06-17 20:35:55
回答 1查看 580关注 0票数 3

我正在使用Delpi XE6,如果这很重要的话。

我正在编写一个DLL,它将从Google的Directions API获得响应,解析生成的JSON,并确定行程的总距离。

GetJSONString_OrDie可以按预期工作。它获取一个URL,并尝试从Google获取响应。如果是,它将返回Google返回的JSON字符串。

ExtractDistancesFromTrip_OrDie应该接受一个JSON字符串,并解析它以获得每段旅程的总距离,并将这些距离保存在一个双精度数组中。

奇怪的是,如果我将这些函数从DLL中取出,并将它们放入一个项目中,然后像这样调用它们,它就会按预期工作。只有当它们被卡在DLL中时,事情似乎才会出错。如前所述,GetJSONString_OrDie按预期工作并返回一个JSON字符串--但是当单步执行DLL的ExtractDistancesForTrip时,传递的PChar字符串为''。为什么会这样呢?

代码语言:javascript
复制
unit Unit1;



interface
uses
  IdHTTP,
  IdSSLOpenSSL,
  System.SysUtils,
  System.JSON;

type
  arrayOfDouble = array of double;

function GetJSONString_OrDie(url : Pchar) : Pchar;
function ExtractDistancesForTrip_OrDie(JSONstring: Pchar) : arrayOfDouble;
function SumTripDistances(tripDistancesArray: arrayOfDouble) : double;

implementation



{ Attempts to get JSON back from Google's Directions API }
function GetJSONString_OrDie(url : Pchar) : PChar;
var
  lHTTP: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  {Sets up SSL}
  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  {Creates an HTTP request}
  lHTTP := TIdHTTP.Create(nil);
  {Sets the HTTP request to use SSL}
  lHTTP.IOHandler := SSL;
  try
    {Attempts to get JSON back from Google's Directions API}
    Result := PWideChar(lHTTP.Get(url));
  finally
    {Frees up the HTTP object}
    lHTTP.Free;
    {Frees up the SSL object}
    SSL.Free;
  end;
end;

{ Extracts the distances from the JSON string }
function ExtractDistancesForTrip_OrDie(JSONstring: Pchar) : arrayOfDouble;
var
  jsonObject: TJSONObject;
  jsonArray: TJSONArray;
  numberOfLegs: integer;
  I: integer;
begin
  //raise Exception.Create('ExtractDistancesForTrip_OrDie:array of double has not yet been '+
  //                        'implemented.');

  jsonObject:= nil;
  jsonArray:= nil;
  try
    { Extract the number of legs in the trip }
    jsonObject:= TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSONstring), 0) as TJSONObject;
    jsonObject:= (jsonObject.Pairs[0].JsonValue as TJSONArray).Items[0] as TJSONObject;
    jsonArray:= jsonObject.Pairs[2].JSONValue as TJSONArray;
    numberOfLegs:= jsonArray.Count;

    {Resize the Resuls arrayOfDouble}
    SetLength(Result, numberOfLegs);

    {Loop through the json and set the result of the distance of the leg}
    {Distance is in km}
    for I := 0 to numberOfLegs-1 do
    begin
      Result[I]:= StrToFloat((((jsonArray.Items[I] as TJSONObject).Pairs[0].JsonValue as TJSONObject).Pairs[1]).JsonValue.Value);
    end;


  finally
    jsonObject.Free;
    jsonArray.Free;
  end;

end;


function SumTripDistances(tripDistancesArray: arrayOfDouble) : double;
var
  I: integer;
begin
  //raise Exception.Create('GetDistanceBetweenPoints_OrDie:double has not yet been ' +
 //                        'implemented.');

 Result:= 0;
 {Loop through the tripDistancesArray, and add up all the values.}
 for I := Low(tripDistancesArray) to High(tripDistancesArray) do
    Result := Result + tripDistancesArray[I];


end;

end.

下面是这些函数的调用方式:

代码语言:javascript
复制
program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  ShareMem,
  IdHTTP,
  IdSSLOpenSSL,
  System.SysUtils,
  System.JSON,
  System.Classes;

type
  arrayOfDouble = array of double;

  testRecord = record
    testString : string;
    testInt : integer;
  end;

function GetJSONString_OrDie(url : Pchar) : PWideChar; stdcall; external 'NMBSGoogleMaps.dll';

function ExtractDistancesForTrip_OrDie(JSONstring: pchar) : arrayOfDouble;  stdcall; external 'NMBSGoogleMaps.dll';

function SumTripDistances(tripDistancesArray: arrayOfDouble) : double; stdcall; external 'NMBSGoogleMaps.dll';


var
  jsonReturnString: string;
  jsonReturnString2: Pchar;
  doubles: arrayOfDouble;
  totalJourney: double;
  uri:Pchar;

  a : testRecord;
  begin
  try
    uri:= 'https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&key=GETYOUROWNKEY';


    { TODO -oUser -cConsole Main : Insert code here }
    jsonReturnString:= GetJSONString_OrDie(uri); //On step-through, uri is fine.

    jsonReturnString2:= stralloc(length(jsonreturnstring)+1);
    strpcopy(jsonreturnstring2, jsonreturnstring);
    jsonreturnstring2 := 'RANDOMJUNK';

    doubles:= ExtractDistancesForTrip_OrDie(jsonReturnString2); //On step-through, jsonReturnString2 is seen as '', rather than 'RANDOMJUNK'
    totalJourney:= SumTripDistances(doubles);
    WriteLn('The total journey was: ');
    WriteLn(totalJourney);

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  readln;
end.

当第一个函数被调用时,GetJSONString_OrDie,uri被传入,并且在查看或打印出来时是正确的。但是,在创建一个随机字符串并将其传递给ExtractDistancesForTrip_OrDie之后,该函数只能看到''。如果我将ExtractDistancesForTrip_OrDie更改为接受一个整数、一条记录或任何东西-它会看到垃圾数据或随机数据。无论我是通过引用传递还是通过值传递,都没有关系。

EN

回答 1

Stack Overflow用户

发布于 2015-06-17 20:38:51

您的调用约定不匹配。您的函数使用register调用约定,但是您可以像导入stdcall一样导入它们。这就是你传递的参数没有到达的原因。

在实现函数和导入函数时都要使用stdcall

GetJSONString_OrDie中,您将返回一个指向局部变量缓冲区的指针。一旦函数返回,该局部变量就会被销毁,指针就会失效。您需要找到一种不同的方法将字符串数据传递出此函数。

通常情况下,返回一个字符串就可以了。但由于此函数是从DLL导出的,因此无法工作。你需要一个调用者分配的缓冲区,或者一个在共享堆上分配的缓冲区。

最后,动态数组不是有效的互操作类型。您不应该跨模块边界传递这些内容。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30891803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档