Novell Home

How to intercept EVENT MODULE LOAD (NetWare)

From Developer Community

This code demonstrates how to use RegisterForEvent() to install a handler for EVENT_MODULE_LOAD event.

Sample Code

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

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

static void sigHandler( int sig_no) {
#ifdef __MWERKS__
#pragma unused( sig_no)
#endif
   int saved_thread_group_ID = SetThreadGroupID( mainThreadGroupID);
   signal( SIGTERM, sigHandler);
   exitRequested = !0;
   ResumeThread( mainThreadID);
   while ( threadCount) delay( 100);
   SetThreadGroupID( saved_thread_group_ID);
}
static void eventProc( unsigned long param, unsigned long user_param) {
#ifdef __MWERKS__
#pragma unused( user_param)
#endif
   int  saved_thread_group_ID;
   char NLMFileName[ 128];
   char NLMDescription[ 128];
   int ccode;
   ++threadCount;
   saved_thread_group_ID = SetThreadGroupID( mainThreadGroupID);
   printf( "NLM Handle = %x\n", param);
   if (( ccode = GetNLMNameFromNLMHandle(( int) param, NLMFileName, NLMDescription)) == ESUCCESS) {
      printf( "  NLMFileName = %s, NLMDescription = %s\n", NLMFileName, NLMDescription);
   }
   else {
      printf( "  GetNLMNameFromNLMHandle() failed, ccode = %d\n", ccode);
   }
   SetThreadGroupID( saved_thread_group_ID);
   --threadCount;
}
int main() {
   unsigned long event_handle;
   ++threadCount;
   mainThreadID      = GetThreadID();
   mainThreadGroupID = GetThreadGroupID();
   signal( SIGTERM, sigHandler);
   event_handle = RegisterForEvent( EVENT_MODULE_LOAD, eventProc, NULL);
   if ( event_handle == EFAILURE) {
      signal( SIGTERM, SIG_DFL);
      puts( "RegisterForEvent failed");
      return EXIT_FAILURE;
   }
   puts( "Waiting...");
   SuspendThread( mainThreadID);
   UnregisterForEvent( event_handle);
   --threadCount;
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.