This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
CYG14 Dynaloader without USEIMPORTLIB, and search cyg prefix
[perl5.git] / ext / DynaLoader / dl_win32.xs
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
21 #ifdef __GNUC__
22 #define Win32_Winsock
23 #endif
24 #include <windows.h>
25 #include <string.h>
26
27 #define PERL_NO_GET_CONTEXT
28
29 #include "EXTERN.h"
30 #include "perl.h"
31 #include "win32.h"
32
33 #include "XSUB.h"
34
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)
43
44 static char *
45 OS_Error_String(pTHX)
46 {
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);
54 }
55
56 static void
57 dl_private_init(pTHX)
58 {
59     (void)dl_generic_private_init(aTHX);
60 }
61
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 */
67 static int
68 dl_static_linked(char *filename)
69 {
70     char **p;
71     char *ptr, *hptr;
72     static char subStr[] = "/auto/";
73     char szBuffer[MAX_PATH];
74
75     /* avoid buffer overflow when called with invalid filenames */
76     if (strlen(filename) >= sizeof(szBuffer))
77         return 0;
78
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
96     for (p = staticlinkmodules; *p;p++) {
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         }
105     };
106     return 0;
107 }
108
109 MODULE = DynaLoader     PACKAGE = DynaLoader
110
111 BOOT:
112     (void)dl_private_init(aTHX);
113
114 void *
115 dl_load_file(filename,flags=0)
116     char *              filename
117     int                 flags
118     PREINIT:
119     CODE:
120   {
121     DLDEBUG(1,PerlIO_printf(Perl_debug_log,"dl_load_file(%s):\n", filename));
122     if (dl_static_linked(filename) == 0) {
123         RETVAL = PerlProc_DynaLoad(filename);
124     }
125     else
126         RETVAL = (void*) Win_GetModuleHandle(NULL);
127     DLDEBUG(2,PerlIO_printf(Perl_debug_log," libref=%x\n", RETVAL));
128     ST(0) = sv_newmortal() ;
129     if (RETVAL == NULL)
130         SaveError(aTHX_ "load_file:%s",
131                   OS_Error_String(aTHX)) ;
132     else
133         sv_setiv( ST(0), (IV)RETVAL);
134   }
135
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
148 void *
149 dl_find_symbol(libhandle, symbolname)
150     void *      libhandle
151     char *      symbolname
152     CODE:
153     DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_find_symbol(handle=%x, symbol=%s)\n",
154                       libhandle, symbolname));
155     RETVAL = (void*) GetProcAddress((HINSTANCE) libhandle, symbolname);
156     DLDEBUG(2,PerlIO_printf(Perl_debug_log,"  symbolref = %x\n", RETVAL));
157     ST(0) = sv_newmortal() ;
158     if (RETVAL == NULL)
159         SaveError(aTHX_ "find_symbol:%s",
160                   OS_Error_String(aTHX)) ;
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:
179     DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_install_xsub(name=%s, symref=%x)\n",
180                       perl_name, symref));
181     ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
182                                         (void(*)(pTHX_ CV *))symref,
183                                         filename)));
184
185
186 char *
187 dl_error()
188     CODE:
189     dMY_CXT;
190     RETVAL = dl_last_error;
191     OUTPUT:
192     RETVAL
193
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
209 # end.