TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Jonathan de Boyne Pollard
from: Mario Semo
date: 1997-01-20 08:42:16
subject: writing REXX DLL`s

Hallo Jonathan!

Antwort auf eine Message von Jonathan de Boyne Pollard an Darin McBride:

 DM>> 
 >> BTW, is there a user-function that is called on init (and
 >> term)?  I can't seem to find a reference to it... :-/
 DM>> 

 JdBP> static HMODULE handle = 0 ;         //  Module handle of the DLL

 JdBP> #if defined(__IBMCPP__)

 JdBP> extern "C" int _CRT_init () ;
 JdBP> extern "C" void __ctordtorInit () ;
 JdBP> extern "C" void __ctordtorTerm () ;
 JdBP> extern "C" int _CRT_term () ;

here's a long full version of dll-initterm for vacpp for OS/2 and Windows from IBM:

#: 7239 S1/Compiler / Linker
    13-Jan-97  21:05:26
Sb: #6887-#Static Classes and DLL's
Fm: The TEAMIBM Network 72370,250

I agree that the documentation about writing your own init/term functions is
pretty lean.  I've put together a better description of how to write your own,
which I'll be putting up on the web once I add support to work around the OS/2
design flaw that causes DLLs to get unloaded in what is essentially a random
order (Mario and Roger had a thread about this back in November).  This code
is essentially the code we use in the C runtime.

  /* Skeleton _DLL_InitTerm() function */

  #pragma strings(readonly)

  #include 
  #include 
  #if __WINDOWS__
    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    #include 
  #endif

  /*
   * --- Prototypes ---
   */

  int  _IMPORT _Optlink _CRT_init(void);
  void _IMPORT _Optlink _CRT_term(void);
  void         _Optlink __ctordtorInit(unsigned);
  void         _Optlink __ctordtorTerm(unsigned);
  int  _IMPORT _Optlink _abort_in_progress(void);
  void         _Optlink _exception_dllinit(void *);
  void         _Optlink _fpninit(void);
  int          _Optlink _rmem_init(void);
  int          _Optlink _rmem_term(void);
  #if __OS2__
  unsigned long _System DllMain( unsigned long ulModHandle,
                                     unsigned long ulFlag );
  unsigned long _System wDllMain( unsigned long ulModHandle,
                                     unsigned long ulFlag );
  #elif __WINDOWS__
  BOOL         WINAPI   DllMain( HINSTANCE ulModHandle,
                               DWORD ulFlag, LPVOID dummy );
  BOOL         WINAPI   wDllMain( HINSTANCE ulModHandle,
                                DWORD ulFlag, LPVOID dummy );
  int          _Optlink _TlsWin32s(unsigned long);
  #endif

  /*
   * --- Weak externs ---
   * These directives cause the linker to resolve references to these
   * functions as empty stubs, unless the real versions of these functions
   * are referenced elsewhere
   */

  #pragma weak(DllMain, wDllMain)
  #pragma weak(__ctordtorInit, _crtweak)
  #pragma weak(__ctordtorTerm, _crtweak)
  #if __WINDOWS__
    #pragma weak(_TlsWin32s, _crtweak1)
  #endif

  /*
   * --- _DLL_InitTerm ---
   * This is the function that is called when the DLL is loaded and
   * unloaded.
   *
   * Parameters:
   *   ulModHandle  Module handle for the DLL
   *   ulFlag       Flag indicating whether DLL is being loaded or unloaded
   *   dummy        Not used
   * Returns:
   *   1 or 0       for success or failure
   */

  #if __OS2__
  unsigned long _System _DLL_InitTerm( unsigned long ulModHandle,
                                     unsigned long ulFlag )
  #elif __WINDOWS__
  BOOL WINAPI _DLL_InitTerm( HINSTANCE ulModHandle,
                                    DWORD ulFlag, LPVOID dummy )
  #endif

  {
    /*
     * DLL is being unloaded.
     */

   #if __WINDOWS__
    if (ulFlag == DLL_PROCESS_DETACH) {
   #else
    if (ulFlag) {
   #endif

      /*
       * -----------------------------------------------------------------
       * User code goes here
       * -----------------------------------------------------------------
       */

      /*
       * If we're terminating abnormally, don't be polite about cleaning
       * up.
       */
      if (!_abort_in_progress()) {
        /*
         * Notify user code that the DLL is being unloaded.  If there is
         * no user-supplied DllMain(), a null stub in the runtime library
         * will be called.
         */
       #if __OS2__
        DllMain( ulModHandle, ulFlag );
       #elif __WINDOWS__
        DllMain( ulModHandle, ulFlag, dummy );
       #endif

        /*
         * Run static destructors
         */
        __ctordtorTerm(0);

        /*
         * Terminiate the runtime environment.  If the no-environment
         * version of the runtime is being used (ie, we're compiling with
         * -Rn), only the memory-management code needs to be terminated.
         */
       #if __SPC__
        _rmem_term();
       #else
        _CRT_term( );
       #endif

        /*
         * Terminate thread-local storage manager.
         */
       #if __WINDOWS__
        _TlsWin32s( DLL_PROCESS_DETACH );
       #endif
      }
    }

    /*
     * DLL is being loaded.
     */

   #if __WINDOWS__
    else if (ulFlag == DLL_PROCESS_ATTACH) {
   #else
    else {
   #endif
      /*
       * Initialise thread-local storage manager.
       */
     #if __WINDOWS__
      if (_TlsWin32s( DLL_PROCESS_ATTACH ) == 0)
        return 0;
     #endif

      /*
       * Initialise floating-point flags.  This does not need to be done
       * if the runtime library is not statically bound to this DLL.
       */
     #if __SPC__
      _fpninit();
     #elif !__IMPORTLIB__
      _fpreset();
     #endif

      /*
       * Initialise the runtime environment.  If the no-environment
       * version of the runtime is being used (ie, we're compiling with
       * -Rn), only the memory-management code needs to be initialised.
       */
     #if __SPC__
      _rmem_init();
     #else
      if (_CRT_init() == -1)
        return 0;
     #endif

      /*
       * Set up the default math error exception handler.
       */
     #if !__SPC__
      _exception_dllinit( (void *)_matherr );
     #endif

      /*
       * Run static contructors
       */
      __ctordtorInit(0);

      /*
       * Notify user code that the DLL is being loaded.  If there is
       * no user-supplied DllMain(), a null stub in the runtime library
       * will be called.
       */
     #if __OS2__
      if (!DllMain( ulModHandle, ulFlag ))
     #elif __WINDOWS__
      if (!DllMain( ulModHandle, ulFlag, dummy ))
     #endif
        return 0;

      /*
       * -----------------------------------------------------------------
       * User code goes here
       * -----------------------------------------------------------------
       */

    }

    /*
     * Return indicating success.
     */
    return 1;
  }

dave

MessageID: 19970113192708.VACPP1

Servus, Mario!

--- FleetStreet 1.18 PR#2
* Origin: LC/32 Development Team, KirchnerSoft, Vienna, Austria (2:310/14.11)
SEEN-BY: 50/99 54/99 270/101 620/243 625/110 160 711/401 413 430 808 934
SEEN-BY: 712/311 407 505 506 517 623 624 704 713/317 800/1
@PATH: 310/14 1 24/999 888 396/1 270/101 712/624 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.