473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

callbacks in vb.net and c# using copyfileex

I have been trying to code a callback in vb.net using the delegate method. I
have successfully compiled the program and run it but after it accessess the
copyfileex winapi method and then calls the callback method there are
problems. For one the data that is populated in the parameters is wrong. I am
expecting a value of 0 , 1 or maybe 2 for the dwcallbackreason and I get some
outrageous number like 10788264 which incidentally is the same each time I
run it, even on different machines. If that were not enough the callback
method once it completes and returns, returns back into itself. At that point
it goes through one more time and then dies. Now I thought there might be a
bug or something since all the code I had referenced on the subject is close
to mine and I have seen other comments regarding not being able to get this
to work so I re-coded it in C# just to check.
In C# the code behaves slightly better in that the numbers coming in for
the dwcallback reason are what I expect, a 0 for the dwcallback reason but
the method still calls itself again. Any help would be greatly appreciated. I
will post code for a reference if needed.

This all worked fine in vb6.

Nov 21 '05 #1
3 5107
In article <1A**********************************@microsoft.co m>, Paul wrote:
I have been trying to code a callback in vb.net using the delegate method. I
have successfully compiled the program and run it but after it accessess the
copyfileex winapi method and then calls the callback method there are
problems. For one the data that is populated in the parameters is wrong. I am
expecting a value of 0 , 1 or maybe 2 for the dwcallbackreason and I get some
outrageous number like 10788264 which incidentally is the same each time I
run it, even on different machines. If that were not enough the callback
method once it completes and returns, returns back into itself. At that point
it goes through one more time and then dies. Now I thought there might be a
bug or something since all the code I had referenced on the subject is close
to mine and I have seen other comments regarding not being able to get this
to work so I re-coded it in C# just to check.
In C# the code behaves slightly better in that the numbers coming in for
the dwcallback reason are what I expect, a 0 for the dwcallback reason but
the method still calls itself again. Any help would be greatly appreciated. I
will post code for a reference if needed.

This all worked fine in vb6.


Post the code....

--
Tom Shelton [MVP]
Nov 21 '05 #2
Here is the C# code - the VB.net codee is below - It is kinda long

Imports System.Runtime.InteropServices

Module Module1
Public Const PROGRESS_CONTINUE As Integer = 0
Public Const PROGRESS_CANCEL As Integer = 1
Public Const PROGRESS_STOP As Integer = 2
Public Const PROGRESS_QUIET As Integer = 3

'CopyFileEx callback routine state change values
Public Const CALLBACK_CHUNK_FINISHED As Integer = &H0S
Public Const CALLBACK_STREAM_SWITCH As Integer = &H1S

'CopyFileEx option flags
Public Const COPY_FILE_FAIL_IF_EXISTS As Integer = &H1S
Public Const COPY_FILE_RESTARTABLE As Integer = &H2S
Public Const COPY_FILE_OPEN_SOURCE_FOR_WRITE As Integer = &H4S

Public Declare Auto Function CopyFileEx Lib "kernel32" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CPCallback, ByVal lpData As Long, ByVal
pbCancel As Boolean, _
ByVal dwCopyFlags As Integer) As Integer

Public Delegate Function CPCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As
Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Public Function CopyProgressCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As
Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Select Case dwCallbackReason
Case CALLBACK_STREAM_SWITCH

'this value is passed whenever the
'callback is initialized for each file.
'F_Migration.DefInstance.ProgressBar1.Value = 0
'F_Migration.DefInstance.ProgressBar1.Min = 0
'F_Migration.DefInstance.ProgressBar1.Max = (TotalFileSize *
10000)
'F_Migration.DefInstance.ProgressBar1.CtlRefresh()

CopyProgressCallback = PROGRESS_CONTINUE

Case CALLBACK_CHUNK_FINISHED

'called when a block has been copied
'F_Migration.DefInstance.ProgressBar1.Value =
(TotalBytesTransferred * 10000)

