Novell Home

How to intercept EVENT NEW PUBLIC (NetWare)

From Developer Community

This code demonstrates how to use RegisterForEvent() to install a handler for EVENT_NEW_PUBLIC event. To see how it works, just load this program and then load another NLM that exports symbols. This sample will show you the name of the loaded NLM and names of symbols that it exports.

Sample Code

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

static unsigned int threadCount   = 0;
static          int 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 moduleLoadProc( unsigned long param, unsigned long user_param) {
#ifdef __MWERKS__
#pragma unused( user_param)
#endif
   int  saved_thread_group_ID;
   char NLMFileName[ 13];
   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;
}
static void newPublicProc( unsigned long param, unsigned long user_param) {
#ifdef __MWERKS__
#pragma unused( user_param)
#endif
   int  saved_thread_group_ID;
   char NewPublic[ 256];
   ++threadCount;
   saved_thread_group_ID = SetThreadGroupID( mainThreadGroupID);
   LenToASCIIZStr( NewPublic, ( char const*) param);
   printf( "New Public = %s\n", NewPublic);
   SetThreadGroupID( saved_thread_group_ID);
   --threadCount;
}
int main() {
   unsigned long module_load_event_handle;
   unsigned long new_public_event_handle;
   ++threadCount;
   mainThreadID      = GetThreadID();
   mainThreadGroupID = GetThreadGroupID();
   signal( SIGTERM, sigHandler);
   module_load_event_handle = RegisterForEvent( EVENT_MODULE_LOAD, moduleLoadProc, NULL);
   if ( module_load_event_handle == EFAILURE) {
      signal( SIGTERM, SIG_DFL);
      puts( "RegisterForEvent(EVENT_MODULE_LOAD) failed");
      return EXIT_FAILURE;
   }
   new_public_event_handle = RegisterForEvent( EVENT_NEW_PUBLIC, newPublicProc, NULL);
   if ( new_public_event_handle == EFAILURE) {
      signal( SIGTERM, SIG_DFL);
      UnregisterForEvent( module_load_event_handle);
      puts( "RegisterForEvent(EVENT_NEW_PUBLIC) failed");
      return EXIT_FAILURE;
   }
   puts( "Waiting...");
   SuspendThread( mainThreadID);
   UnregisterForEvent( new_public_event_handle);
   UnregisterForEvent( module_load_event_handle);
   --threadCount;
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.