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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
Newbie Thread Programming 1 161
indy Tmemory stream non latin charcter issue 2 121
control image tags in a string ? 12 142
FMX and jaudiotracker playing memory stream 29 87
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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