-
CopyFileEx API - NullReferenceException
I am trying to use the CopyFileEx API in a VB.NET app and I am getting a
"object reference not set to an instance of an object" error every time I
run.
If I comment out the callback function definition (cb = AddressOf
Me.myCopyProgressRoutine) then it copies the file just fine but of course I
don't have any way to update progress.
I have simplified and included my code below for anybody to look at. I am
totally out of ideas on this one.
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Delegate Function CopyProgressRoutine( _
ByVal TotalFileSize As Long, _
ByVal TotalBytesTransferred As Long, _
ByVal StreamSize As Long, _
ByVal StreamBytesTransferred As Long, _
ByVal dwStreamNumber As Long, _
ByVal dwCallbackReason As Long, _
ByVal hSourceFile As Long, _
ByVal hDestinationFile As Long, _
ByVal lpData As Long _
) As Long
Declare Auto Function CopyFileEx Lib "kernel32.dll" ( _
ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CopyProgressRoutine, _
ByVal lpData As Long, _
ByVal lpBool As Long, _
ByVal dwCopyFlags As Long _
) As Boolean
Public Function myCopyProgressRoutine( _
ByVal TotalFileSize As Long, _
ByVal TotalBytesTransfered As Long, _
ByVal StreamSize As Long, _
ByVal StreamBytesTransferred As Long, _
ByVal dwStreamNumber As Long, _
ByVal dqCallbackReason As Long, _
ByVal hSourceFile As Long, _
ByVal hDestinationFile As Long, _
ByVal lpData As Long _
) As Long
copyForm.ProgressBar.Value = TotalBytesTransfered / TotalFileSize *
100
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cb As CopyProgressRoutine
cb = AddressOf Me.myCopyProgressRoutine
copyForm.Show()
CopyFileEx("c:\eric.log", "c:\logs\eric1.log", cb, 0, False, 0)
copyForm.Hide()
End Sub
End Class
Any ideas as to why the error is occurring?
Thanks,
Eric
-
Re: CopyFileEx API - NullReferenceException
> Any ideas as to why the error is occurring?
Eric: Try changing your Longs to Integers.
--
Phil Weber
-
Re: CopyFileEx API - NullReferenceException
Phil,
Unfortunately, I have tried that and still have no luck. I must be
miss-defining something else but I can't find it. The new code looks like
this:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Delegate Function CopyProgressRoutine( _
ByVal TotalFileSize As Integer, _
ByVal TotalBytesTransferred As Integer, _
ByVal StreamSize As Integer, _
ByVal StreamBytesTransferred As Integer, _
ByVal dwStreamNumber As Integer, _
ByVal dwCallbackReason As Integer, _
ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, _
ByVal lpData As Integer _
) As Integer
' Here's where the CopyFileEx method is located, and its signature.
Declare Auto Function CopyFileEx Lib "kernel32.dll" ( _
ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CopyProgressRoutine, _
ByVal lpData As Integer, _
ByVal lpBool As Integer, _
ByVal dwCopyFlags As Integer _
) As Integer
Public Function myCopyProgressRoutine( _
ByVal TotalFileSize As Integer, _
ByVal TotalBytesTransfered As Integer, _
ByVal StreamSize As Integer, _
ByVal StreamBytesTransferred As Integer, _
ByVal dwStreamNumber As Integer, _
ByVal dqCallbackReason As Integer, _
ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, _
ByVal lpData As Integer _
) As Integer
copyForm.ProgressBar.Value = TotalBytesTransfered / TotalFileSize *
100
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cb As CopyProgressRoutine
cb = AddressOf Me.myCopyProgressRoutine
copyForm.Show()
CopyFileEx("c:\eric.log", "c:\logs\eric1.log", cb, 0, False, 0)
copyForm.Hide()
End Sub
End Class
Any Other thoughts...
Thanks,
Eric
"Phil Weber" <pweber@nospam.fawcette.com> wrote in message
news:3d9c9f9f@10.1.10.29...
> > Any ideas as to why the error is occurring?
>
> Eric: Try changing your Longs to Integers.
> --
> Phil Weber
>
>
-
Re: CopyFileEx API - NullReferenceException
> Unfortunately, I have tried that and still have no luck.
> I must be mis-defining something else but I can't find it.
Eric: Well, I was able to get your code to run without error by changing your
False parameter in the call to CopyFileEx to 0:
CopyFileEx("c:\eric.log", "c:\logs\eric1.log", cb, 0, 0, 0)
The file gets copied and the callback routine gets called, but
TotalBytesTransfered is always 0, and I have to kill the app in Task Manager. I
don't know what else to suggest at this point.
Why not use SHFileOperation instead? It can display Windows' built-in progress
dialog.
--
Phil Weber
-
Re: CopyFileEx API - NullReferenceException
I would use SHFileOperation but I am actually planning on storing the copy
percentage in a var for use elsewhere. This is problem is driving me nuts
because I know it is probably just a simple incorrect declaration somewhere
but I can't seem to find it.
Using API's in .NET at this point can be very problematic.
I have run into the freezing problem but only when I try and copy files
smaller then 10k. Do you have a file that is larger then 1MB that you could
try with?
Thanks for your help,
Eric
"Phil Weber" <pweber@nospam.fawcette.com> wrote in message
news:3d9cacc5@10.1.10.29...
> > Unfortunately, I have tried that and still have no luck.
> > I must be mis-defining something else but I can't find it.
>
> Eric: Well, I was able to get your code to run without error by changing
your
> False parameter in the call to CopyFileEx to 0:
>
> CopyFileEx("c:\eric.log", "c:\logs\eric1.log", cb, 0, 0, 0)
>
> The file gets copied and the callback routine gets called, but
> TotalBytesTransfered is always 0, and I have to kill the app in Task
Manager. I
> don't know what else to suggest at this point.
>
> Why not use SHFileOperation instead? It can display Windows' built-in
progress
> dialog.
> --
> Phil Weber
>
>
-
Re: CopyFileEx API - NullReferenceException
> Do you have a file that is larger then 1MB that
> you could try with?
Eric: The file I tested with is 12.8 MB.
--
Phil Weber
-
Re: CopyFileEx API - NullReferenceException
"Eric Immerman" <NewsGroup@theatertec.com> wrote in message
news:3d9cb60e@10.1.10.29...
> I would use SHFileOperation but I am actually planning on storing the copy
> percentage in a var for use elsewhere. This is problem is driving me nuts
> because I know it is probably just a simple incorrect declaration
somewhere
> but I can't seem to find it.
>
> Using API's in .NET at this point can be very problematic.
>
> I have run into the freezing problem but only when I try and copy files
> smaller then 10k. Do you have a file that is larger then 1MB that you
could
> try with?
>
> Thanks for your help,
> Eric
Eric,
See my reply over on the microsoft group... I have an example program I
posted.
Tom Shelton
-
Re: CopyFileEx API - NullReferenceException
Thanks so much for your help with this. Late last night a friend of mine
also sent me a solution. He did it with the code changes below. Working
with API's in VB.NET can definitely by problematic. It is basically like
working with a Rubik Cube and trying to find the correct combination that
will allow you to cast the variables. I would pay big money for a good .NET
resource that had all the API functions converted for .NET.
Although I guess most have already been converted to some sort of built in
function in .NET there are still a few that come in very handy.
Public Delegate Function CopyProgressRoutine( _
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
Declare Auto Function CopyFileEx Lib "kernel32.dll" ( _
ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CopyProgressRoutine, _
ByVal lpData As Int32, _
ByVal lpBool As Int32, _
ByVal dwCopyFlags As Int32 _
) As Int32
Thanks Again,
Eric
"Tom Shelton" <tom@mtogden.com> wrote in message news:3d9ce4e7@10.1.10.29...
>
> "Eric Immerman" <NewsGroup@theatertec.com> wrote in message
> news:3d9cb60e@10.1.10.29...
> > I would use SHFileOperation but I am actually planning on storing the
copy
> > percentage in a var for use elsewhere. This is problem is driving me
nuts
> > because I know it is probably just a simple incorrect declaration
> somewhere
> > but I can't seem to find it.
> >
> > Using API's in .NET at this point can be very problematic.
> >
> > I have run into the freezing problem but only when I try and copy files
> > smaller then 10k. Do you have a file that is larger then 1MB that you
> could
> > try with?
> >
> > Thanks for your help,
> > Eric
>
> Eric,
>
> See my reply over on the microsoft group... I have an example program I
> posted.
>
> Tom Shelton
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks