-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
369 lines (245 loc) · 8.48 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* main.c
mainmodule - exports main_export and i1TestMain -> dll1
*/
#if defined _MSC_VER || __MINGW64__
#include <windows.h>
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> // for ...
#include <ctype.h> // isdigit
#include <signal.h>
#include <errno.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <dlfcn.h> // Unix: dlopen
#endif
#define FUNC_LOAD_only 20
#define FUNC_LOAD_all 21
#define FUNC_CONNECT 22
#define FUNC_EXEC 23
#define FUNC_UNLOAD 24
#define FUNC_RECOMPILE 25
#define FUNC_QUERY 26
#define FUNC_Init 27
int i1TestMain; // export to dll
int main_export (char *txt); // export to dll
void TX_Error (char* txt, ...);
//================================================================
int main (int argc, char *argv[]) {
//================================================================
int irc;
char s1[64];
static char *dllNam = "dll1";
static char *fncNam = "dll_main_start";
printf("\n=========================\n");
printf(" test_dll_main starting ..\n");
i1TestMain = 123;
main_export ("-- in main 1 --");
//----------------------------------------------------------------
L_start:
// MSYS and MSVC; LoadLibrary ..
// 0=load+start+unload; 1=load+start; 2=unload;
// test load+start+unload
irc = OS_dll_do (dllNam, fncNam, NULL, 0);
if(irc < 0) goto L_exit;
main_export ("-- in main 2 --");
//----------------------------------------------------------------
// test load+start
irc = OS_dll_do (dllNam, "dll_main_start", NULL, 1);
if(irc < 0) goto L_exit;
// already loaded;
irc = OS_dll_do (dllNam, "dll_main_exit", NULL, 1);
if(irc < 0) goto L_exit;
// unload
irc = OS_dll_do (dllNam, NULL, NULL, 2);
if(irc < 0) goto L_exit;
//----------------------------------------------------------------
// TODO recompile dll
//----------------------------------------------------------------
// get key from user; Esc to exit, CR to edit dll1.c
printf("- key e CR for edit dll1.c\n");
printf("- key CR for exit\n");
s1[0] = getchar ();
if(s1[0] == 10) goto L_exit;
s1[0] = getchar (); // get char 10
//----------------------------------------------------------------
// reuse dll ..
// check in dll-output ..
i1TestMain = 456;
main_export ("-- in main 20 --");
goto L_start;
//----------------------------------------------------------------
L_exit:
main_export (" exit test_dll_main");
printf("=========================\n\n");
return 0;
}
//================================================================
int main_export (char *txt) {
//================================================================
// func to be used by dll
printf(" main_export |%s|\n",txt);
return 0;
}
//===================================================================
int OS_dll_build (char *dllNam) {
//===================================================================
// recompile dll
int irc;
char *sdir, cbuf[256]; // char cbuf[512];
printf("OS_dll_build |%s|\n",dllNam);
// // sprintf(cbuf, "%sxa\\%s",AP_get_bas_dir(),dllNam);
// // sprintf(cbuf, "%s..\\src\\APP\\%s",AP_get_loc_dir(),dllNam);
// sprintf(cbuf, "%ssrc\\APP\\%s", sdir, dllNam);
//
//
// // ".dll" -> ".nmak"
// strcpy(&cbuf[strlen(cbuf)-4], ".nmak");
// // printf(" exist: |%s|\n",cbuf);
// if(OS_checkFilExist (cbuf, 1) == 0) goto L_err_nof;
//
//
// printf(".. compile .. link .. %s",dllNam);
//
// // sprintf(cbuf, "cd %sxa&&nmake -f %s",AP_get_bas_dir(),dllNam);
// // sprintf(cbuf, "cd %s..\\src\\APP&&nmake -f %s",AP_get_loc_dir(),dllNam);
// sprintf(cbuf, "CMD /C \"make -f %s\"", sdir, dllNam);
// // printf(" OS_dll_build 2 |%s|\n",cbuf);
//
// irc = OS_system(cbuf);
// if(irc != 0) TX_Error("Error build %s",dllNam);
//
// return irc;
//
// L_err_nof:
// printf("***** %s does not exist ..",cbuf);
return 0;
}
//====================================================================
int OS_debug_dll_ (char *dllNam) {
//====================================================================
// wenn im Debug-Modus wird hier gestoppt;
// In .gdbinit muss stehen:
// break OS_debug_dll_
// wenn Debugger steht, eingeben "watch varnam" und "c"
//
// Zweck: Debugger kann DLL nur ansprechen wenn geladen.
printf("OS_debug_dll_ |%s|\n",dllNam);
return 0;
}
#if defined _MSC_VER || __MINGW64__
// Windows - MS-VC or MSYS
//================================================================
int OS_checkFilExist (char* filnam, int mode) {
//================================================================
// OS_checkFilExist check if File or Directory exists
// mode = 0: display message sofort;
// mode = 1: just fix returncode, no message
//
// rc = 0: no, file does NOT exist, error
// rc = 1: yes, file exists, OK.
int i1;
char fn[512];
printf("OS_checkFilExist |%s| %d\n", filnam, mode);
// if(strlen(filnam) > 510) {
// TX_Error ("OS_checkFilExist E1"); exit (-1);
// }
//
// sprintf(fn, "\"%s\a",filnam);
i1 = GetFileAttributes (filnam);
// printf(" GetFileAttributes %d %s\n",i1,filnam);
// returns -1=file not found; 16=file, 32=directory ?
// printf("GetFileAttributes %d |%s|\n",i1+1,filnam);
i1 += 1;
if(i1 > 1) i1 = 1;
printf("ex OS_checkFilExist %d |%s|\n",i1,filnam);
return i1;
}
//================================================================
int OS_system (char *syscmd) {
//================================================================
// OS_system Perform OS-Command; wait for completion (system)
// - enclose all parameters with pathname with ""
// - enclose complete command also with ""
// os-function "system" CANNOT handle exename with blanks (does not accept
// exename enclosed with "")
// returns 0=OK; -1=Error
int irc, i1;
// char sDir[256];
BOOL fExit;
DWORD dwExitCode, dw;
HANDLE hProcess, hThread;
PROCESS_INFORMATION pi;
STARTUPINFO sui;
// printf("OS_system |%s|\n",syscmd);
memset(&sui, 0, sizeof(sui));
sui.cb = sizeof(sui);
// sui.dwFlags = STARTF_FORCEONFEEDBACK | STARTF_USESHOWWINDOW;
// sui.wShowWindow = SW_SHOWNORMAL;
if(!CreateProcess(
NULL, // zu startende Appli (mit Pfad)
syscmd, // oder Commandline (appli=NULL)
NULL, // Proc.Security
NULL, // Thread security
TRUE, // handle inheritance flag
0, //HIGH_PRIORITY_CLASS, // creation flags
NULL, // pointer to new environment block
NULL, // sDir // pointer to current directory name
&sui, // pointer to STARTUPINFO
&pi)) { // pointer to PROCESS_INFORMATION
irc = -1;
goto L_exit;
}
// aufs beenden warten
dw = WaitForSingleObject (pi.hProcess, INFINITE) ;
irc = 0;
if(dw != 0xFFFFFFFF) {
// den Exitcode abfragen
i1 = GetExitCodeProcess (pi.hProcess, &dwExitCode) ;
// printf(" GetExitCodeProcess %d %d\n",i1,dwExitCode);
if(dwExitCode) irc = -1;
}
// close the process and thread object handles
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
// printf(" GetLastError = %d\n",GetLastError());
L_exit:
// printf(" ex-OS_system %d\n",irc);
return irc;
}
#else
// Unix
//================================================================
int OS_system (char *buf) {
//================================================================
// OS_system Perform OS-Command; wait for completion (system)
// do not wait: use OS_exec
// Spezialversion fuer AIX + CATIA.
void *catch;
int ret;
catch = signal(SIGCLD, SIG_DFL); // SIGCLD | SIGCHLD
ret = system(buf);
signal(SIGCLD, catch);
if (ret) { perror(buf); }
return(ret);
}
#endif
///==========================================================
void TX_Error (char* txt, ...) {
///==========================================================
/// formatted errortext into messagewindow.
/// Example:
/// TX_Print (".. distance must not exceed %lf", d1);
char TX_buf1[1024];
va_list va;
va_start(va,txt);
strcpy(TX_buf1, "*** Fehler: ");
// o123456789012
vsprintf(&TX_buf1[12],txt,va);
va_end(va);
printf ("%s",TX_buf1);
}
// EOF