SWIPL Zwapel 0.0.1
paule32
Loading...
Searching...
No Matches
main.cc
1// -----------------------------------------------------------------------
2// File: SWIPL-ZWAPL main.cc
3//
4// Author: Jens Kallup - paule32 <paule32.jk@gmail.com>
5// Rights: (c) 2023 kallup non-profit.
6//
7// notice: each term: t0, t0 + 1, t0 + n, ...
8// -----------------------------------------------------------------------
9# include <iostream>
10# include "prolog.h"
11
12using namespace std;
13
14#ifdef __WINDOWS__
15HMODULE zwap_mod_handle; // external Delphi 7 32-Bit DLL
16
17LPCSTR inline get_zwap_name () { return LPCSTR("zwapwin64vcl.dll"); }
18HINSTANCE inline get_zwap_handle() { return zwap_mod_handle; }
19#else
20void* zwap_mod_handle; // external Lazarus 32-Bit DLL
21
22const char* inline get_zwap_name () { return "zwap_laz.so"; }
23void* inline get_zwap_handle() { return zwap_mod_handle; }
24#endif
25
26typedef void (__stdcall *ref_show_message)(LPCSTR, LPCSTR);
27
28typedef struct zwap_funcs {
29 ref_show_message show_message;
30}
32zwap_funcs zwap_func;
33
34#ifdef __WINDOWS_BIT32__
35extern "C" int PL_get_string(term_t t, char **s, size_t *len) { __asm__ __volatile__("jmp PL_get_string"); }
36extern "C" int PL_initialise(int argc, char **argv) { __asm__ __volatile__("jmp PL_initialise"); }
37extern "C" int PL_register_foreign(
38 const char *name,
39 size_t arity,
40 pl_function_t func,
41 int flags, ...) { __asm__ __volatile__("jmp PL_register_foreign"); }
42#endif
43
44// -------------------------------------------------------------------
45// @brief initialize the external vcl procedure's, and function's ...
46// -------------------------------------------------------------------
47static void
48init_stuff(void)
49{
50 #ifdef __WINDOWS__
51 zwap_func.show_message = (ref_show_message)GetProcAddress(get_zwap_handle(),"vcl_show_message");
52
53 char* buffer = new char[100];
54 sprintf(buffer,"addr: 0x%x",&zwap_func.show_message);
55
56 MessageBox(NULL,
57 buffer, "Warning",
58 MB_OK | MB_TASKMODAL);
59
60 zwap_func.show_message("zapo","mapo");
61
62 # else
63 # define gui_func(name) dlsym(get_zwap_handle(),name)
64 #endif
65}
66
67// -----------------------------------------------------------------------
68// simply a test ...
69// -----------------------------------------------------------------------
70static foreign_t
71pl_show_message(term_t a0, int arity, void *context)
72{
73 char *t_title;
74 char *t_text;
75
76 size_t len;
77
78 if (PL_get_string(a0, &t_title, &len)) {
79 if (PL_get_string(a0+1, &t_text, &len)) {
80 zwap_func.show_message(t_title, t_text);
81 return PL_succeed();
82 }
83 }
84 return PL_fail();
85}
86
87// -----------------------------------------------------------------------
88// @brief This function register the used predicate's that come with this
89// library. You have to use it unmangled (extern "C"). Because
90// SWIPL search for native C names: install_zwap(), then zwap().
91// -----------------------------------------------------------------------
92extern "C" install_t
93install_zwapwin64()
94{
95 PL_register_foreign(
96 "show_message",2,
97 reinterpret_cast<void*>(pl_show_message),
98 PL_FA_VARARGS);
99}
100
101// -----------------------------------------------------------------------
102// @brief under newer Windows Operation Systems, the entry point for DLL
103// files is DllMain.
104// -----------------------------------------------------------------------
105#ifdef __WINDOWS__
106BOOL WINAPI
107DllMain(
108 HINSTANCE hInstDLL, // handle to DLL module
109 DWORD fwReason, // reason for calling function
110 LPVOID lpReserved) { // reserved
111
112 switch (fwReason) {
113 case DLL_PROCESS_ATTACH: // initialize once for each new process
114 {
115 zwap_mod_handle = LoadLibraryA(get_zwap_name());
116 if (zwap_mod_handle == NULL) {
117 char* buffer = new char[2048];
118 sprintf(buffer,
119 "could not open library:\n%s\nLoading aborted\n"
120 "Windows Error: %d",
121 get_zwap_name(),
122 GetLastError());
123
124 MessageBox(NULL,
125 buffer, "Warning",
126 MB_OK | MB_TASKMODAL);
127
128 delete buffer;
129 return FALSE;
130 }
131 MessageBox(NULL,
132 "init", "Warning",
133 MB_OK | MB_TASKMODAL);
134
135 init_stuff();
136 }
137 break;
138 case DLL_THREAD_ATTACH: // do thread-specific initialization
139 break;
140 case DLL_THREAD_DETACH: // di thread-specific cleanup
141 break;
142 case DLL_PROCESS_DETACH: // perform any necassary cleanu
143 if (!FreeLibrary(get_zwap_handle())) {
144 char* buffer = new char[2048];
145 sprintf(buffer,
146 "could not close library handle: 0x%x.\n(%s)\n"
147 "Windows Error-Code: %d",
148 get_zwap_handle(),
149 get_zwap_name(),
150 GetLastError());
151
152 MessageBox(NULL,
153 buffer, "Warning",
154 MB_OK | MB_TASKMODAL);
155
156 delete buffer;
157 return FALSE;
158 }
159 break;
160 }
161
162 return 1;
163}
164#else
165// -----------------------------------------------------------------------
166// @brief library entry point / initialize stuff ...
167// -----------------------------------------------------------------------
168void __attribute__ ((constructor)) my_init(void)
169{
170 if (!(zwap_mod_handle = dlopen("zwap.so", RTLF_LAZY))) {
171 fputs(dlerror(), stderr);
172 exit(1);
173 }
174}
175
176// -----------------------------------------------------------------------
177// @brief library clean up stuff ...
178// -----------------------------------------------------------------------
179void __attribute__ ((destructor)) my_fini(void)
180{
181 dlclose(get_zwap_handle());
182}
183#endif