YAP 7.1.0
load_dll.c
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: load_dl.c *
12* comments: dl based dynamic loader of external routines *
13* tested on i486-linuxelf *
14*************************************************************************/
15
16#include "Yap.h"
17#include "Yatom.h"
18#include "YapHeap.h"
19#include "yapio.h"
20#include "Foreign.h"
21#include "amiops.h"
22
23#if _WIN32
24
25#include <windows.h>
26
27/*
28 * YAP_FindExecutable(argv[0]) should be called on yap initialization to
29 * locate the executable of Yap
30*/
31char *
32Yap_FindExecutable(void)
33{
34 enum { BUFFERSIZE = 1024 };
35 char *buf = malloc(BUFFERSIZE);
36
37 if (!GetModuleFileName(NULL, buf, BUFFERSIZE-1))
38 return NULL;
39
40 return buf;
41}
42
43void *
44Yap_LoadForeignFile(char *file, int flags)
45{
46 char *buf = malloc(1024);
47 void *ptr= (void *)LoadLibrary(file);
48 if (!ptr) {
49 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
50 NULL, GetLastError(),
51 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 1023,
52 NULL);
53 }
54 return ptr;
55}
56
57int
58Yap_CallForeignFile(void *handle, char *f)
59{
60 YapInitProc proc = (YapInitProc)GetProcAddress((HMODULE)handle, f);
61 if (!proc)
62 return FALSE;
63 (*proc)();
64 return TRUE;
65}
66
67int
68Yap_CloseForeignFile(void *handle)
69{
70 return FreeLibrary((HMODULE)handle);
71}
72
73/*
74 * LoadForeign(ofiles,libs,proc_name,init_proc) dynamically loads foreign
75 * code files and libraries and locates an initialization routine
76*/
77static Int
78LoadForeign(StringList ofiles, StringList libs,
79 const char *proc_name, YapInitProc *init_proc)
80{
81 CACHE_REGS
82 while (ofiles) {
83 HINSTANCE handle;
84
85 if (*init_proc == NULL &&
86 (*init_proc = (YapInitProc)GetProcAddress((HMODULE)handle, proc_name)))
87 {
88 YapInitProc f = *init_proc;
89 f();
90 return true;
91 }
92
93 const char *file = AtomName(ofiles->name);
94 if ((file=Yap_AbsoluteFile(file, true)) &&
95 (handle=LoadLibrary(file)) != 0)
96 {
97 LOCAL_ErrorMessage = NULL;
98 if (*init_proc == NULL)
99 *init_proc = (YapInitProc)GetProcAddress((HMODULE)handle, proc_name);
100 } else {
101 char *buf = malloc(1024);
102 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
103 NULL, GetLastError(),
104 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 1023,
105 NULL);
106 //fprintf(stderr,"WinError: %s\n", LOCAL_ErrorSay);
107 }
108 ofiles = ofiles->next;
109 }
110 /* load libraries first so that their symbols are available to
111 other routines */
112 while (libs) {
113 HINSTANCE handle;
114 const char * s = AtomName(libs->name);
115
116 if (s[0] == '-') {
117 strcat(LOCAL_FileNameBuf,s+2);
118 strcat(LOCAL_FileNameBuf,".dll");
119 } else {
120 strcpy(LOCAL_FileNameBuf,s);
121 }
122
123 if((handle=LoadLibrary(LOCAL_FileNameBuf)) == 0)
124 {
125/* strcpy(LOCAL_ErrorSay,dlerror());*/
126 return LOAD_FAILLED;
127 }
128
129 if (*init_proc == NULL)
130 *init_proc = (YapInitProc)GetProcAddress((HMODULE)handle, proc_name);
131
132 libs = libs->next;
133 }
134
135 if(*init_proc == NULL) {
136 LOCAL_ErrorMessage = "Could not locate initialization routine";
137 return LOAD_FAILLED;
138 }
139
140 return LOAD_SUCCEEDED;
141}
142
143Int
144Yap_LoadForeign(StringList ofiles, StringList libs,
145 char *proc_name, YapInitProc *init_proc)
146{
147 return LoadForeign(ofiles, libs, proc_name, init_proc);
148}
149
150void
151Yap_ShutdownLoadForeign(void)
152{
153}
154
155Int
156Yap_ReLoadForeign(StringList ofiles, StringList libs,
157 char *proc_name, YapInitProc *init_proc)
158{
159 return(LoadForeign(ofiles,libs, proc_name, init_proc));
160}
161
162#endif
load_foreign_files/3 has works for the following configurations:
Main definitions.
const char * Yap_AbsoluteFile(const char *spec, bool ok)
generate absolute path, if ok first expand SICStus Prolog style
Definition: absf.c:145