Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CopyFileEx with callback never returns

Status
Not open for further replies.

Vachaun22

Programmer
Oct 7, 2003
171
US
I am trying to use the CopyFileEx function with a callback in order to display a progress bar. However, something does not seem to be working correctly.

When I use the callback function, the CopyFileEx function never returns even after the copy process has completed. Also, the progress bar never gets updated with the progress.

Code:
UINT CopyThreadFunc( LPVOID pParam )
{
    THREADPARAMS* ptp = (THREADPARAMS*)pParam;
    HWND hWnd = ptp->hWnd;
    CString copyFrom = ptp->copyFrom;
    CString copyTo = ptp->copyTo;
    delete ptp;

	
    CopyFileEx( copyFrom,
                copyTo,
		(LPPROGRESS_ROUTINE)CopyProgressRoutine,
		(LPVOID)hWnd,
		FALSE,
		0 );
    // Tell the parent to close the dialog
    PostMessage( hWnd, WM_CLOSE, 0, 0 );
    TRACE( "Ending thread\n" );
    return 0;
}

DWORD CALLBACK 
    CopyProgressRoutine( LARGE_INTEGER TotalFileSize,
                         LARGE_INTEGER TotalBytesTransferred,
                         LARGE_INTEGER StreamBytesTransferred,
                         DWORD dwStreamNumber,
                         DWORD dwCallbackReason,
                         HANDLE hSourceFile,
                         HANDLE hDestinationFile,
                         LPVOID lpData )
{
    double nPercentage = (double(TotalBytesTransferred.QuadPart))/
                        ((double)TotalFileSize.QuadPart)*100;
    TRACE( "%0.2f\n", nPercentage );
    if( nPercentage >= 100 )
	return PROGRESS_CONTINUE;

    int nPercent = (int)nPercentage;
    ::PostMessage(  HWND(lpData), 
                    WM_USER_UPDATE_PROGRESS, 
                    nPercent, 
                    0 );
    return PROGRESS_CONTINUE;
}

The structure is rather basic:

Code:
typedef struct tagTHREADPARAMS {
	HWND hWnd;
	CString copyFrom;
	CString copyTo;
} THREADPARAMS;

What really confuses me is the fact that with the callback, CopyFileEx never returns (the trace never happens after the function call), but there are no more traces from the call back function, so it must no longer be reporting statuses after 100%.

I have looked for a few days for a solutions on this, even found solutions that claim to work with code nearly identical to the code I am using, still no dice.

Anyone have any ideas of using CopyFileEx with a callback?

Thanks.
 
As far as I know, CopyProgressRoutine has 9 parameters.
The 3rd parameter is LARGE_INTEGER StreamSize...

It seems you convert a pointer to invalid function to LPPROGRESS_ROUTINE type in C-style cast...
 
Uggggh...apparently my eyes aren't as good as I thought they were.

Thanks ArkM. That did the trick.

I looked at the MSDN documentation for the callback function prototype countless times and never noticed I missed the StreamSize parameter...

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top