Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   problems with CopyFileEX (http://www.programmingforums.org/showthread.php?t=14300)

badbasser98 Oct 30th, 2007 1:00 PM

problems with CopyFileEX
 
This is my first time trying to copy a file using code. I am having trouble getting the function call correct. Could someone please look at what I am doing wrong?
:


BOOL WINAPI CopyFileEx(
                    LPCTSTR "\\\\PC1\\Directory\\filename.pdf",
                    LPCTSTR "\\\\PC2\\Directory\\filename.pdf",
                    LPPROGRESS_ROUTINE NULL,
                    LPVOID NULL,
                    LPBOOL false,
                    DWORD COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE);

Should I be learning sockets instead since I an trying to copy a file from one copmuter to another?

This is on a Win XP Pro SP2 box using Dev-C++ 4.9.9.2

Thanks,
-BB98

Prm753 Oct 30th, 2007 2:00 PM

Re: problems with CopyFileEX
 
Take out the types. I can't even compile with them in there. The function has already been written in windows.h, just call it. For example:

:

CopyFileEx("C:\\Dev-Cpp\\1.txt", "C:\\2.txt", NULL, NULL, false, COPY_FILE_FAIL_IF_EXISTS | COPY_FILE_RESTARTABLE);

Works fine for me to copy and paste on a local computer. Dev-C++ 4.9.9.2 here as well.

badbasser98 Oct 31st, 2007 11:50 AM

Re: problems with CopyFileEX
 
Thanks for the reply. I included the types in my post to try and be as clear as I could in the post. I did not have the types in my function though I did have a typo I found.

On another note, I am trying to use the CopyProgressRoutine CALLBACK. As usual, I am in in a bit over my head. I haven't a clue where to start to get it working. Do I have to declare and define the function in my code? I have visited MSDN, but feel like an idiot because I cannot make heads or tails of what its telling me :confused:

Does anyone know of a tutorial or other guide to help me understand its use?

thanks,
-BB98

DaWei Oct 31st, 2007 1:15 PM

Re: problems with CopyFileEX
 
You have to write your own CopyProgressRoutine. You need to define it as specified:
Quote:

Originally Posted by MSDN
:

DWORD CALLBACK CopyProgressRoutine(
  [in]                LARGE_INTEGER TotalFileSize,
  [in]                LARGE_INTEGER TotalBytesTransferred,
  [in]                LARGE_INTEGER StreamSize,
  [in]                LARGE_INTEGER StreamBytesTransferred,
  [in]                DWORD dwStreamNumber,
  [in]                DWORD dwCallbackReason,
  [in]                HANDLE hSourceFile,
  [in]                HANDLE hDestinationFile,
  [in]                LPVOID lpData
);


Obviously, you can change the names to anything you like.

badbasser98 Feb 15th, 2008 2:35 PM

Re: problems with CopyFileEX
 
Its been a while since I have been able to work on this program, but have got back into it yesterday. I have the copy and callback functions working, I am having trouble calculating the percentage of the file that's being copied however. I am using that data to increment a progressbar. Through some research it appears that the problem is with the LARGE_INTEGER type.

Does anyone have any documentation (or links) on how to use math functions with that type? I was hoping it was going to be as easy as

:


Percent = (TotalFileSize / TotalBytesTransferred) * 100;


It obviously isn't since I get the error

:


no match for 'operator/' in 'TotalFileSize / TotalBytesTransferred'


Thank you for your help thus far :)

null_ptr0 Feb 15th, 2008 3:47 PM

Re: problems with CopyFileEX
 
LARGE_INTEGER maybe doesn't define operator/(const LARGE_INTEGER)?

badbasser98 Feb 27th, 2008 2:36 PM

Re: problems with CopyFileEX
 
anyone have a clue what's going on with this?

Thanks

Cache Feb 27th, 2008 6:43 PM

Re: problems with CopyFileEX
 
LARGE_INTEGER is a union type. You have to access it's data members which are actually numeric types.

example:
:

LARGE_INTEGER x, y;
LONGLONG z = (x.QuadPart / y.QuadPart) * 100;


http://msdn2.microsoft.com/en-us/lib...13(VS.85).aspx

badbasser98 Mar 20th, 2008 4:14 PM

Re: problems with CopyFileEX
 
Thanks Cache, that works though I am still having problems with it working for some reason.

I have resorted to setting individual variables equal to each part and number that is included in the math portion of figuring the amount of the file that has been copied.

I have set TotalFileSize.QuadPart equal to Total and TotalBytesTransfered.QuadPart equal to Transfer. If I look at either of these values at each call to the progress routine, Total is the correct size of the file and Transfer is increasing in size appropriately until it is equal to Total when the copy function ends.

I have a few other variables: Divide, Percent, & Hundred. Divide is a float, Percent is an int and Hundred is an int whose value is 100.

This is the bit of math I have in my CALLBACK_CHUNK_FINISHED:
:


Divide = (Transfer / Total);
Percent = (Divide * Hundred);


I have also tried:
:

Percent = ((Transfer / Total)*(Hundred));

With either method, at any step in the progress Divide is zero and Percent is zero in which Total and Transfer are their correct values. The only time Percent is not zero is when the process is complete and Transfer and Total are equal. Obviously Percent then equals 100 and the progress bar is stepped to the end.

This makes no sense to me at all. Anyone know something else I can try, or am I overlooking something simple again?

Thanks,
~BB98

Arla Mar 20th, 2008 4:47 PM

Re: problems with CopyFileEX
 
My guess (and it is a guess) is that Total and Transfer are ints (or some variant like that) and when you divide one by the other the result is an int (even if you put it into a float, it's doing integer division)

Try it, I wrote a quick program that did

int a = 5;
int b = 6;
float c = a/b;

Result is 0 because a and b are ints. have to do c = (float)a/(float)b (or something like that, I'm using C# so syntax may be slightly different).


All times are GMT -5. The time now is 3:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC