win32: initialize Windows Sockets in NSObject only

And update to Windows Socket version 2.2. Also clean up the DllMain initialization to handle CRT init errors and call _CRT_INIT also on DLL/thread detach, and remove uneeded .idata section after removing WSAStartup() import.

WSAStartup() should not be called from DllMain according to MS documentation. Fixes #186.
This commit is contained in:
Frederik Seiffert 2021-08-10 17:53:37 +02:00 committed by Frederik Seiffert
parent b2f2a3a429
commit f67c99dbcc
5 changed files with 26 additions and 82 deletions

View file

@ -1,3 +1,12 @@
2021-08-10 Frederik Seiffert <frederik@algoriddim.com>
* Source/NSObject:
* Source/NSProcessInfo.m:
* Source/NSSocketPort.m:
* Source:libgnustep-base-entry.m:
Initialize Windows Sockets in NSObject only and update to Windows
Socket version 2.2.
2021-08-03 Frederik Seiffert <frederik@algoriddim.com>
* Headers/GNUstepBase/config.h.in:

View file

@ -979,11 +979,14 @@ static id gs_weak_load(id obj)
if (self == [NSObject class])
{
#ifdef _WIN32
{
// See libgnustep-base-entry.m
extern void gnustep_base_socket_init(void);
gnustep_base_socket_init();
}
/* Start of sockets so we can get host name and other info */
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
int wsaResult = WSAStartup(wVersionRequested, &wsaData);
if (wsaResult != 0)
{
fprintf(stderr, "Error %d initializing Windows Sockets\n", wsaResult);
}
#else /* _WIN32 */
#ifdef SIGPIPE

View file

@ -987,21 +987,6 @@ int main(int argc, char *argv[], char *env[])
sizeof(_NSConstantStringClassReference));
#endif
#if defined(_WIN32)
WSADATA lpWSAData;
// Initialize Windows Sockets
if (WSAStartup(MAKEWORD(1,1), &lpWSAData))
{
printf("Could not startup Windows Sockets\n");
exit(1);
}
#endif /* _WIN32 */
#ifdef __MS_WIN__
_MB_init_runtime();
#endif /* __MS_WIN__ */
_gnu_process_args(argc, argv, env);
/* Call the user defined main function */

View file

@ -541,13 +541,6 @@ static Class runLoopClass;
{
tlsLock = [NSLock new];
[[NSObject leakAt: &tlsLock] release];
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);
WSAStartup(wVersionRequested, &wsaData);
#endif
mutableArrayClass = [NSMutableArray class];
mutableDataClass = [NSMutableData class];
portMessageClass = [NSPortMessage class];

View file

@ -30,31 +30,10 @@
/* Only if using Microsoft's tools and libraries */
#ifdef __MS_WIN32__
#include <stdio.h>
WINBOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved);
// Global errno isn't defined in Microsoft's thread safe C library
void errno()
{}
int _MB_init_runtime()
{
return 0;
}
#endif /* __MS_WIN32__ */
void
gnustep_base_socket_init()
{
/* Start of sockets so we can get host name and other info */
static WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,0), &wsaData))
{
NSLog(@"Error: Could not startup Windows Sockets.\n");
}
}
//
// DLL entry function for GNUstep Base Library
// This function gets called everytime a process/thread attaches to DLL
@ -65,46 +44,21 @@ DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
#ifdef __MS_WIN32__
/* Initialize the Microsoft C stdio DLL */
_CRT_INIT(hInst, ul_reason_for_call, lpReserved);
#endif /* __MS_WIN32__ */
// Initialize Windows Sockets
gnustep_base_socket_init();
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
#ifdef __MS_WIN32__
/* Initialize C stdio DLL */
_CRT_INIT(hInst, ul_reason_for_call, lpReserved);
#endif /* __MS_WIN32__ */
break;
}
case DLL_THREAD_DETACH:
{
break;
#ifdef __MS_WIN32__
// CRT_INIT must be called on DLL/thread attach and detach, although
// first on attach and last on detach. Since we don't do anything else
// in this method we can just always call it.
if (!_CRT_INIT(hInst, ul_reason_for_call, lpReserved)) {
return FALSE;
}
#endif /* __MS_WIN32__ */
}
break;
}
return TRUE;
}
#ifndef __clang__
/*
This section terminates the list of imports under GCC. If you do not
include this then you will have problems when linking with DLLs.
*/
asm (".section .idata$3\n" ".long 0,0,0,0,0,0,0,0");
#endif