Novell Home

How to intercept EVENT MODULE UNLOAD (NetWare)

From Developer Community

This code demonstrates how to use RegisterForEvent() to install a handler for EVENT_MODULE_UNLOAD event. You can use it, for example, to unimport dynamically imported functions when the library NLM that exports them is unloaded from the console (if you don't do this, the OS will not let you unload the library NLM).

Sample Code

#include <nwadv.h>
#include <nwerrno.h>
#include <nwthread.h>
#include <stdio.h>
#include <stdlib.h>

static unsigned int threadCount   = 0;
static          int exitRequested = 0;
static          int mainThreadID;
static          int mainThreadGroupID;
static unsigned int NLM_Handle;

static LONG warnProc( void (*OutputProc)( void const* controlStr, ...), LONG param, LONG user_param) {
#ifdef __MWERKS__
#pragma unused( user_param)
#endif
   int saved_thread_group_ID;
   ++threadCount;
   saved_thread_group_ID = SetThreadGroupID( mainThreadGroupID);
   if ( param != NLM_Handle) {
      puts( "Another NLM is being unloaded");
      SetThreadGroupID( saved_thread_group_ID);
      return 0;
   }
   OutputProc( "Unloading...\n");
   exitRequested = !0;
   ResumeThread( mainThreadID);
   SetThreadGroupID( saved_thread_group_ID);
   --threadCount;
   return !0;
}
static void eventProc( LONG param, LONG user_param) {
#ifdef __MWERKS__
#pragma unused( param, user_param)
#endif
}
int main() {
   unsigned long event_handle;
   ++threadCount;
   mainThreadID      = GetThreadID();
   mainThreadGroupID = GetThreadGroupID();
   NLM_Handle        = GetNLMHandle();
   event_handle      = RegisterForEvent( EVENT_MODULE_UNLOAD, eventProc, warnProc);
   if ( event_handle == EFAILURE) {
      puts( "RegisterForEvent failed");
      return EXIT_FAILURE;
   }
   puts( "UNLOAD me...");
   SuspendThread( mainThreadID);
   UnregisterForEvent( event_handle);
   while ( threadCount > 1) delay( 100);
   --threadCount;
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.