title: How to hook WinInet properly with multiple threads?
body: <p>I've written a BHO for Internet Explorer that hooks WinInet to modify some HTTP requests from IE by redirecting them to an internal server.</p>
  
  <p>It works fine when I open the first 3 tabs in IE as a new iexplore.exe process is created for each tab, each loading the BHO. </p>
  
  <p>Troubles appear when there are more than 4 tabs: instead of creating a new iexplore.exe process, existing processes are re-used in different thread. I can either hook the same process multiple times (1 per thread), or just once. In both cases, crashes happen when tabs are closed.</p>
  
  <p>Example: open tabs 1 (separate iepxlore.exe), 2 (separate iepxlore.exe), 3 (separate iepxlore.exe) and 4 (share iexplore with tab 1). Close tab 1. Refresh tab 4, IE crashes inside NDTDLL.DLL. The crash happen if i I don't do anything inside the hooked WinInet.ddl (just call the old function)</p>
  
  <p>If I hook WinInet only once per iexplore.exe process, I cannot intercept requests in all tabs.  </p>
  
  <p>I'm wondering what is the best way to proceed. All examples I've found so far assume that there is 1 iexpolore.exe process per tab.</p>
  
  <p>The WinInet hook code is based on a <a href="http://www.codeproject.com/Articles/38616/Retrieve-HttpOnly-Session-Cookie-in-WebBrowser" rel="nofollow">Code Project example</a>, I just hook fewer functions.</p>
  
  <p>It looks like a pointer to he old WinInet function, or to the one I created, is no longer valid.</p>