Will any one check the module.

If you remove the WinHttpSetStatusCallback line and run the module you will find Ret_strWinHttpResponseText is filled with the response text from the target web URL.

I want to  make the module work Asynchronous - and Ret_strWinHttpResponseText must be filled with the text from the web URL.

I have written the code,please if someone will check it by debugging it & were it is going wrong.

Also sometimes it is crashing at Winhttpreceiveresponse & goes to else part.

Please help.
Module1.bas
RuuuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArkCommented:
As Any ALWAYS marshalling ByRef, even if you use ByVal in your declaration. You mast specify ByVal or ByRef in function call instead or in your case just use ByVal Long for function pointer

Public Declare Function WinHttpSetStatusCallback Lib "winhttp.dll" (ByVal hInternet As Long, ByVal lpfnInternetCallback As Any, ByVal dwNotificationFlags As Long, ByVal dwReserved As Long) As Long

Should be either

Public Declare Function WinHttpSetStatusCallback Lib "winhttp.dll" (ByVal hInternet As Long, ByVal lpfnInternetCallback As Long, ByVal dwNotificationFlags As Long, ByVal dwReserved As Long) As Long

or

Public Declare Function WinHttpSetStatusCallback Lib "winhttp.dll" (ByVal hInternet As Long, ByRef lpfnInternetCallback As Any, ByVal dwNotificationFlags As Long, ByVal dwReserved As Long) As Long

In second case

pcallback = WinHttpSetStatusCallback(hSession, ByVal AddressOf Asynccallback, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS, 0)
0
RuuuAuthor Commented:
Ok also whatever code I have written is correct?will it work asynchronous
0
RuuuAuthor Commented:
In second case you mentioned  I follow yours but its giving syntax error
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ArkCommented:
Sorry, it's known behavior of VB
pcallback=....ByVal getLP(AddressOf Asynccallback)..
Private Function getLP(Byval lp As Long) As Long
   getLP=lp
End Function
Also, check:
Public Function Asynccallback(ByVal hInternet As Long, ByVal dwContext As Long, ByVal dwInternetStatus As Long, ByVal lpvStatusInformation As Long, dwStatusInformationLength As Long)
    Dim cpContext As AsyncWinhttp
    Dim c As Long
   
    If dwContext = 0 Then
        Exit Function 'Return - classic VB doesn't support Return. Or u'r working with Net?
    End If
