CopyFileEx error...

G

Guest

I've been executing the CopyFileEx code below in a Windows Application for
several months...

Try
If CopyFileEx(fiSource.FullName, strTargetFile, Fpr, ACTION_COPY, 0,
0) > 0 Then
Else
Throw New System.ComponentModel.Win32Exception( _
System.Runtime.InteropServices.Marshal.GetLastWin32Error)
End If
Catch ex As Exception
msgbox ex.Message
End Try

But now its bombing when it encounters a file containing 0 bytes. The error
message I'm getting is:

Run-time exception thrown : System.OverflowException - Value was either
too large or too small for an Int32.

Any idea why the CopyFileEx return value is causing an overflow? How can I
deal with this problem?

Any help would be greatly appreicated!
 
M

Mattias Sjögren

Any idea why the CopyFileEx return value is causing an overflow? How can I
deal with this problem?

Probably due to an incorrect API declaration. The return type should
be a Boolean.


Mattias
 
G

Guest

I figured out the problem... my delegate function had a bug... I was dividing
by zero when the filesize of the file being copied was 0.

Note this declaration:
Dim Fpr As New FileProgressRoutine(AddressOf FileProgress)

ORIGINAL
Private Function FileProgress( _
ByVal totalFileSize As Int64, _
ByVal totalBytesTransferred As Int64, _
ByVal streamSize As Int64, _
ByVal streamBytesTransferred As Int64, _
ByVal dwStreamNumber As Int32, _
ByVal dwCallbackReason As Int32, _
ByVal hSourceFile As Int32, _
ByVal hDestinationFile As Int32, _
ByVal lpData As Int32) As Int32
Me.pgAction.Value = Convert.ToInt64((totalBytesTransferred *
100.0 / totalFileSize))

CHANGED
Private Function FileProgress( _
ByVal totalFileSize As Int64, _
ByVal totalBytesTransferred As Int64, _
ByVal streamSize As Int64, _
ByVal streamBytesTransferred As Int64, _
ByVal dwStreamNumber As Int32, _
ByVal dwCallbackReason As Int32, _
ByVal hSourceFile As Int32, _
ByVal hDestinationFile As Int32, _
ByVal lpData As Int32) As Int32
If totalFileSize > 0 Then
Me.pgAction.Value = Convert.ToInt64((totalBytesTransferred *
100.0 / totalFileSize))
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top