Link to home
Start Free TrialLog in
Avatar of kacsab
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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Do you know what signature is for the callback function from the IBindStatusCallBack interface?  You need to define a delegate function and pass the address of the delegate:

Public Delegate Sub StatusCallBackDelegate(<signature goes here>)

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
Avatar of kacsab
kacsab

ASKER

Thank you for your reply, Bob!

As you suggested I used delegate function, but my program doesn't work. A System.NullReferenceException 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.OnProgress

              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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial