title: WinHttp Async Callback in vb6
body: <p>I have a vb6 service which uses Synchronous WinHttp calls to a remote server. These calls lockup the service during times when there appears to be connection issues (network/Internet etc).</p>
  
  <p>My task is to implement WinHttp Asynchronous calls.</p>
  
  <p>First step, I'm trying to figure out the conversions from C++ to vb6. Bearing in mind I've never written a line of C++ before.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa383917(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa383917(v=vs.85).aspx</a></p>
  
  <pre><code>typedef void ( CALLBACK *WINHTTP_STATUS_CALLBACK)(
  _In_  HINTERNET hInternet,
  _In_  DWORD_PTR dwContext,
  _In_  DWORD dwInternetStatus,
  _In_  LPVOID lpvStatusInformation,
  _In_  DWORD dwStatusInformationLength
  );
  </code></pre>
  
  <p>This is my attempt:</p>
  
  <pre><code>Public Declare Function WINHTTP_STATUS_CALLBACK Lib "winhttp.dll"
  (ByVal hSession As Long, dwContext As Long, dwInternetStatus As Integer,
  lpvStatusInformation As Long, dwStatusInformationLength As Integer)
  </code></pre>
  
  <p>I have no idea if that's correct and unfortunately I can't test it until I get a bit further.</p>
  
  <p>The next one is WinHttpSetStatusCallback</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa384115(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa384115(v=vs.85).aspx</a></p>
  
  <pre><code>WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback(
  _In_        HINTERNET hInternet,
  _In_        WINHTTP_STATUS_CALLBACK lpfnInternetCallback,
  _In_        DWORD dwNotificationFlags,
  _Reserved_  DWORD_PTR dwReserved
  );
  </code></pre>
  
  <p>This is my attempt:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I don't really know if my conversions are correct. Can anyone verify this?</p>
  
  <p>Next, I don't know how to implement WinHttpSetStatusCallback - for example I don't know if I have the pointer to the Callback function correct.</p>
  
  <pre><code>_In_        WINHTTP_STATUS_CALLBACK lpfnInternetCallback,
  </code></pre>
  
  <p>converted to:</p>
  
  <pre><code>ByVal lpfnInternetCallback As Long
  </code></pre>
  
  <p>I'm not sure if I have that correct either.</p>
  
  <p>I don't have a clear idea on how to define the Async Callback function itself. Here is my initial attempt:</p>
  
  <pre><code>Public Function AsyncCallback(
                          ByVal hInternet As Long,
                          dwContext As Long,
                          dwInternetStatus As Long,
                          lpvStatusInformation As Long,
                          dwStatusInformationLength As Long)
      //code in here
  End Function
  </code></pre>
  
  <p>Then finally, wiring it all up is going to be interesting. But first I have to make sure I have the foundations right, otherwise I can't expect it to magically "work".</p>
  
  <p>The MSDN C++ example is as follows: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa384115(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa384115(v=vs.85).aspx</a></p>
  
  <pre><code>// Install the status callback function.
  WINHTTP_STATUS_CALLBACK isCallback = WinHttpSetStatusCallback( hSession,
                                             (WINHTTP_STATUS_CALLBACK)AsyncCallback,
                                             WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 
                                             NULL);
  </code></pre>
  
  <p>How do I define a pointer to the AsyncCallback function in vb6?</p>