'optional. While the app is copying it
'will not respond to input for canceling.
System.Windows.Forms.Application.DoEvents()

CopyProgressCallback = PROGRESS_CONTINUE

End Select
CopyProgressCallback = PROGRESS_CONTINUE

End Function
End Module

vb.net

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lpCallback As CPCallback
Dim bUseCallback As Boolean
Dim fcSuccess As Integer
Dim sSourcefile, sTargetFile As String
Dim bcancelbackup As Boolean
bcancelbackup = False
bUseCallback = True

sSourcefile = "c:\document.xml"
sTargetFile = "c:\temp\document.xml"
'
If bUseCallback Then

fcSuccess = CopyFileEx(sSourcefile, sTargetFile, AddressOf
CopyProgressCallback, _
0, bcancelbackup, COPY_FILE_RESTARTABLE) = 1
End If
End Sub

Option Strict Off
Option Explicit On
Module Module1

Public Const MAXDWORD As Integer = &HFFFFFFFF
Public Const MAX_PATH As Integer = 260
Public Const INVALID_HANDLE_VALUE As Integer = -1
Public Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10S

'Define possible return codes from the CopyFileEx callback routine
Public Const PROGRESS_CONTINUE As Integer = 0
Public Const PROGRESS_CANCEL As Integer = 1
Public Const PROGRESS_STOP As Integer = 2
Public Const PROGRESS_QUIET As Integer = 3

'CopyFileEx callback routine state change values
Public Const CALLBACK_CHUNK_FINISHED As Integer = &H0S
Public Const CALLBACK_STREAM_SWITCH As Integer = &H1S

'CopyFileEx option flags
Public Const COPY_FILE_FAIL_IF_EXISTS As Integer = &H1S
Public Const COPY_FILE_RESTARTABLE As Integer = &H2S
Public Const COPY_FILE_OPEN_SOURCE_FOR_WRITE As Integer = &H4S

Public Declare Function CopyFileEx Lib "kernel32" Alias "CopyFileExA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CPCallback, ByVal lpData As Long, ByVal
pbCancel As Integer, _
ByVal dwCopyFlags As Integer) As Integer

Public Delegate Function CPCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As
Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Public Function CopyProgressCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As
Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _
ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Select Case dwCallbackReason
Case CALLBACK_STREAM_SWITCH

'this value is passed whenever the
'callback is initialized for each file.

'F_Migration.DefInstance.ProgressBar1.Value = 0
'F_Migration.DefInstance.ProgressBar1.Min = 0
'F_Migration.DefInstance.ProgressBar1.Max = (TotalFileSize *
10000)
'F_Migration.DefInstance.ProgressBar1.CtlRefresh()

CopyProgressCallback = PROGRESS_CONTINUE

Case CALLBACK_CHUNK_FINISHED

'called when a block has been copied
'F_Migration.DefInstance.ProgressBar1.Value =
(TotalBytesTransferred * 10000)

'optional. While the app is copying it
'will not respond to input for canceling.
System.Windows.Forms.Application.DoEvents()

CopyProgressCallback = PROGRESS_CONTINUE

End Select
CopyProgressCallback = PROGRESS_CONTINUE

End Function
End Module

Thanks

Paul
"Tom Shelton" wrote:
In article <1A**********************************@microsoft.co m>, Paul wrote:
I have been trying to code a callback in vb.net using the delegate method. I
have successfully compiled the program and run it but after it accessess the
copyfileex winapi method and then calls the callback method there are
problems. For one the data that is populated in the parameters is wrong. I am
expecting a value of 0 , 1 or maybe 2 for the dwcallbackreason and I get some
outrageous number like 10788264 which incidentally is the same each time I
run it, even on different machines. If that were not enough the callback
method once it completes and returns, returns back into itself. At that point
it goes through one more time and then dies. Now I thought there might be a
bug or something since all the code I had referenced on the subject is close
to mine and I have seen other comments regarding not being able to get this
to work so I re-coded it in C# just to check.
In C# the code behaves slightly better in that the numbers coming in for
the dwcallback reason are what I expect, a 0 for the dwcallback reason but
the method still calls itself again. Any help would be greatly appreciated. I
will post code for a reference if needed.

This all worked fine in vb6.


Post the code....

--
Tom Shelton [MVP]

Nov 21 '05 #3
Hi,
<snip>
Public Declare Auto Function CopyFileEx Lib "kernel32" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CPCallback, ByVal lpData As Long, ByVal
pbCancel As Boolean, ByVal dwCopyFlags As Integer) As Integer
Public Declare Auto Function CopyFileEx Lib "kernel32" ( _
ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal lpProgressRoutine As CPCallback, _
ByRef lpData As Long, _
ByRef pbCancel As Boolean, _
ByVal dwCopyFlags As Integer) As Integer
This way CopyFileEx will work, but changing pbCancel during copy will not
have an effect. If you need it to work, then there are workarounds, but I
guess you don't need it because you can always cancel it from within
ProgressRoutine using the return value.

Public Delegate Function CPCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _ ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer
VB6 has no 64bit integer, so Currency * 10000 was a hack.
VB.NET has a 64bit integer data type : Long.

Public Delegate Function CPCallback( _
ByVal TotalFileSize As Long, _
ByVal TotalBytesTransfered As Long, _
ByVal StreamSize As Long, _
ByVal StreamBytesTransfered As Long, _
ByVal StreamNumber As Integer, _
ByVal dwCallbackReason As Integer, _
ByVal hSourceFile As IntPtr, _
ByVal hDestFile As IntPtr, _
ByRef lpData As Long ) As Integer
HTH,
greetings


Public Function CopyProgressCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _ ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Select Case dwCallbackReason
Case CALLBACK_STREAM_SWITCH

'this value is passed whenever the
'callback is initialized for each file.
'F_Migration.DefInstance.ProgressBar1.Value = 0
'F_Migration.DefInstance.ProgressBar1.Min = 0
'F_Migration.DefInstance.ProgressBar1.Max = (TotalFileSize * 10000)
'F_Migration.DefInstance.ProgressBar1.CtlRefresh()

CopyProgressCallback = PROGRESS_CONTINUE

Case CALLBACK_CHUNK_FINISHED

'called when a block has been copied
'F_Migration.DefInstance.ProgressBar1.Value =
(TotalBytesTransferred * 10000)

'optional. While the app is copying it
'will not respond to input for canceling.
System.Windows.Forms.Application.DoEvents()

CopyProgressCallback = PROGRESS_CONTINUE

End Select
CopyProgressCallback = PROGRESS_CONTINUE

End Function
End Module

vb.net

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lpCallback As CPCallback
Dim bUseCallback As Boolean
Dim fcSuccess As Integer
Dim sSourcefile, sTargetFile As String
Dim bcancelbackup As Boolean
bcancelbackup = False
bUseCallback = True

sSourcefile = "c:\document.xml"
sTargetFile = "c:\temp\document.xml"
'
If bUseCallback Then

fcSuccess = CopyFileEx(sSourcefile, sTargetFile, AddressOf
CopyProgressCallback, _
0, bcancelbackup, COPY_FILE_RESTARTABLE) = 1
End If
End Sub

Option Strict Off
Option Explicit On
Module Module1

Public Const MAXDWORD As Integer = &HFFFFFFFF
Public Const MAX_PATH As Integer = 260
Public Const INVALID_HANDLE_VALUE As Integer = -1
Public Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10S

'Define possible return codes from the CopyFileEx callback routine
Public Const PROGRESS_CONTINUE As Integer = 0
Public Const PROGRESS_CANCEL As Integer = 1
Public Const PROGRESS_STOP As Integer = 2
Public Const PROGRESS_QUIET As Integer = 3

'CopyFileEx callback routine state change values
Public Const CALLBACK_CHUNK_FINISHED As Integer = &H0S
Public Const CALLBACK_STREAM_SWITCH As Integer = &H1S

