kacsab
asked on
How can I use IBindStatusCallBack interface in VB.NET?
Hi!
I wrote a program which download a specified file from the Internet to a local file.
I used the URLDownloadToFile function (urlmon.dll). I would like to show a progressbar while the file is downloading. I have found Edanmo's type library (olelib.tlb) which contains the IBindStatusCallBack interface and made a class which implements it.
The last parameter of URLDownloadToFile is an integer but according to the description of URLDownLoadToFile it is a pointer referred to the caller's IBindStatusCallBack interface.
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReversed As Integer, _
ByVal lpfnCB As Integer) As Integer
How can I pass this parameter to the URLDownLoad function? Is it possible to solve this problem in VB.NET at all?
If there is a solution in C#, it would be helpful for me.
Thanks
I wrote a program which download a specified file from the Internet to a local file.
I used the URLDownloadToFile function (urlmon.dll). I would like to show a progressbar while the file is downloading. I have found Edanmo's type library (olelib.tlb) which contains the IBindStatusCallBack interface and made a class which implements it.
The last parameter of URLDownloadToFile is an integer but according to the description of URLDownLoadToFile it is a pointer referred to the caller's IBindStatusCallBack interface.
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReversed As Integer, _
ByVal lpfnCB As Integer) As Integer
How can I pass this parameter to the URLDownLoad function? Is it possible to solve this problem in VB.NET at all?
If there is a solution in C#, it would be helpful for me.
Thanks
ASKER
Thank you for your reply, Bob!
As you suggested I used delegate function, but my program doesn't work. A System.NullReferenceExcept ion occurs.
Could you tell me where is the error?
Here is the code:
Public Class clsBSC
Implements olelib.IBindStatusCallback
...
Public Sub OnProgress(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer) Implements olelib.IBindStatusCallback .OnProgres s
RaiseEvent ProgressEvent(ulProgress, ulProgressMax, ulStatusCode, szStatusText)
End Sub
Public Event ProgressEvent(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer)
Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReversed As Integer, _
ByVal lpfnCB As OnProgressDelegate) As Integer
Public Delegate Sub OnProgressDelegate(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer)
End Class
I have a form with a button, a progressbar and two textboxes on it:
Public Class Form1
Inherits System.Windows.Forms.Form
...
Private WithEvents BSC As clsBSC
Private mOnProgressDelegate As clsBSC.OnProgressDelegate
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BSC = New clsBSC
mOnProgressDelegate = New clsBSC.OnProgressDelegate( AddressOf BSC.OnProgress)
BSC.URLDownloadToFile(0, TextBox1.Text, TextBox2.Text, 0, mOnProgressDelegate) '<====The exception occurs at this row.
End Sub
Private Sub BSC_ProgressEvent(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer) Handles BSC.ProgressEvent
ProgressBar1.Maximum = ulProgressMax
ProgressBar1.Value = ulProgress
End Sub
End Class
Thank you in anticipation
As you suggested I used delegate function, but my program doesn't work. A System.NullReferenceExcept
Could you tell me where is the error?
Here is the code:
Public Class clsBSC
Implements olelib.IBindStatusCallback
...
Public Sub OnProgress(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer) Implements olelib.IBindStatusCallback
RaiseEvent ProgressEvent(ulProgress, ulProgressMax, ulStatusCode, szStatusText)
End Sub
Public Event ProgressEvent(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer)
Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReversed As Integer, _
ByVal lpfnCB As OnProgressDelegate) As Integer
Public Delegate Sub OnProgressDelegate(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer)
End Class
I have a form with a button, a progressbar and two textboxes on it:
Public Class Form1
Inherits System.Windows.Forms.Form
...
Private WithEvents BSC As clsBSC
Private mOnProgressDelegate As clsBSC.OnProgressDelegate
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BSC = New clsBSC
mOnProgressDelegate = New clsBSC.OnProgressDelegate(
BSC.URLDownloadToFile(0, TextBox1.Text, TextBox2.Text, 0, mOnProgressDelegate) '<====The exception occurs at this row.
End Sub
Private Sub BSC_ProgressEvent(ByVal ulProgress As Integer, _
ByVal ulProgressMax As Integer, _
ByVal ulStatusCode As olelib.BINDSTATUS, _
ByVal szStatusText As Integer) Handles BSC.ProgressEvent
ProgressBar1.Maximum = ulProgressMax
ProgressBar1.Value = ulProgress
End Sub
End Class
Thank you in anticipation
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public Delegate Sub StatusCallBackDelegate(<si
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReversed As Integer, _
ByVal lpfnCB As StatusCallBackDelegate) As Integer
Bob