Recent

Author Topic: Download a file without external libraries  (Read 2434 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Download a file without external libraries
« on: May 28, 2018, 09:42:16 am »
I was checking how to download a file from internet and even when it is easy with Synapse, i want to know if it is possible without add a new library, only with the ones installed in lazarus v1.8.2 by default.

My app need check if windwos is 32/64 bit, and donwload the necessary SSL files automatically (i plan to use this link for that: http://wiki.lazarus.freepascal.org/Detect_Windows_x32-x64_example )

Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Download a file without external libraries
« Reply #1 on: May 28, 2018, 10:45:57 am »
Code: Pascal  [Select][+][-]
  1. // untested, but should work
  2. function IsWin64:Boolean;
  3. begin
  4.    Result := false;
  5.    {$if defined(win64)}Result := true;{$ifend}
  6. end;
Specialize a type, not a var.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Download a file without external libraries
« Reply #2 on: May 28, 2018, 11:12:07 am »
Code: Pascal  [Select][+][-]
  1. // untested, but should work
  2. function IsWin64:Boolean;
  3. begin
  4.    Result := false;
  5.    {$if defined(win64)}Result := true;{$ifend}
  6. end;

Works perfect and very simple!!
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Download a file without external libraries
« Reply #3 on: May 28, 2018, 11:24:59 am »
Such function checks if the binary is 64-bit, not if windows is 64-bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Download a file without external libraries
« Reply #4 on: May 28, 2018, 11:31:16 am »
Such function checks if the binary is 64-bit, not if windows is 64-bit.
I interpreted the question as for which platform are we compiling? In his case that can be a 32 bit windows or a 64 bit windows.
My solution provides for that. (IOW a 64 bit compiler for a 64 bit Windows needs 64 bit libraries. A 32 bit compiler will download the 32 bit libraries even on a 64 bit windows)
My understanding is that is what he means. To be more precise it detects the target binaries, which he needs to download and install the target libraries.

But you know that. :P
« Last Edit: May 28, 2018, 11:34:56 am by Thaddy »
Specialize a type, not a var.

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: Download a file without external libraries
« Reply #5 on: May 28, 2018, 11:36:40 am »
Yes, my understanding is also that you need to know what bitness your program is. That's needed to download the correct OpenSSL files. You can't have 64bit OpenSSL with 32 bit program, even if your Windows is 64 bit.

So you might want to use this:

Code: Pascal  [Select][+][-]
  1. uses fphttpclient, zipper;
  2.  
  3. type
  4.   TMyUnZipper = class(TUnZipper)
  5.     FMyStream: TStream;
  6.   private
  7.     procedure MyStreamOpen(Sender: TObject; var AStream: TStream);
  8.   public
  9.     constructor Create(AStream: TStream);
  10.   end;
  11.  
  12. constructor TMyUnZipper.Create(AStream: TStream);
  13. begin
  14.   inherited Create;
  15.   FMyStream := AStream;
  16.   OnOpenInputStream := @MyStreamOpen;
  17. end;
  18.  
  19. procedure TMyUnZipper.MyStreamOpen(Sender: TObject; var AStream: TStream);
  20. begin
  21.   AStream := FMyStream;
  22. end;
  23.  
  24. procedure DownloadOpenSSL;
  25. var
  26.   Url: string;
  27.   UnZipper: TUnZipper;
  28.   Stream: TStream;
  29.   List: TStringList;
  30. begin
  31.   if not FileExists('ssleay32.dll') then
  32.   begin
  33.   {$IFDEF WIN32}
  34.     Url := 'http://indy.fulgan.com/SSL/openssl-1.0.2h-i386-win32.zip';
  35.   {$ELSE}
  36.     Url := 'http://indy.fulgan.com/SSL/openssl-1.0.2h-x64_86-win64.zip';
  37.   {$ENDIF}
  38.     Stream := TMemoryStream.Create;
  39.     TFPCustomHTTPClient.SimpleGet(Url, Stream); // get the .zip
  40.     UnZipper := TMyUnZipper.Create(Stream); // NOTE. UnZipper will release the Stream !!
  41.     try
  42.       List := TStringList.Create;
  43.       List.Add('ssleay32.dll');
  44.       List.Add('libeay32.dll');
  45.       UnZipper.UnZipFiles(List);
  46.     finally
  47.       UnZipper.Free;
  48.       List.Free;
  49.     end;
  50.   end;
  51. end;

 

TinyPortal © 2005-2018