🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9767

CopyFileEx - CopyProgressRoutine - devision by zero

#9767
From: Tommy
Date: Mon, 14 Jun 2004 23:02
121 lines
4386 bytes
Hi,

in my application is use CopyFileEx to copy completly folders with all there
files. these files may also be larger than 2gb.

here are three of my main functions for my problem:

in my header:

// bytes of all files i need to copy
unsigned __int64 size;

// just copied bytes of a file
unsigned __int64 tmp;

// all bytes already copied
unsigned __int64 already_copied;


void __fastcall TForm1::dir_create(AnsiString source, AnsiString target)
{
   TSearchRec sr;
   source = IncludeTrailingBackslash(source);
   AnsiString wildcard = source + "*";
   target = IncludeTrailingBackslash(target);

   //set attributes
   int iAttributes = faDirectory;

   if (FindFirst(wildcard,iAttributes, sr) == 0)
   {
      do
      {
         // folder
         if( (sr.Attr & faDirectory) && (sr.Name != ".") && (sr.Name !=
"..") )
         {
            // create folder when needed
            if (!DirectoryExists(target + sr.Name))
            {
               CreateDir(target + sr.Name);
            }

            LStatus2->Caption = target + sr.Name;
            Application->ProcessMessages();
            dir_create(source + sr.Name, target + sr.Name);
         }

         // file
         if( (sr.Attr & faAnyFile) && (sr.Name != ".") && (sr.Name !=
"..") )
         {
            LStatus2->Caption = target + sr.Name;
            Application->ProcessMessages();

            // beginning new file, so 0 bytes copied
            tmp = 0;
            CopyTheFile(source + sr.Name,target + sr.Name);
            already_copied += tmp;
         }

      }
      while( FindNext(sr) == 0 );
      FindClose(sr);

   }
   FindClose(sr);
}
//--------------------------------------------------------------------------
-


DWORD WINAPI MyCopyProgressRoutine(LARGE_INTEGER TotalFileSize,LARGE_INTEGER
TotalBytesTransferred,LARGE_INTEGER StreamSize,LARGE_INTEGER
StreamBytesTransferred,DWORD dwStreamNumber,DWORD dwCallbackReason,HANDLE
hSourceFile,HANDLE hDestinationFile,LPVOID lpData)
{
   TForm1 *pForm = (TForm1*) lpData;

   // actual file status
   unsigned __int64 Percent = (((unsigned
__int64)TotalBytesTransferred.QuadPart)*100) / ((unsigned
__int64)TotalFileSize.QuadPart);
   pForm->CGauge1->Progress = Percent;

   // status of all files
   pForm->tmp = (unsigned __int64)TotalBytesTransferred.QuadPart;
   Percent = ((unsigned
__int64)((pForm->already_copied+pForm->tmp)*100))/(unsigned
__int64)(pForm->size);
   pForm->CGauge2->Progress = Percent;

   // Updaten
   pForm->Update();
   Application->ProcessMessages();

   return PROGRESS_CONTINUE;
}
//--------------------------------------------------------------------------
-

// copy a file
bool __fastcall TForm1::CopyTheFile(String source, String target)
{
   BOOL bCancelled = FALSE;
   bool back = CopyFileEx(source.c_str(),
target.c_str(),MyCopyProgressRoutine, this, &bCancelled, 0);
   return back;
}
//--------------------------------------------------------------------------
-

In my copyprogressroutine, i have some source to calcuale how man percent
have already copied of that file and of the hole process. As i work with big
files i use unsigned __int64. but when i run my appliaction i always get
after a short time the message "devision by zero" and the application stops.
when i comment the calculation of the percent, the application run fine.
what is wrong with my calcution. what to change that is calculate the copied
bytes of the file and of the hole process?

tommy
Technical Details
Message-ID: <40ce2e93@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,9a3709a4832c0e51,start
X-Google-Attributes: gid10cc67,public
From: "Tommy" <tommyw@yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: CopyFileEx - CopyProgressRoutine - devision by zero
Date: Tue, 15 Jun 2004 01:02:34 +0200
Lines: 125
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 84.129.30.227
Message-ID: <40ce2e93@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1087254163 84.129.30.227 (14 Jun 2004 16:02:43 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail