[WINHTTP] Properly initialize winsock. Based on wine tests CORE-12104
authorPeter Hater <7element@mail.bg>
Mon, 10 Oct 2016 06:54:56 +0000 (06:54 +0000)
committerPeter Hater <7element@mail.bg>
Mon, 10 Oct 2016 06:54:56 +0000 (06:54 +0000)
svn path=/trunk/; revision=72951

reactos/dll/win32/winhttp/net.c
reactos/dll/win32/winhttp/session.c

index bdb7787..dcf6aee 100644 (file)
@@ -276,6 +276,28 @@ static BOOL ensure_cred_handle(void)
     return ret;
 }
 
+#ifdef __REACTOS__
+static BOOL winsock_initialized = FALSE;
+BOOL netconn_init_winsock()
+{
+    WSADATA wsaData;
+    int error;
+    if (!winsock_initialized)
+    {
+        error = WSAStartup(MAKEWORD(1, 1), &wsaData);
+        if (error)
+        {
+            ERR("WSAStartup failed: %d\n", error);
+            return FALSE;
+        }
+        else
+            winsock_initialized = TRUE;
+    }
+    return winsock_initialized;
+}
+
+#endif
+
 BOOL netconn_init( netconn_t *conn )
 {
     memset(conn, 0, sizeof(*conn));
@@ -291,6 +313,10 @@ void netconn_unload( void )
 #ifndef HAVE_GETADDRINFO
     DeleteCriticalSection(&cs_gethostbyname);
 #endif
+#ifdef __REACTOS__
+    if(winsock_initialized)
+        WSACleanup();
+#endif
 }
 
 BOOL netconn_connected( netconn_t *conn )
index 6ac5622..960619a 100644 (file)
@@ -83,9 +83,6 @@ static void session_destroy( object_header_t *hdr )
     heap_free( session->proxy_username );
     heap_free( session->proxy_password );
     heap_free( session );
-#ifdef __REACTOS__
-    WSACleanup();
-#endif
 }
 
 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
@@ -194,6 +191,9 @@ static const object_vtbl_t session_vtbl =
     session_set_option
 };
 
+#ifdef __REACTOS__
+BOOL netconn_init_winsock();
+#endif /* __REACTOS__ */
 /***********************************************************************
  *          WinHttpOpen (winhttp.@)
  */
@@ -202,9 +202,7 @@ HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWST
     session_t *session;
     HINTERNET handle = NULL;
 #ifdef __REACTOS__
-    WSADATA wsaData;
-    int error = WSAStartup(MAKEWORD(2, 2), &wsaData);
-    if (error) ERR("WSAStartup failed: %d\n", error);
+    if (!netconn_init_winsock()) return NULL;
 #endif
 
     TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);