This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add PL_ to merged file
[perl5.git] / win32 / 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 #include "EXTERN.h"
28 #include "perl.h"
29 #include "win32.h"
30
31 #ifdef PERL_OBJECT
32 #define NO_XSLOCKS
33 #endif  /* PERL_OBJECT */
34
35 #include "XSUB.h"
36
37 static SV *error_sv;
38
39 static char *
40 OS_Error_String(CPERLarg)
41 {
42  DWORD err = GetLastError();
43  STRLEN len;
44  if (!error_sv)
45   error_sv = newSVpv("",0);
46  win32_str_os_error(error_sv,err);
47  return SvPV(error_sv,len);
48 }
49
50 #include "dlutils.c"    /* SaveError() etc      */
51
52 static void
53 dl_private_init(CPERLarg)
54 {
55     (void)dl_generic_private_init(PERL_OBJECT_THIS);
56 }
57
58 /* 
59     This function assumes the list staticlinkmodules
60     will be formed from package names with '::' replaced
61     with '/'. Thus Win32::OLE is in the list as Win32/OLE
62 */
63 static int
64 dl_static_linked(char *filename)
65 {
66     char **p;
67     char* ptr;
68     static char subStr[] = "/auto/";
69     char szBuffer[MAX_PATH];
70
71     /* change all the '\\' to '/' */
72     strcpy(szBuffer, filename);
73     for(ptr = szBuffer; ptr = strchr(ptr, '\\'); ++ptr)
74         *ptr = '/';
75
76     /* delete the file name */
77     ptr = strrchr(szBuffer, '/');
78     if(ptr != NULL)
79         *ptr = '\0';
80
81     /* remove leading lib path */
82     ptr = strstr(szBuffer, subStr);
83     if(ptr != NULL)
84         ptr += sizeof(subStr)-1;
85     else
86         ptr = szBuffer;
87
88     for (p = staticlinkmodules; *p;p++) {
89         if (strstr(ptr, *p)) return 1;
90     };
91     return 0;
92 }
93
94 MODULE = DynaLoader     PACKAGE = DynaLoader
95
96 BOOT:
97     (void)dl_private_init(PERL_OBJECT_THIS);
98
99 void *
100 dl_load_file(filename,flags=0)
101     char *              filename
102     int                 flags
103     PREINIT:
104     CODE:
105     DLDEBUG(1,PerlIO_printf(PerlIO_stderr(),"dl_load_file(%s):\n", filename));
106     if (dl_static_linked(filename) == 0)
107         RETVAL = (void*) LoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH ) ;
108     else
109         RETVAL = (void*) GetModuleHandle(NULL);
110     DLDEBUG(2,PerlIO_printf(PerlIO_stderr()," libref=%x\n", RETVAL));
111     ST(0) = sv_newmortal() ;
112     if (RETVAL == NULL)
113         SaveError(PERL_OBJECT_THIS_ "load_file:%s",
114                   OS_Error_String(PERL_OBJECT_THIS)) ;
115     else
116         sv_setiv( ST(0), (IV)RETVAL);
117
118
119 void *
120 dl_find_symbol(libhandle, symbolname)
121     void *      libhandle
122     char *      symbolname
123     CODE:
124     DLDEBUG(2,PerlIO_printf(PerlIO_stderr(),"dl_find_symbol(handle=%x, symbol=%s)\n",
125                       libhandle, symbolname));
126     RETVAL = (void*) GetProcAddress((HINSTANCE) libhandle, symbolname);
127     DLDEBUG(2,PerlIO_printf(PerlIO_stderr(),"  symbolref = %x\n", RETVAL));
128     ST(0) = sv_newmortal() ;
129     if (RETVAL == NULL)
130         SaveError(PERL_OBJECT_THIS_ "find_symbol:%s",
131                   OS_Error_String(PERL_OBJECT_THIS)) ;
132     else
133         sv_setiv( ST(0), (IV)RETVAL);
134
135
136 void
137 dl_undef_symbols()
138     PPCODE:
139
140
141
142 # These functions should not need changing on any platform:
143
144 void
145 dl_install_xsub(perl_name, symref, filename="$Package")
146     char *              perl_name
147     void *              symref 
148     char *              filename
149     CODE:
150     DLDEBUG(2,PerlIO_printf(PerlIO_stderr(),"dl_install_xsub(name=%s, symref=%x)\n",
151                       perl_name, symref));
152     ST(0)=sv_2mortal(newRV((SV*)newXS(perl_name, (void(*)(CV* _CPERLarg))symref, filename)));
153
154
155 char *
156 dl_error()
157     CODE:
158     RETVAL = LastError ;
159     OUTPUT:
160     RETVAL
161
162 # end.