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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now