'CopyFileEx option flags
Public Const COPY_FILE_FAIL_IF_EXISTS As Integer = &H1S
Public Const COPY_FILE_RESTARTABLE As Integer = &H2S
Public Const COPY_FILE_OPEN_SOURCE_FOR_WRITE As Integer = &H4S

Public Declare Function CopyFileEx Lib "kernel32" Alias "CopyFileExA" _ (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _ ByVal lpProgressRoutine As CPCallback, ByVal lpData As Long, ByVal
pbCancel As Integer, _
ByVal dwCopyFlags As Integer) As Integer

Public Delegate Function CPCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _ ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Public Function CopyProgressCallback(ByVal TotalFileSize As Decimal, _
ByVal TotalBytesTransferred As Decimal, ByVal StreamSize As
Decimal, _
ByVal StreamBytesTransferred As Decimal, ByVal dwStreamNumber As Integer, _
ByVal dwCallbackReason As Integer, ByVal hSourceFile As Integer, _ ByVal hDestinationFile As Integer, ByRef lpData As Integer) As
Integer

Select Case dwCallbackReason
Case CALLBACK_STREAM_SWITCH

'this value is passed whenever the
'callback is initialized for each file.

'F_Migration.DefInstance.ProgressBar1.Value = 0
'F_Migration.DefInstance.ProgressBar1.Min = 0
'F_Migration.DefInstance.ProgressBar1.Max = (TotalFileSize * 10000)
'F_Migration.DefInstance.ProgressBar1.CtlRefresh()

CopyProgressCallback = PROGRESS_CONTINUE

Case CALLBACK_CHUNK_FINISHED

'called when a block has been copied
'F_Migration.DefInstance.ProgressBar1.Value =
(TotalBytesTransferred * 10000)

'optional. While the app is copying it
'will not respond to input for canceling.
System.Windows.Forms.Application.DoEvents()

CopyProgressCallback = PROGRESS_CONTINUE

End Select
CopyProgressCallback = PROGRESS_CONTINUE

End Function
End Module

Thanks

Paul
"Tom Shelton" wrote:
In article <1A**********************************@microsoft.co m>, Paul wrote:
I have been trying to code a callback in vb.net using the delegate method. I have successfully compiled the program and run it but after it accessess the copyfileex winapi method and then calls the callback method there are
problems. For one the data that is populated in the parameters is wrong. I am expecting a value of 0 , 1 or maybe 2 for the dwcallbackreason and I get some outrageous number like 10788264 which incidentally is the same each time I run it, even on different machines. If that were not enough the callback method once it completes and returns, returns back into itself. At that point it goes through one more time and then dies. Now I thought there might be a bug or something since all the code I had referenced on the subject is close to mine and I have seen other comments regarding not being able to get this to work so I re-coded it in C# just to check.
In C# the code behaves slightly better in that the numbers coming in for the dwcallback reason are what I expect, a 0 for the dwcallback reason but the method still calls itself again. Any help would be greatly appreciated. I will post code for a reference if needed.

This all worked fine in vb6.


Post the code....

--
Tom Shelton [MVP]

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Moore | last post by:
I'm trying to check a graph (represented in the usual Python adjacency list format) for loops. This is dead simple - you use a depth-first search, and look out for "back edges" (edges from a vertex...
1
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
2
by: H Glenn Hatfield | last post by:
I know this has come up a couple of times on the group, but I'm still having trouble with it. I'm fine with creating the delegate and CopyFileEx declarations and the function which gets...
2
by: hzgt9b | last post by:
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...
7
by: kelly | last post by:
Hi I have an app where I have 2 dropdown lists (state and city) and a Save button. When a user selects a value from the state dropdown list, I use script callback to populate the data for the...
2
by: =?Utf-8?B?dGFpbG9jaGlr?= | last post by:
I have a VB6 project that call CopyFileEx with ProgressRoutine to display the progress. The code is like this 'Copy the file to the destination folder lpCallBack = AddressOf CopyProgressCallback...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.