From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 27-JUN-1990 00:21:11.82
To:	MRGATE::"ARISIA::EVERHART"
CC:	
Subj:	Re: Dynamic code loading

Received:  by crdgw1.ge.com (5.57/GE 1.70)
	 id AA28693; Wed, 27 Jun 90 00:05:51 EDT
Received: From UCBVAX.BERKELEY.EDU by CRVAX.SRI.COM with TCP; Tue, 26 JUN 90 16:33:04 PDT
Received: by ucbvax.Berkeley.EDU (5.63/1.41)
	id AA08204; Tue, 26 Jun 90 16:15:19 -0700
Received: from USENET by ucbvax.Berkeley.EDU with netnews
	for info-vax@kl.sri.com (info-vax@kl.sri.com)
	(contact usenet@ucbvax.Berkeley.EDU if you have questions)
Date: 26 Jun 90 22:12:33 GMT
From: usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!uvaarpa!murdoch!dale.acc.Virginia.EDU!sdm7g@ucsd.edu  (Steve D. Majewski)
Organization: University of Virginia
Subject: Re: Dynamic code loading
Message-Id: <1990Jun26.221233.26173@murdoch.acc.Virginia.EDU>
References: <1990Jun21.162000.16382@athena.mit.edu>
Sender: info-vax-request@kl.sri.com
To: info-vax@kl.sri.com

FYI: Here is a C callable FindImageSymbol that Translates logicals
     I haven't used this code in over a year, so I don't remember 
     if it is completely debugged! You may consider it in the 
     public domain. 

   RUNTIME$DESCRIPTOR is just $DESCRIPTOR macro with strlen(string) 
   replacing sizeof(string). 
 
/*****************************************************************************
 *                                                                           *
 * MODULE:	FINDIMAGESYMBOL                                              *
 * FILE:	findimagesymbol.c                                            *
 * FUNCTION:                                                                 *
 * 		int (* FindImageSymbol( filename, symbol, trans ))()         *
 * 		[ function returning pointer to function returning int ]     *
 *                                                                           *
 * Written by:	Steve D. Majewski                                            *
 *		                                                             *
 * Copyright (C) 1987 Steve D. Majewski,Penna. Muscle Inst., Univ.of Penna.  *
 *                                                                           *
 * Distributed with No Warranty, Whatsoever.	 			     *
 *****************************************************************************
 *
 * Returns the value of a global symbol from a sharable image library: 
 *  * Not necessarily a funcion pointer * - any GLOBAL exported by
 *  a sharable image library. Internally this function could just
 *  as well be declared "long" for GlobalValue or "char *" for 
 *  GlobalReference. Any data value the right size will return the 
 *  correct value  BUT it is mainly intended for dynamic run-time
 *  linking to functions and the declartion must be right in the 
 *  calling program, so it is done correctly here as a template
 *  for future reference. ( The 2 lines declaring the function 
 *  took longer to figure out than the body of the code. )
 *  Change the "int" to "float" or "char *", etc. according to 
 *  the actual data type returned by the target function.
 *
 * use RTL LIB$ calls to
 * SYS_TRNLOG & FIND_IMAGE_SYMBOL
 *
 */

#include stringdesc
#include ssdef

int  (* FindImageSymbol( filename, symbol, trans ))()
  char * filename, *symbol;
  int trans;	/* switch  - one of : 
		 * 0 = don't translate logical; 
		 * 1 = MUST translate ( return 0 if no translation );
	         * ANY ELSE = attempt translate ;
		 */
#define MUST_TRANSLATE	1 
{

  RUNTIME$DESCRIPTOR( filenameDESC, filename );	  /* convert C strings into */
  RUNTIME$DESCRIPTOR( symbolDESC,   symbol   );   /* VMS string descriptors */
  char	  (* value)() ;                           /* returned global value  */
  char temp_string[255];                          /* translated string  &   */
  struct dsc$descriptor_s TranslatedSymbolDESC =  /* string descriptor      */
	{ 255, DSC$K_DTYPE_T, DSC$K_CLASS_S, temp_string };
  int status ; 
  
 /* Try to translate symbol first ...
  *   (If it doesn't translate, input symbol is returned as output.)
  */
  
  if ( trans ) 
    status = 
   	LIB$SYS_TRNLOG( &symbolDESC,
			&TranslatedSymbolDESC.dsc$w_length, 
		  	&TranslatedSymbolDESC  );

  if ( ( trans == MUST_TRANSLATE ) && (status == SS$_NOTRAN ))  return 0 ;
                                                               
  LIB$FIND_IMAGE_SYMBOL( &filenameDESC,
			 &TranslatedSymbolDESC,
			 &value );
  return value;

}

#ifdef TEST_FINDIM
/* TEST ABOVE FUNCTION */
main( argc, argv )
/*  $FINDIMAGESYMBOL filename symbolname switch  */
 int argc;
 char * argv[];
{      

  int( * FindImageSymbol())(), (*SymbolValue)(), status;

  if ( (argc > 3 ) && argv[3] ) status = atoi( argv[3] );
  else status = 2 ;

  printf( "\n File: %s, Symbol: %s, Value = %d",
         argv[1], argv[2],
         SymbolValue = FindImageSymbol( argv[1], argv[2], status ) );

  if ( SymbolValue == 0 ) {
	printf("\n --- Symbol %s NOT FOUND in %s", argv[2], argv[1] );
	return ;
  }

/* This will probably crash (unless number of args is right) but
 * try it anyway just to check that tries the right routine.
 */

    status = (*SymbolValue) ( 0 );
    return status ;

}
#endif
                                                                              
========================================================================
 Steven D. Majewski		University of Virginia Physiology Dept.
 sdm7g@Virginia.EDU		Box 449 Health Sciences Center
 (804)-982-0831			Charlottesville, VA 22908

