Commit | Line | Data |
---|---|---|
0a753a76 | 1 | /* dl_win32.xs |
2 | * | |
3 | * Platform: Win32 (Windows NT/Windows 95) | |
4 | * Author: Wei-Yuen Tan (wyt@hip.com) | |
5 | * Created: A warm day in June, 1995 | |
6 | * | |
7 | * Modified: | |
8 | * August 23rd 1995 - rewritten after losing everything when I | |
9 | * wiped off my NT partition (eek!) | |
10 | */ | |
11 | ||
12 | /* Porting notes: | |
13 | ||
14 | I merely took Paul's dl_dlopen.xs, took out extraneous stuff and | |
15 | replaced the appropriate SunOS calls with the corresponding Win32 | |
16 | calls. | |
17 | ||
18 | */ | |
19 | ||
20 | #define WIN32_LEAN_AND_MEAN | |
a835ef8a NIS |
21 | #ifdef __GNUC__ |
22 | #define Win32_Winsock | |
23 | #endif | |
0a753a76 | 24 | #include <windows.h> |
25 | #include <string.h> | |
26 | ||
c5be433b GS |
27 | #define PERL_NO_GET_CONTEXT |
28 | ||
0a753a76 | 29 | #include "EXTERN.h" |
30 | #include "perl.h" | |
eda5ff31 | 31 | #include "win32.h" |
565764a8 | 32 | |
0a753a76 | 33 | #include "XSUB.h" |
34 | ||
cdc73a10 JH |
35 | typedef struct { |
36 | SV * x_error_sv; | |
37 | } my_cxtx_t; /* this *must* be named my_cxtx_t */ | |
38 | ||
39 | #define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */ | |
40 | #include "dlutils.c" /* SaveError() etc */ | |
41 | ||
42 | #define dl_error_sv (dl_cxtx.x_error_sv) | |
eda5ff31 GS |
43 | |
44 | static char * | |
acfe0abc | 45 | OS_Error_String(pTHX) |
eda5ff31 | 46 | { |
cdc73a10 JH |
47 | dMY_CXT; |
48 | DWORD err = GetLastError(); | |
49 | STRLEN len; | |
50 | if (!dl_error_sv) | |
51 | dl_error_sv = newSVpvn("",0); | |
52 | PerlProc_GetOSError(dl_error_sv,err); | |
53 | return SvPV(dl_error_sv,len); | |
eda5ff31 GS |
54 | } |
55 | ||
0a753a76 | 56 | static void |
acfe0abc | 57 | dl_private_init(pTHX) |
0a753a76 | 58 | { |
acfe0abc | 59 | (void)dl_generic_private_init(aTHX); |
0a753a76 | 60 | } |
61 | ||
26b3385c DL |
62 | /* |
63 | This function assumes the list staticlinkmodules | |
64 | will be formed from package names with '::' replaced | |
65 | with '/'. Thus Win32::OLE is in the list as Win32/OLE | |
66 | */ | |
0a753a76 | 67 | static int |
68 | dl_static_linked(char *filename) | |
69 | { | |
68dc0745 | 70 | char **p; |
d2b25974 | 71 | char *ptr, *hptr; |
26b3385c DL |
72 | static char subStr[] = "/auto/"; |
73 | char szBuffer[MAX_PATH]; | |
74 | ||
0e244b13 JD |
75 | /* avoid buffer overflow when called with invalid filenames */ |
76 | if (strlen(filename) >= sizeof(szBuffer)) | |
77 | return 0; | |
78 | ||
26b3385c DL |
79 | /* change all the '\\' to '/' */ |
80 | strcpy(szBuffer, filename); | |
81 | for(ptr = szBuffer; ptr = strchr(ptr, '\\'); ++ptr) | |
82 | *ptr = '/'; | |
83 | ||
84 | /* delete the file name */ | |
85 | ptr = strrchr(szBuffer, '/'); | |
86 | if(ptr != NULL) | |
87 | *ptr = '\0'; | |
88 | ||
89 | /* remove leading lib path */ | |
90 | ptr = strstr(szBuffer, subStr); | |
91 | if(ptr != NULL) | |
92 | ptr += sizeof(subStr)-1; | |
93 | else | |
94 | ptr = szBuffer; | |
95 | ||
0a753a76 | 96 | for (p = staticlinkmodules; *p;p++) { |
d2b25974 VK |
97 | if (hptr = strstr(ptr, *p)) { |
98 | /* found substring, need more detailed check if module name match */ | |
99 | if (hptr==ptr) { | |
100 | return strcmp(ptr, *p)==0; | |
101 | } | |
102 | if (hptr[strlen(*p)] == 0) | |
103 | return hptr[-1]=='/'; | |
104 | } | |
68dc0745 | 105 | }; |
106 | return 0; | |
0a753a76 | 107 | } |
108 | ||
109 | MODULE = DynaLoader PACKAGE = DynaLoader | |
110 | ||
111 | BOOT: | |
acfe0abc | 112 | (void)dl_private_init(aTHX); |
0a753a76 | 113 | |
114 | void * | |
115 | dl_load_file(filename,flags=0) | |
116 | char * filename | |
117 | int flags | |
118 | PREINIT: | |
119 | CODE: | |
b9010385 | 120 | { |
bf49b057 | 121 | DLDEBUG(1,PerlIO_printf(Perl_debug_log,"dl_load_file(%s):\n", filename)); |
7fac1903 | 122 | if (dl_static_linked(filename) == 0) { |
0cb96387 | 123 | RETVAL = PerlProc_DynaLoad(filename); |
7fac1903 | 124 | } |
68dc0745 | 125 | else |
7bd379e8 | 126 | RETVAL = (void*) Win_GetModuleHandle(NULL); |
bf49b057 | 127 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," libref=%x\n", RETVAL)); |
0a753a76 | 128 | ST(0) = sv_newmortal() ; |
129 | if (RETVAL == NULL) | |
acfe0abc GS |
130 | SaveError(aTHX_ "load_file:%s", |
131 | OS_Error_String(aTHX)) ; | |
0a753a76 | 132 | else |
133 | sv_setiv( ST(0), (IV)RETVAL); | |
b9010385 | 134 | } |
0a753a76 | 135 | |
6a57da86 D |
136 | int |
137 | dl_unload_file(libref) | |
138 | void * libref | |
139 | CODE: | |
140 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", PTR2ul(libref))); | |
141 | RETVAL = FreeLibrary(libref); | |
142 | if (!RETVAL) | |
143 | SaveError(aTHX_ "unload_file:%s", OS_Error_String(aTHX)) ; | |
144 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL)); | |
145 | OUTPUT: | |
146 | RETVAL | |
147 | ||
0a753a76 | 148 | void * |
149 | dl_find_symbol(libhandle, symbolname) | |
150 | void * libhandle | |
151 | char * symbolname | |
152 | CODE: | |
bf49b057 | 153 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_find_symbol(handle=%x, symbol=%s)\n", |
68dc0745 | 154 | libhandle, symbolname)); |
0a753a76 | 155 | RETVAL = (void*) GetProcAddress((HINSTANCE) libhandle, symbolname); |
bf49b057 | 156 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," symbolref = %x\n", RETVAL)); |
0a753a76 | 157 | ST(0) = sv_newmortal() ; |
158 | if (RETVAL == NULL) | |
acfe0abc GS |
159 | SaveError(aTHX_ "find_symbol:%s", |
160 | OS_Error_String(aTHX)) ; | |
0a753a76 | 161 | else |
162 | sv_setiv( ST(0), (IV)RETVAL); | |
163 | ||
164 | ||
165 | void | |
166 | dl_undef_symbols() | |
167 | PPCODE: | |
168 | ||
169 | ||
170 | ||
171 | # These functions should not need changing on any platform: | |
172 | ||
173 | void | |
174 | dl_install_xsub(perl_name, symref, filename="$Package") | |
175 | char * perl_name | |
176 | void * symref | |
177 | char * filename | |
178 | CODE: | |
bf49b057 | 179 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_install_xsub(name=%s, symref=%x)\n", |
68dc0745 | 180 | perl_name, symref)); |
4f63d024 | 181 | ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name, |
acfe0abc | 182 | (void(*)(pTHX_ CV *))symref, |
4f63d024 | 183 | filename))); |
0a753a76 | 184 | |
185 | ||
186 | char * | |
187 | dl_error() | |
188 | CODE: | |
cdc73a10 JH |
189 | dMY_CXT; |
190 | RETVAL = dl_last_error; | |
0a753a76 | 191 | OUTPUT: |
192 | RETVAL | |
193 | ||
8c472fc1 CB |
194 | #if defined(USE_ITHREADS) |
195 | ||
196 | void | |
197 | CLONE(...) | |
198 | CODE: | |
199 | MY_CXT_CLONE; | |
200 | ||
201 | /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid | |
202 | * using Perl variables that belong to another thread, we create our | |
203 | * own for this thread. | |
204 | */ | |
205 | MY_CXT.x_dl_last_error = newSVpvn("", 0); | |
206 | ||
207 | #endif | |
208 | ||
0a753a76 | 209 | # end. |