2 /* --------------------------------------------------------------------------
3 * Dynamic loading (of .dll or .so files) for Hugs
5 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7 * Technology, 1994-1999, All rights reserved. It is distributed as
8 * free software under the license in the file "License", which is
9 * included in the distribution.
11 * $RCSfile: dynamic.c,v $
13 * $Date: 2000/03/23 14:54:21 $
14 * ------------------------------------------------------------------------*/
16 #include "hugsbasictypes.h"
21 #if HAVE_WINDOWS_H && !defined(__MSDOS__)
25 void* getDLLSymbol(line,dll0,symbol0) /* load dll and lookup symbol */
34 if (strlen(dll0) > 996-strlen(installDir)) {
35 ERRMSG(line) "Excessively long library name:\n%s\n",dll0
39 if (strcmp("nHandle",dll0)==0) strcat(dll,installDir);
43 if (strlen(symbol0) > 96) {
44 ERRMSG(line) "Excessively long symbol name:\n%s\n",symbol0
47 strcpy(&(symbol[1]),symbol0);
50 instance = LoadLibrary(dll);
51 if (NULL == instance) {
52 /* GetLastError allegedly provides more detail - in practice,
53 * it tells you nothing more.
55 ERRMSG(line) "Can't open library \"%s\"", dll
58 sym = GetProcAddress(instance,symbol0);
62 Bool stdcallAllowed ( void )
72 #elif HAVE_DLFCN_H /* eg LINUX, SOLARIS, ULTRIX */
77 void* getDLLSymbol(line,dll0,symbol) /* load dll and lookup symbol */
84 if (strlen(dll0) > 996-strlen(installDir)) {
85 ERRMSG(line) "Excessively long library name:\n%s\n",dll0
89 if (strcmp("nHandle",dll0)==0) strcat(dll,installDir);
93 instance = dlopen(dll,RTLD_NOW);
94 #elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
95 instance = dlopen(dll,RTLD_LAZY);
96 #else /* eg FreeBSD doesn't have RTLD_LAZY */
97 instance = dlopen(dll,1);
100 if (NULL == instance) {
101 ERRMSG(line) "Can't open library \"%s\":\n %s\n",dll,dlerror()
104 if ((sym = dlsym(instance,symbol)))
107 ERRMSG(line) "Can't find symbol \"%s\" in library \"%s\"",symbol,dll
111 Bool stdcallAllowed ( void )
121 #elif HAVE_DL_H /* eg HPUX */
125 void* getDLLSymbol(line,dll0,symbol) /* load dll and lookup symbol */
129 ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
131 if (NULL == instance) {
132 ERRMSG(line) "Error while importing DLL \"%s\"", dll0
135 return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
138 Bool stdcallAllowed ( void )
148 #else /* Dynamic loading not available */
150 void* getDLLSymbol(line,dll0,symbol) /* load dll and lookup symbol */
154 #if 1 /* very little to choose between these options */
157 ERRMSG(line) "This Hugs build does not support dynamic loading\n"
162 Bool stdcallAllowed ( void )
167 #endif /* Dynamic loading not available */