2 /* Support for dynamic loading of extension modules */
11 const struct filedescr _PyImport_DynLoadFiletab
[] = {
13 {"_d.pyd", "rb", C_EXTENSION
},
14 {"_d.dll", "rb", C_EXTENSION
},
16 {".pyd", "rb", C_EXTENSION
},
17 {".dll", "rb", C_EXTENSION
},
25 /* Case insensitive string compare, to avoid any dependencies on particular
26 C RTL implementations */
28 static int strcasecmp (char *string1
, char *string2
)
33 first
= tolower(*string1
);
34 second
= tolower(*string2
);
37 } while (first
&& first
== second
);
39 return (first
- second
);
43 /* Function to return the name of the "python" DLL that the supplied module
44 directly imports. Looks through the list of imported modules and
45 returns the first entry that starts with "python" (case sensitive) and
46 is followed by nothing but numbers until the separator (period).
48 Returns a pointer to the import name, or NULL if no matching name was
51 This function parses through the PE header for the module as loaded in
52 memory by the system loader. The PE header is accessed as documented by
53 Microsoft in the MSDN PE and COFF specification (2/99), and handles
54 both PE32 and PE32+. It only worries about the direct import table and
55 not the delay load import table since it's unlikely an extension is
56 going to be delay loading Python (after all, it's already loaded).
58 If any magic values are not found (e.g., the PE header or optional
59 header magic), then this function simply returns NULL. */
61 #define DWORD_AT(mem) (*(DWORD *)(mem))
62 #define WORD_AT(mem) (*(WORD *)(mem))
64 static char *GetPythonImport (HINSTANCE hModule
)
66 unsigned char *dllbase
, *import_data
, *import_name
;
67 DWORD pe_offset
, opt_offset
;
69 int num_dict_off
, import_off
;
71 /* Safety check input */
72 if (hModule
== NULL
) {
76 /* Module instance is also the base load address. First portion of
77 memory is the MS-DOS loader, which holds the offset to the PE
78 header (from the load base) at 0x3C */
79 dllbase
= (unsigned char *)hModule
;
80 pe_offset
= DWORD_AT(dllbase
+ 0x3C);
82 /* The PE signature must be "PE\0\0" */
83 if (memcmp(dllbase
+pe_offset
,"PE\0\0",4)) {
87 /* Following the PE signature is the standard COFF header (20
88 bytes) and then the optional header. The optional header starts
89 with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
90 uses 64-bits for some fields). It might also be 0x107 for a ROM
91 image, but we don't process that here.
93 The optional header ends with a data dictionary that directly
94 points to certain types of data, among them the import entries
95 (in the second table entry). Based on the header type, we
96 determine offsets for the data dictionary count and the entry
97 within the dictionary pointing to the imports. */
99 opt_offset
= pe_offset
+ 4 + 20;
100 opt_magic
= WORD_AT(dllbase
+opt_offset
);
101 if (opt_magic
== 0x10B) {
105 } else if (opt_magic
== 0x20B) {
114 /* Now if an import table exists, offset to it and walk the list of
115 imports. The import table is an array (ending when an entry has
116 empty values) of structures (20 bytes each), which contains (at
117 offset 12) a relative address (to the module base) at which a
118 string constant holding the import name is located. */
120 if (DWORD_AT(dllbase
+ opt_offset
+ num_dict_off
) >= 2) {
121 import_data
= dllbase
+ DWORD_AT(dllbase
+
124 while (DWORD_AT(import_data
)) {
125 import_name
= dllbase
+ DWORD_AT(import_data
+12);
126 if (strlen(import_name
) >= 6 &&
127 !strncmp(import_name
,"python",6)) {
130 /* Ensure python prefix is followed only
131 by numbers to the end of the basename */
132 pch
= import_name
+ 6;
133 while (*pch
&& *pch
!= '.') {
134 if (*pch
>= '0' && *pch
<= '9') {
143 /* Found it - return the name */
153 #endif /* MS_WIN32 */
156 dl_funcptr
_PyImport_GetDynLoadFunc(const char *fqname
, const char *shortname
,
157 const char *pathname
, FILE *fp
)
160 char funcname
[258], *import_python
;
162 sprintf(funcname
, "init%.200s", shortname
);
168 if (strchr(pathname
, '\\') == NULL
&&
169 strchr(pathname
, '/') == NULL
)
171 /* Prefix bare filename with ".\" */
174 _getcwd(pathbuf
, sizeof pathbuf
);
175 if (*p
!= '\0' && p
[1] == ':')
177 sprintf(p
, ".\\%-.255s", pathname
);
180 /* Look for dependent DLLs in directory of pathname first */
181 /* XXX This call doesn't exist in Windows CE */
182 hDLL
= LoadLibraryEx(pathname
, NULL
,
183 LOAD_WITH_ALTERED_SEARCH_PATH
);
186 unsigned int errorCode
;
188 /* Get an error string from Win32 error code */
189 char theInfo
[256]; /* Pointer to error text
191 int theLength
; /* Length of error text */
193 errorCode
= GetLastError();
195 theLength
= FormatMessage(
196 FORMAT_MESSAGE_FROM_SYSTEM
, /* flags */
197 NULL
, /* message source */
198 errorCode
, /* the message (error) ID */
199 0, /* default language environment */
200 (LPTSTR
) theInfo
, /* the buffer */
201 sizeof(theInfo
), /* the buffer size */
202 NULL
); /* no additional format args. */
204 /* Problem: could not get the error message.
205 This should not happen if called correctly. */
206 if (theLength
== 0) {
208 "DLL load failed with error code %d",
212 /* For some reason a \r\n
213 is appended to the text */
214 if (theLength
>= 2 &&
215 theInfo
[theLength
-2] == '\r' &&
216 theInfo
[theLength
-1] == '\n') {
218 theInfo
[theLength
] = '\0';
220 strcpy(errBuf
, "DLL load failed: ");
221 len
= strlen(errBuf
);
222 strncpy(errBuf
+len
, theInfo
,
224 errBuf
[sizeof(errBuf
)-1] = '\0';
226 PyErr_SetString(PyExc_ImportError
, errBuf
);
231 sprintf(buffer
,"python%d%d.dll",
232 PY_MAJOR_VERSION
,PY_MINOR_VERSION
);
233 import_python
= GetPythonImport(hDLL
);
236 strcasecmp(buffer
,import_python
)) {
238 "Module use of %s conflicts "
239 "with this version of Python.",
241 PyErr_SetString(PyExc_ImportError
,buffer
);
246 p
= GetProcAddress(hDLL
, funcname
);
248 #endif /* MS_WIN32 */
253 if (strchr(pathname
, '\\') == NULL
&&
254 strchr(pathname
, '/') == NULL
)
256 /* Prefix bare filename with ".\" */
257 sprintf(pathbuf
, ".\\%-.13s", pathname
);
260 hDLL
= LoadLibrary(pathname
);
261 if (hDLL
< HINSTANCE_ERROR
){
264 "DLL load failed with error code %d", hDLL
);
265 PyErr_SetString(PyExc_ImportError
, errBuf
);
268 p
= GetProcAddress(hDLL
, funcname
);
270 #endif /* MS_WIN16 */