1 /********************************************************************/
2 /* */
3 /* dll_win.c Dynamic link library (*.dll) support. */
4 /* Copyright (C) 1989 - 2014 Thomas Mertes */
5 /* */
6 /* This file is part of the Seed7 Runtime Library. */
7 /* */
8 /* The Seed7 Runtime Library is free software; you can */
9 /* redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation; either version 2.1 of the License, or (at your */
12 /* option) any later version. */
13 /* */
14 /* The Seed7 Runtime Library is distributed in the hope that it */
15 /* will be useful, but WITHOUT ANY WARRANTY; without even the */
16 /* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
17 /* PURPOSE. See the GNU Lesser General Public License for more */
18 /* details. */
19 /* */
20 /* You should have received a copy of the GNU Lesser General */
21 /* Public License along with this program; if not, write to the */
22 /* Free Software Foundation, Inc., 51 Franklin Street, */
23 /* Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* */
25 /* Module: Seed7 Runtime Library */
26 /* File: seed7/src/dll_win.c */
27 /* Changes: 2014 Thomas Mertes */
28 /* Content: Dynamic link library (*.dll) support. */
29 /* */
30 /********************************************************************/
31
32 #define LOG_FUNCTIONS 0
33 #define VERBOSE_EXCEPTIONS 0
34
35 #include "version.h"
36
37 #include "stdlib.h"
38 #include "stdio.h"
39 #include "string.h"
40 #include "windows.h"
41
42 #include "common.h"
43 #include "heaputl.h"
44 #include "dll_drv.h"
45
46
47
48 /**
49 * Open a DLL respectively shared library with the given 'dllName'.
50 * @return a reference to the library, or NULL if it was not found.
51 */
dllOpen(const char * dllName)52 void *dllOpen (const char *dllName)
53
54 {
55 memSizeType nameLength;
56 char *dllPath;
57 memSizeType pos;
58 void *aDll;
59
60 /* dllOpen */
61 if (strchr(dllName, '/') != NULL) {
62 /* LoadLibrary() needs a path with backslashes (\) */
63 /* instead of forward slashes (/). */
64 nameLength = strlen(dllName);
65 if (unlikely(!ALLOC_CSTRI(dllPath, nameLength))) {
66 aDll = NULL;
67 } else {
68 for (pos = 0; pos < nameLength; pos++) {
69 if (dllName[pos] == '/') {
70 dllPath[pos] = '\\';
71 } else {
72 dllPath[pos] = dllName[pos];
73 } /* if */
74 } /* for */
75 dllPath[nameLength] = '\0';
76 aDll = (void *) LoadLibrary(dllPath);
77 UNALLOC_CSTRI(dllPath, nameLength);
78 } /* if */
79 } else {
80 aDll = (void *) LoadLibrary(dllName);
81 } /* if */
82 logErrorIfTrue(aDll == NULL,
83 {
84 printf("LoadLibrary(\"%s\") failed:\n"
85 "error: " FMT_U32 "\n",
86 dllName, GetLastError());
87 });
88 return aDll;
89 } /* dllOpen */
90
91
92
93 /**
94 * Get the function specified with 'symbol' from the library 'dll'.
95 * @return a pointer to the function, or NULL if it was not found.
96 */
dllFunc(void * dll,const char * symbol)97 funcPtrType dllFunc (void *dll, const char *symbol)
98
99 { /* dllFunc */
100 return (funcPtrType) GetProcAddress((HMODULE) dll, symbol);
101 } /* dllFunc */
102