| 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 | SV ** l_dl_error_svp = &dl_error_sv; |
| 51 | SV * l_dl_error_sv; |
| 52 | if (!*l_dl_error_svp) |
| 53 | *l_dl_error_svp = newSVpvs(""); |
| 54 | l_dl_error_sv = *l_dl_error_svp; |
| 55 | PerlProc_GetOSError(l_dl_error_sv,err); |
| 56 | return SvPV(l_dl_error_sv,len); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | dl_private_init(pTHX) |
| 61 | { |
| 62 | (void)dl_generic_private_init(aTHX); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | This function assumes the list staticlinkmodules |
| 67 | will be formed from package names with '::' replaced |
| 68 | with '/'. Thus Win32::OLE is in the list as Win32/OLE |
| 69 | */ |
| 70 | static int |
| 71 | dl_static_linked(char *filename) |
| 72 | { |
| 73 | const char * const *p; |
| 74 | char *ptr, *hptr; |
| 75 | static const char subStr[] = "/auto/"; |
| 76 | char szBuffer[MAX_PATH]; |
| 77 | |
| 78 | /* avoid buffer overflow when called with invalid filenames */ |
| 79 | if (strlen(filename) >= sizeof(szBuffer)) |
| 80 | return 0; |
| 81 | |
| 82 | /* change all the '\\' to '/' */ |
| 83 | strcpy(szBuffer, filename); |
| 84 | for(ptr = szBuffer; ptr = strchr(ptr, '\\'); ++ptr) |
| 85 | *ptr = '/'; |
| 86 | |
| 87 | /* delete the file name */ |
| 88 | ptr = strrchr(szBuffer, '/'); |
| 89 | if(ptr != NULL) |
| 90 | *ptr = '\0'; |
| 91 | |
| 92 | /* remove leading lib path */ |
| 93 | ptr = strstr(szBuffer, subStr); |
| 94 | if(ptr != NULL) |
| 95 | ptr += sizeof(subStr)-1; |
| 96 | else |
| 97 | ptr = szBuffer; |
| 98 | |
| 99 | for (p = staticlinkmodules; *p;p++) { |
| 100 | if (hptr = strstr(ptr, *p)) { |
| 101 | /* found substring, need more detailed check if module name match */ |
| 102 | if (hptr==ptr) { |
| 103 | return strcmp(ptr, *p)==0; |
| 104 | } |
| 105 | if (hptr[strlen(*p)] == 0) |
| 106 | return hptr[-1]=='/'; |
| 107 | } |
| 108 | }; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | MODULE = DynaLoader PACKAGE = DynaLoader |
| 113 | |
| 114 | BOOT: |
| 115 | (void)dl_private_init(aTHX); |
| 116 | |
| 117 | void |
| 118 | dl_load_file(filename,flags=0) |
| 119 | char * filename |
| 120 | #flags is unused |
| 121 | SV * flags = NO_INIT |
| 122 | PREINIT: |
| 123 | void *retv; |
| 124 | SV * retsv; |
| 125 | CODE: |
| 126 | { |
| 127 | PERL_UNUSED_VAR(flags); |
| 128 | DLDEBUG(1,PerlIO_printf(Perl_debug_log,"dl_load_file(%s):\n", filename)); |
| 129 | if (dl_static_linked(filename) == 0) { |
| 130 | retv = PerlProc_DynaLoad(filename); |
| 131 | } |
| 132 | else |
| 133 | retv = (void*) Win_GetModuleHandle(NULL); |
| 134 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," libref=%x\n", retv)); |
| 135 | |
| 136 | if (retv == NULL) { |
| 137 | SaveError(aTHX_ "load_file:%s", |
| 138 | OS_Error_String(aTHX)) ; |
| 139 | retsv = &PL_sv_undef; |
| 140 | } |
| 141 | else |
| 142 | retsv = sv_2mortal(newSViv((IV)retv)); |
| 143 | ST(0) = retsv; |
| 144 | } |
| 145 | |
| 146 | int |
| 147 | dl_unload_file(libref) |
| 148 | void * libref |
| 149 | CODE: |
| 150 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", PTR2ul(libref))); |
| 151 | RETVAL = FreeLibrary((HMODULE)libref); |
| 152 | if (!RETVAL) |
| 153 | SaveError(aTHX_ "unload_file:%s", OS_Error_String(aTHX)) ; |
| 154 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL)); |
| 155 | OUTPUT: |
| 156 | RETVAL |
| 157 | |
| 158 | void |
| 159 | dl_find_symbol(libhandle, symbolname) |
| 160 | void * libhandle |
| 161 | char * symbolname |
| 162 | PREINIT: |
| 163 | void *retv; |
| 164 | CODE: |
| 165 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_find_symbol(handle=%x, symbol=%s)\n", |
| 166 | libhandle, symbolname)); |
| 167 | retv = (void*) GetProcAddress((HINSTANCE) libhandle, symbolname); |
| 168 | DLDEBUG(2,PerlIO_printf(Perl_debug_log," symbolref = %x\n", retv)); |
| 169 | ST(0) = sv_newmortal() ; |
| 170 | if (retv == NULL) |
| 171 | SaveError(aTHX_ "find_symbol:%s", |
| 172 | OS_Error_String(aTHX)) ; |
| 173 | else |
| 174 | sv_setiv( ST(0), (IV)retv); |
| 175 | |
| 176 | |
| 177 | void |
| 178 | dl_undef_symbols() |
| 179 | CODE: |
| 180 | |
| 181 | |
| 182 | |
| 183 | # These functions should not need changing on any platform: |
| 184 | |
| 185 | void |
| 186 | dl_install_xsub(perl_name, symref, filename="$Package") |
| 187 | char * perl_name |
| 188 | void * symref |
| 189 | char * filename |
| 190 | CODE: |
| 191 | DLDEBUG(2,PerlIO_printf(Perl_debug_log,"dl_install_xsub(name=%s, symref=%x)\n", |
| 192 | perl_name, symref)); |
| 193 | ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name, |
| 194 | (void(*)(pTHX_ CV *))symref, |
| 195 | filename))); |
| 196 | |
| 197 | |
| 198 | SV * |
| 199 | dl_error() |
| 200 | CODE: |
| 201 | dMY_CXT; |
| 202 | RETVAL = newSVsv(MY_CXT.x_dl_last_error); |
| 203 | OUTPUT: |
| 204 | RETVAL |
| 205 | |
| 206 | #if defined(USE_ITHREADS) |
| 207 | |
| 208 | void |
| 209 | CLONE(...) |
| 210 | CODE: |
| 211 | MY_CXT_CLONE; |
| 212 | |
| 213 | PERL_UNUSED_VAR(items); |
| 214 | |
| 215 | /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid |
| 216 | * using Perl variables that belong to another thread, we create our |
| 217 | * own for this thread. |
| 218 | */ |
| 219 | MY_CXT.x_dl_last_error = newSVpvs(""); |
| 220 | |
| 221 | #endif |
| 222 | |
| 223 | # end. |