www.digitalmars.com         C & C++   DMDScript  

c++.rtl - Invalid STD-handles on DLL_PROCESS_DETACH

Hello,

Consider the code as follows.

MyDLL.c
===
#include <windows.h>

/* This is DLL entry point, no CRT used */
BOOL _stdcall MyDllMain(HINSTANCE hInst, DWORD dwReason,
                                           void * pReserved)
{
  HANDLE hCon;
  DWORD cw;

  hCon = GetStdHandle(STD_OUTPUT_HANDLE);  /* STD_ERROR_ as well */
  if (dwReason==DLL_PROCESS_ATTACH)
    WriteFile(hCon, "DLL Process Attach\r\n", 20, &cw, NULL);
  else
    WriteFile(hCon, "DLL Process Detach\r\n", 20, &cw, NULL);
  
  return(TRUE);
}
 
/* This one is exported */
int _stdcall func1(int Arg)
{
  return(Arg+1);
}

===

Here is the executable to call MyDLL from:

MyEXE.c
===
#include <stdio.h>
#include <string.h>

int _stdcall func1(int Arg);

void _cdecl main(void)
{
  printf("EXE Call func1(0)=%d\n", func1(0));
}
===

Expected console output:

===
DLL Process Attach
EXE Call func1(0)=1
DLL Process Detach
===

However, when MyEXE is compiled with DMC there is only two
first lines, but no the third. The reason is that in the case 
of DLL_PROCESS_DETACH WriteFile fails with ERROR_INVALID_HANDLE.
I suppose DM' CRT code is responsible for that. Also, the 
feature does not happen when MyEXE is linked with SND.

I'm not sure is it legal to write to the console from
inside the DllMain, but other compilers (tested with
MSVC, Borland, PelleC, and LADSoft) show it as expected.
Aug 06 2008