Beispiel #1
0
/*
 * @implemented
 */
INT
WSAAPI
WSACleanup(VOID)
{
    PWSPROCESS Process;
    PWSTHREAD Thread;
    INT ErrorCode;
    LONG RefCount;
    DPRINT("WSACleanup\n");

    /* Enter startup lock */
    WsStartupLock();

    /* Enter prolog */
    if ((ErrorCode = WsApiProlog(&Process, &Thread)) == ERROR_SUCCESS)
    {
        /* Decrement process reference count and check if it's zero */
        if (!(RefCount = InterlockedDecrement(&Process->RefCount)))
        {
            /* It's zero, destroy the process structure */
            WsProcDelete(Process);
        }
        else if (RefCount == 1 && WsAsyncThreadInitialized)
        {
            /* Kill async thread */
            WsAsyncTerminateThread();
        }

        /* Return success */
        ErrorCode = ERROR_SUCCESS;
    }
    else
    {
        /* Weren't initialized */
        SetLastError(ErrorCode);
        ErrorCode = SOCKET_ERROR;
    }

    /* Release startup lock */
    WsStartupUnlock();

    /* Done */
    return ErrorCode;
}
Beispiel #2
0
BOOL
APIENTRY
DllMain(HANDLE hModule,
        DWORD dwReason,
        LPVOID lpReserved)
{
    PWSPROCESS WsProcess;

    /* Main Entrypoint */
    switch (dwReason) 
    {
        case DLL_PROCESS_ATTACH:
            /* Save DLL Handle */
            WsDllHandle = hModule;

            /* Get Global Heap */
            WsSockHeap = GetProcessHeap();

            /* TLS Allocation */
            if (GlobalTlsIndex == TLS_OUT_OF_INDEXES)
			{
				GlobalTlsIndex = TlsAlloc();
				if (GlobalTlsIndex == TLS_OUT_OF_INDEXES)
				{
					return FALSE;
				}
			}

            /* Initialize some critical sections */
            WsCreateStartupSynchronization();
            WsAsyncGlobalInitialize();
            WsRasInitializeAutodial();
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:
            /* Destroy the attached Winsock Thread */
            WsThreadDestroyCurrentThread();
            break;

        case DLL_PROCESS_DETACH:
            /* Make sure we were initialized */
            if (!WsDllHandle) break;

            /* Check if this was a FreeLibrary call (ie: not process cleanup) */
            if (lpReserved)
            {
                /* Destroy the thread which is exiting */
                WsThreadDestroyCurrentThread();

                /* Check if we have a process and destroy it */
                WsProcess = WsGetProcess();
                if (WsProcess) WsProcDelete(WsProcess);

                /* Cleanup the Thread and Socket managers */
                WsThreadCleanup();
                WsSockCleanup();

                /* Cleanup critical sections */
                WsDestroyStartupSynchronization();
                WsAsyncGlobalTerminate();

                /* Free the TLS Index */
                TlsFree(GlobalTlsIndex);
            }

            /* Cleanup RAS auto-dial helper */
            WsRasUninitializeAutodial();

            /* Clear our handle */
            WsDllHandle = NULL;
            break;
    }

    /* Return to OS */
    return TRUE;
}