Convert Int64 to Int32 (Integer) ?

Posted on 2011-09-25
Last Modified: 2016-09-29
Is it possible to convert int64 to in32 in delphi ?
Question by:rotem156
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 36707476
calculate the percentage of the completion ... a lot easier for the progressbar
you only get numbers from 0 to 100
LVL 25

Accepted Solution

epasquier earned 250 total points
ID: 36707917
first, use 1Gb parts, it will be a lot easier.

procedure Split1GoParts(TotalSize:Int64;Var NbFullParts,LastIncompletePartSize:Integer):
 NbFullParts:=TotalSize SHR 30;
 LastIncompletePartSize:=TotalSize And $3FFFFFFF;

Open in new window

then,in your thread Create, instead of using an EndOffset, use a Part Size to download (Integer).
You'll end up with a much cleaner thread code and an easier way to create those threads (only ONE int64 value is needed, the offset):
Somewhere in your code you'll have to use what Split1GoParts returns :

PartSize:=1 SHL 30;
for i:=0 to NbFullParts-1 do
 CreateThread ( alabel[I], apbar[I], hOpenFile[I], hInetFile[I], i*PartSize, PartSize);

Open in new window

Where CreateThread function is of course something that creates the thread and add it to your manager.
TFetchDataThread = class(TThread)
    FStartOffset    : Int64;
    FPartSize       : Integer;
    FInetFile       : HINTERNET;
    FDestFile       : THandle;
    FProgressBar    : TProgressBar;
    FLabelProgress  : TLabel;
    FLoadedSize     : Integer;
    procedure ProgressGui;
    procedure Execute; override;
    constructor Create(
    LabelProgress : TLabel;
    Progress      : TProgressBar;
    DestFile      : THandle;
    InetFile      : HINTERNET;
    StartOffset  : Int64;
    PartSize    : Integer);
    destructor Destroy;

constructor TFetchDataThread.Create(
LabelProgress: TLabel;
Progress     : TProgressBar;
DestFile     : THandle;
InetFile     : HINTERNET;
StartOffset  : Int64;
PartSize    : Integer);
  FStartOffset    := StartOffset;
  FPartSize       := PartSize;
//  FEndOffset      := EndOffset;
  FInetFile       := InetFile;
  FDestFile       := DestFile;
  FProgressBar    := Progress;
  FLabelProgress  := LabelProgress;
  inherited Create( {DAPClass.UpdateProgressBar,} False );

procedure TFetchDataThread.ProgressGui;
  FProgressBar.Value := FLoadedSize;
  FProgressBar.Repaint; // Avoid gui problems
  FLabelProgress.Text :=
  Format( 'Thread: %d, Start Offset: %d (%x h), Loaded %d / %d (%f %%)',
   [ThreadID, FStartOffset, FStartOffset, FLoadedSize, FPartSize, 100*FLoadedSize/FPartSize ] );

procedure TFetchDataThread.Execute;
  TypeByteArray = array [1..1024*2] of Byte;
  Buffer            : TypeByteArray;
 FreeOnTerminate := True;
 FProgressBar.Min :=  0;
 FProgressBar.Max :=  FPartSize;
 InternetSetFilePointer( FInetFile, FStartOffset, nil, FILE_BEGIN, 0 );
 SetFilePointer( FDestFile, FStartOffset, nil, FILE_BEGIN );
   BytesToReadWrite := FPartSize - FLoadedSize;
   if BytesToReadWrite > SizeOf( Buffer ) 
     Then BytesToReadWrite  := SizeOf( Buffer )

   InternetReadFile( FInetFile, @Buffer, BytesToReadWrite, BufferLen );

   LockFile( FDestFile, FStartOffset, 0, BytesToReadWrite, 0 );
   WriteFile( FDestFile, Buffer, BytesToReadWrite, BytesWritten, nil );
   UnlockFile( FDestFile, FStartOffset, 0, BytesToReadWrite, 0 );
   Synchronize( ProgressGui );

// Why reducing CPU in the download thread ? it will just slow down everything.
//    Sleep( Random( 15 ) ); // Avoid cpu usage
  until (FLoadedSize>=FPartSize) or (Terminated);
  CloseHandle( FDestFile );
  InternetCloseHandle( FInetFile );

Open in new window


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 127
Magic Software info 18 144
How to Get Images From Server to Client using App Tethering 1 41
delphi popmenu non latine charcters 3 35
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question