Re: Sockets in MFC Extension DLL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mark Robinson (mark_at_simsol.co.uk)
Date: 05/19/04


Date: Wed, 19 May 2004 16:44:44 +0100


"Helge Opgård" wrote:
>
> I'm trying to use a software licensing tool (FlexLM) from within an MFC
> Extension DLL. It works for local license files but not with a license
> server. I suspect that this is because the license server must be
> contacted by using socket communications.
>
> I'm trying to check the license server from DllMain (dwReason ==
> DLL_PROCESS_ATTACH) and it fails.
> Moved the same code into an MFC console application and it works there.

>From the documentation for DllMain (Note that calling sockets functions
during an attach is strictly forbidden):

--------------------------------------------------

Warning On attach, the body of your DLL entry-point function should
perform only simple initialization tasks, such as setting up thread
local storage (TLS), creating objects, and opening files. You must not
call LoadLibrary in the entry-point function, because you may create
dependency loops in the DLL load order. This can result in a DLL being
used before the system has executed its initialization code. Similarly,
you must not call the FreeLibrary function in the entry-point function
on detach, because this can result in a DLL being used after the system
has executed its termination code.

Calling Win32 functions other than TLS, object-creation, and file
functions may result in problems that are difficult to diagnose. For
example, calling User, Shell, COM, RPC, and Windows Sockets functions
(or any functions that call these functions) can cause access violation
errors, because their DLLs call LoadLibrary to load other system
components. While it is acceptable to create synchronization objects in
DllMain, you should not perform synchronization in DllMain (or a
function called by DllMain) because all calls to DllMain are serialized.
Waiting on synchronization objects in DllMain can cause a deadlock.

To provide more complex initialization, create an initialization routine
for the DLL. You can require applications to call the initialization
routine before calling any other routines in the DLL. Otherwise, you can
have the initialization routine create a named mutex, and have each
routine in the DLL call the initialization routine if the mutex does not
exist.

--------------------------------------------------

Cheers

mark-r

-- 
A banana flies like fruit
Arrow flies like thyme


Relevant Pages

  • Re: P-Invoke + IJW Failure
    ... of our early initialization code, native and managed, and we will be looking ... dependency loops in the DLL load order. ... routine before calling any other routines in the DLL. ... a slightly modified DLL load order or other factors may ...
    (microsoft.public.dotnet.framework.interop)
  • Re: error R6031
    ... When called _CRT_INIT on initialization, in crtdll.c at line: ... The fact that you have what appears to be a statically-linked CRT in a DLL (I'd suggest ... debugger was awakened by an interrupt. ...
    (microsoft.public.vc.mfc)
  • Re: VS2008 destroys static objects before global (non-static) objects?
    ... destroyed before globals. ... While the class definitions are in one DLL o is defined in another DLL ... DLLs are loaded p_ in 2.dll is initialized first followed by an initialization of o and again p_ in 1.dll. ... As the destructor of p_ is then called first which frees all memory managed by the memory manager I get an access violation when o is destroyed. ...
    (microsoft.public.vc.language)
  • Re: MapiInitialize/MAPIUninitialize/Multithreading
    ... >> DllMain, you should not perform synchronization in DllMain (or a ... >> To provide more complex initialization, create an initialization routine ... >> routine before calling any other routines in the DLL. ...
    (microsoft.public.win32.programmer.messaging)
  • Re: error R6031
    ... I tried to debug _DllMainCRTStartup and __DllMainCRTStartup. ... When called _CRT_INIT on initialization, in crtdll.c at line: ... The fact that you have what appears to be a statically-linked CRT in a DLL (I'd suggest ... It is amazing that on application exit system calls DllMainCRTStartup. ...
    (microsoft.public.vc.mfc)