0
RuuuAuthor Commented:
Will the module work asynchronously..?
0
ArkCommented:
Actually there are a lot of incorrect type conversions from C to VB. Do you really need API to perform async GET? Why not use UserControl.AsyncRead instead? Sample attached (just rename UserControl1.txt to UserControl1.ctl - for some reason EE doesn't allow *.ctl files for upload)
AsyncRead.zip
0
RuuuAuthor Commented:
I want to call asynccallback.
0
ArkCommented:
For what?
All your code in module just check callback status. Checkout my sample - you'll get same results (plus more) in statusCode field in DownloadProgress event
0
RuuuAuthor Commented:
I want the function in the module asynccallback should be called when winhttpsendrequest is called and the project doesn't lock.
0
ArkCommented:
Did you try my sample????
0
RuuuAuthor Commented:
Am checking but I want it in the same way as in c
0
ArkCommented:
I want it in the same way as in c
Why in this case not create complete CPP App? Or create MFC control and use it in your VB app?
UserControl.AsyncRead performs SAME code under the hood. It creates 'hidden' callback and raise native VB events. You don't need any 3rd party controls or even additional VB references - just add usercontrol to your project and compile it.
0
RuuuAuthor Commented:
I have a service which is developed in vb6 so thats I wantthe code in vb.will your sample will work in the same way aasi want?
0
RuuuAuthor Commented:
From where to add user control?
0
RuuuAuthor Commented:
Its giving error at downloadstart in cmd action button.Method or data member not found
0
RuuuAuthor Commented:
Please refer the attached error file.I am facing in the sample provided by you
Error.docx
0
ArkCommented:
Method or data member not found
Did you rename UserControl.txt to UserControl.ctl?
OK. Since you'r speaking about service - you don't use GUI
Start VB6.
Form1 will created by default.
Go to Menu->Project->Add User Control
UserControl1 will be created
Right click on usercontrol and select "View Code"
Copy/Paste following code
Option Explicit

Private m_Key As Long
Private m_Bytes() As Byte
Private m_nBytes As Long, m_Duration As Long

Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub DownloadStart(ByVal URL As String, Optional ByVal Mode As AsyncReadConstants = vbAsyncReadResynchronize)
   If Len(URL) Then
      If m_Key Then Me.DownloadCancel
      m_Key = GetTickCount()
      On Error Resume Next
      UserControl.AsyncRead URL, vbAsyncTypeByteArray, CStr(m_Key), Mode
      If Err.Number Then
         Debug.Print "AsyncRead Error"; Err.Number, Err.Description
      End If
   End If
End Sub

Public Sub DownloadCancel()
   On Error Resume Next
   UserControl.CancelAsyncRead CStr(m_Key)
   Debug.Print CStr(m_Key); " - cancel"
   If Err.Number Then
      Debug.Print "CancelAsyncRead Error"; Err.Number, Err.Description
   End If
End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
   m_Duration = Abs(GetTickCount - m_Key)
   Debug.Print CStr(m_Key); " - done"
   m_Key = 0
   With AsyncProp
      On Error GoTo BadDownload
      If .AsyncType = vbAsyncTypeByteArray Then
         m_Bytes = .Value
         m_nBytes = UBound(m_Bytes) + 1
         Call DownloadComplete(m_nBytes)
      End If
   End With
   Exit Sub
BadDownload:
   m_nBytes = 0
   Call DownloadFailed(Err.Number, Err.Description)
End Sub

Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
   With AsyncProp
      On Error GoTo BadProgress
      If .AsyncType = vbAsyncTypeByteArray Then
         Call DownloadProgress(.BytesRead, .BytesMax, .Target, .Status, .StatusCode)
         DoEvents
      End If
   End With
   Exit Sub
BadProgress:
   
End Sub
Public Property Get Bytes() As Byte()
   Bytes = m_Bytes()
End Property

Public Function StatusCodeName(code As AsyncStatusCodeConstants) As String
   Dim sCode As String
   Select Case code
        Case vbAsyncStatusCodeBeginDownloadData
            sCode = "BeginDownloadData"
        Case vbAsyncStatusCodeBeginSyncOperation
            sCode = "BeginSyncOperation"
        Case vbAsyncStatusCodeCacheFileNameAvailable
            sCode = "CacheFileNameAvailable"
        Case vbAsyncStatusCodeConnecting
            sCode = "Connecting"
        Case vbAsyncStatusCodeDownloadingData
            sCode = "DownloadingData"
        Case vbAsyncStatusCodeEndDownloadData
            sCode = "EndDownloadData"
        Case vbAsyncStatusCodeEndSyncOperation
            sCode = "EndSyncOperation"
        Case vbAsyncStatusCodeError
            sCode = "CodeError"
        Case vbAsyncStatusCodeFindingResource
            sCode = "FindingResource"
        Case vbAsyncStatusCodeMIMETypeAvailable
            sCode = "MIMETypeAvailable"
        Case vbAsyncStatusCodeRedirecting
            sCode = "Redirecting"
        Case vbAsyncStatusCodeSendingRequest
            sCode = "SendingRequest"
        Case vbAsyncStatusCodeUsingCachedCopy
            sCode = "UsingCachedCopy"
        Case Else
            sCode = "Unknown"
   End Select
   StatusCodeName = sCode
End Function

Open in new window

Close UserControl
See UserControl icon appeared in ToolBox.
Select this icon and draw control on Form1.
Go to Menu->Project->Add Module
Copy/Paste following code in a module
Option Explicit

Dim downloader As Object

Public Sub main()
   DownloadBegin "http://www.microsoft.com"
End Sub
Public Sub DownloadComplete(ByVal nBytes As Long)
   Debug.Print StrConv(downloader.Bytes, vbUnicode)
End Sub

Public Sub DownloadProgress(ByVal bytesCurrent As Long, ByVal bytesTotal As Long, _
                              ByVal target As String, ByVal status As String, _
                              ByVal statusCode As AsyncStatusCodeConstants)
   Debug.Print downloader.StatusCodeName(statusCode)
End Sub

Public Sub DownloadFailed(ByVal errNum As Long, ByVal errDesc As String)

End Sub

Public Sub DownloadBegin(ByVal URL As String, Optional ByVal Mode As AsyncReadConstants = vbAsyncReadResynchronize)
   If downloader Is Nothing Then
      Set downloader = Form1.UserControl11
   End If
   downloader.DownloadStart URL, Mode
End Sub

Open in new window

Go to Menu->Project->Project Options and select sub main as startup object
Press F5
See output in debug.window
0
ArkCommented:
Please refer the attached error file.I am facing in the sample provided by you
No need declaring uc Dim uc as UserControl1
Just move it from toolbox on your form
0
RuuuAuthor Commented:
Its giving error Set downloader = Form1.UserControl11.method or data member not found
0
RuuuAuthor Commented:
sorry sorry its working.Great
0
RuuuAuthor Commented:
Its getting locked when there is internet connection problem
0
ArkCommented:
No. It executing asyncroneously.
Just add in a module
   downloader.DownloadStart URL, Mode
   Debug.Print "After DownloadStart" '<===ADDED

and you'll see this text immediately after sub main starts
Since VB6 itself is single-threading this means that downloading performs in another thread.
You may want add smth to DownloadFiled sub to see error:
Public Sub DownloadFailed(ByVal errNum As Long, ByVal errDesc As String)
   Debug.Print "Failed: " & errDesc
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RuuuAuthor Commented:
Yes its working asynchronously
0
RuuuAuthor Commented:
Great thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.