0

I'm making a DLL (for GameMaker 8) with Lazarus. I have a function:

function Download(URL: PChar; FileName: PChar):double; cdecl;
var
  HTTPClient : THTTPSend;
begin
  HTTPClient := THTTPSend.Create;
  try
    if(Ping(URL, 4000) <> 4000) then
    begin
      if HTTPClient.HTTPMethod('GET', URL) then
      begin
        HTTPClient.Document.SaveToFile(FileName);
        if HTTPClient.ResultCode = 200 then Result := 1 else Result := 2;
      end
      else Result := 0;
    end
    else Result := 0;
  finally
    HTTPClient.Free;
  end;
end; 

And the Ping function is:

function Ping(ip:PChar; ttl:double):double; cdecl;
var PingSend: TPINGSend; PingBool: boolean;
begin
  PingSend := TPINGSend.Create;
  try
    PingSend.Timeout := round(ttl);
    PingBool := PingSend.Ping(ip);
    if PingBool = True then Result := PingSend.PingTime else Result := PingSend.Timeout;
  finally
    PingSend.Free;
  end;
end;   

In the URL I provide, for example, "http://www.google.co.in/images/nav_logo4.png" (no quotes). In the FileName I provide "D:\logo.png", a file that doesn't exist. No matter what I try, function always returns 0 (which means HTTPMethod didn't work) and doesn't save the file (though, it worked for some time before, but maybe I modified something crucial). The DLL's function is defined correctly in GameMaker, it freezes if providing an unreachable URL (Ping = 4000).

I'm kinda new to DLLs, Synapse and Lazarus. Is there something I'm doing wrong?

Thom A
  • 88,727
  • 11
  • 45
  • 75
GreatCorn
  • 81
  • 12
  • 4
    OT: *"it worked for some time before, but maybe I modified something crucial"*, well, that's a good lesson for you to start using version control system. – TLama Jan 01 '20 at 19:59
  • 2
    There is no good reason to ping a server just before downloading from it. That is just a waste of time and resources. Perform the download by itself, with timeout, and handle any error that may occur. – Remy Lebeau Jan 02 '20 at 03:49
  • @RemyLebeau I couldn't find any info on timeouting HTTPMethod and I pinged it to prevent the application from freezing. – GreatCorn Jan 02 '20 at 04:13
  • 2
    @GreatCorn pinging won't prevent freezing. All it can do is tell you whether the server is reachable, but then actually accessing the server can still freeze. `THTTPSend` has `SetTimeout()`, `SetSendTimeout()`, and `SetRecvTimeout()`. – Remy Lebeau Jan 02 '20 at 05:56

0 Answers0