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 | ||
75 | /* change all the '\\' to '/' */ | |
76 | strcpy(szBuffer, filename); | |
77 | for(ptr = szBuffer; ptr = strchr(ptr, '\\'); ++ptr) | |
78 | *ptr = '/'; | |
79 | ||
80 | /* delete the file name */ | |
81 | ptr = strrchr(szBuffer, '/'); | |
82 | if(ptr != NULL) | |
83 | *ptr = '\0'; | |
84 | ||
85 | /* remove leading lib path */ | |
86 | ptr = strstr(szBuffer, subStr); | |
87 | if(ptr != NULL) | |
88 | ptr += sizeof(subStr)-1; | |
89 | else | |
90 | ptr = szBuffer; | |
91 | ||
0a753a76 | 92 | for (p = staticlinkmodules; *p;p++) { |
d2b25974 VK |
93 | if (hptr = strstr(ptr, *p)) { |
94 | /* found substring, need more detailed check if module name match */ | |
95 | if (hptr==ptr) { | |
96 | return strcmp(ptr, *p)==0; | |
97 | } | |
98 | if (hptr[strlen(*p)] == 0) | |
99 | return hptr[-1]=='/'; | |
100 | } | |
68dc0745 | 101 | }; |
102 | return 0; | |
0a753a76 | 103 | } |
104 | ||
105 | MODULE = DynaLoader PACKAGE = DynaLoader | |
106 | ||
107 | BOOT: | |
acfe0abc | 108 | (void)dl_private_init(aTHX); |
0a753a76 | 109 | |
110 | void * | |
111 | dl_load_file(filename,flags=0) | |
112 | char * filename | |
113 | int flags | |
114 | PREINIT: | |
115 | CODE: | |
b9010385 | 116 | { |
bf49b057 | 117 | DLDEBUG(1,PerlIO_printf(Perl_debug_log,"dl_load_file(%s):\n", filename)); |
7fac1903 | 118 | if (dl_static_linked(filename) == 0) { |
0cb96387 | 119 | RETVAL = PerlProc_DynaLoad(filename); |
7fac1903 | 120 | } |
68dc0745 | 121 | else |
7bd379e8 | 122 | RETVAL = (void*) Win_GetModuleHandle(NULL); |
bf49b057 | 123 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," libref=%x\n", RETVAL)); |
0a753a76 | 124 | ST(0) = sv_newmortal() ; |
125 | if (RETVAL == NULL) | |
acfe0abc GS |
126 | SaveError(aTHX_ "load_file:%s", |
127 | OS_Error_String(aTHX)) ; | |
0a753a76 | 128 | else |
129 | sv_setiv( ST(0), (IV)RETVAL); | |
b9010385 | 130 | } |
0a753a76 | 131 | |
6a57da86 D |
132 | int |
133 | dl_unload_file(libref) | |
134 | void * libref | |
135 | CODE: | |
136 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", PTR2ul(libref))); | |
137 | RETVAL = FreeLibrary(libref); | |
138 | if (!RETVAL) | |
139 | SaveError(aTHX_ "unload_file:%s", OS_Error_String(aTHX)) ; | |
140 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL)); | |
141 | OUTPUT: | |
142 | RETVAL | |
143 | ||
0a753a76 | 144 | void * |
145 | dl_find_symbol(libhandle, symbolname) | |
146 | void * libhandle | |
147 | char * symbolname | |
148 | CODE: | |
bf49b057 | 149 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_find_symbol(handle=%x, symbol=%s)\n", |
68dc0745 | 150 | libhandle, symbolname)); |
0a753a76 | 151 | RETVAL = (void*) GetProcAddress((HINSTANCE) libhandle, symbolname); |
bf49b057 | 152 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," symbolref = %x\n", RETVAL)); |
0a753a76 | 153 | ST(0) = sv_newmortal() ; |
154 | if (RETVAL == NULL) | |
acfe0abc GS |
155 | SaveError(aTHX_ "find_symbol:%s", |
156 | OS_Error_String(aTHX)) ; | |
0a753a76 | 157 | else |
158 | sv_setiv( ST(0), (IV)RETVAL); | |
159 | ||
160 | ||
161 | void | |
162 | dl_undef_symbols() | |
163 | PPCODE: | |
164 | ||
165 | ||
166 | ||
167 | # These functions should not need changing on any platform: | |
168 | ||
169 | void | |
170 | dl_install_xsub(perl_name, symref, filename="$Package") | |
171 | char * perl_name | |
172 | void * symref | |
173 | char * filename | |
174 | CODE: | |
bf49b057 | 175 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_install_xsub(name=%s, symref=%x)\n", |
68dc0745 | 176 | perl_name, symref)); |
4f63d024 | 177 | ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name, |
acfe0abc | 178 | (void(*)(pTHX_ CV *))symref, |
4f63d024 | 179 | filename))); |
0a753a76 | 180 | |
181 | ||
182 | char * | |
183 | dl_error() | |
184 | CODE: | |
cdc73a10 JH |
185 | dMY_CXT; |
186 | RETVAL = dl_last_error; | |
0a753a76 | 187 | OUTPUT: |
188 | RETVAL | |
189 | ||
8c472fc1 CB |
190 | #if defined(USE_ITHREADS) |
191 | ||
192 | void | |
193 | CLONE(...) | |
194 | CODE: | |
195 | MY_CXT_CLONE; | |
196 | ||
197 | /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid | |
198 | * using Perl variables that belong to another thread, we create our | |
199 | * own for this thread. | |
200 | */ | |
201 | MY_CXT.x_dl_last_error = newSVpvn("", 0); | |
202 | ||
203 | #endif | |
204 | ||
0a753a76 | 205 | # end. |