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
},
23 /* Case insensitive string compare, to avoid any dependencies on particular
24 C RTL implementations */
26 static int strcasecmp (char *string1
, char *string2
)
31 first
= tolower(*string1
);
32 second
= tolower(*string2
);
35 } while (first
&& first
== second
);
37 return (first
- second
);
41 /* Function to return the name of the "python" DLL that the supplied module
42 directly imports. Looks through the list of imported modules and
43 returns the first entry that starts with "python" (case sensitive) and
44 is followed by nothing but numbers until the separator (period).
46 Returns a pointer to the import name, or NULL if no matching name was
49 This function parses through the PE header for the module as loaded in
50 memory by the system loader. The PE header is accessed as documented by
51 Microsoft in the MSDN PE and COFF specification (2/99), and handles
52 both PE32 and PE32+. It only worries about the direct import table and
53 not the delay load import table since it's unlikely an extension is
54 going to be delay loading Python (after all, it's already loaded).
56 If any magic values are not found (e.g., the PE header or optional
57 header magic), then this function simply returns NULL. */
59 #define DWORD_AT(mem) (*(DWORD *)(mem))
60 #define WORD_AT(mem) (*(WORD *)(mem))
62 static char *GetPythonImport (HINSTANCE hModule
)
64 unsigned char *dllbase
, *import_data
, *import_name
;
65 DWORD pe_offset
, opt_offset
;
67 int num_dict_off
, import_off
;
69 /* Safety check input */
70 if (hModule
== NULL
) {
74 /* Module instance is also the base load address. First portion of
75 memory is the MS-DOS loader, which holds the offset to the PE
76 header (from the load base) at 0x3C */
77 dllbase
= (unsigned char *)hModule
;
78 pe_offset
= DWORD_AT(dllbase
+ 0x3C);
80 /* The PE signature must be "PE\0\0" */
81 if (memcmp(dllbase
+pe_offset
,"PE\0\0",4)) {
85 /* Following the PE signature is the standard COFF header (20
86 bytes) and then the optional header. The optional header starts
87 with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
88 uses 64-bits for some fields). It might also be 0x107 for a ROM
89 image, but we don't process that here.
91 The optional header ends with a data dictionary that directly
92 points to certain types of data, among them the import entries
93 (in the second table entry). Based on the header type, we
94 determine offsets for the data dictionary count and the entry
95 within the dictionary pointing to the imports. */
97 opt_offset
= pe_offset
+ 4 + 20;
98 opt_magic
= WORD_AT(dllbase
+opt_offset
);
99 if (opt_magic
== 0x10B) {
103 } else if (opt_magic
== 0x20B) {
112 /* Now if an import table exists, offset to it and walk the list of
113 imports. The import table is an array (ending when an entry has
114 empty values) of structures (20 bytes each), which contains (at
115 offset 12) a relative address (to the module base) at which a
116 string constant holding the import name is located. */
118 if (DWORD_AT(dllbase
+ opt_offset
+ num_dict_off
) >= 2) {
119 import_data
= dllbase
+ DWORD_AT(dllbase
+
122 while (DWORD_AT(import_data
)) {
123 import_name
= dllbase
+ DWORD_AT(import_data
+12);
124 if (strlen(import_name
) >= 6 &&
125 !strncmp(import_name
,"python",6)) {
128 /* Ensure python prefix is followed only
129 by numbers to the end of the basename */
130 pch
= import_name
+ 6;
131 while (*pch
&& *pch
!= '.') {
132 if (*pch
>= '0' && *pch
<= '9') {
141 /* Found it - return the name */
153 dl_funcptr
_PyImport_GetDynLoadFunc(const char *fqname
, const char *shortname
,
154 const char *pathname
, FILE *fp
)
157 char funcname
[258], *import_python
;
159 PyOS_snprintf(funcname
, sizeof(funcname
), "init%.200s", shortname
);
162 HINSTANCE hDLL
= NULL
;
165 /* We use LoadLibraryEx so Windows looks for dependent DLLs
166 in directory of pathname first. However, Windows95
167 can sometimes not work correctly unless the absolute
168 path is used. If GetFullPathName() fails, the LoadLibrary
169 will certainly fail too, so use its error code */
170 if (GetFullPathName(pathname
,
174 /* XXX This call doesn't exist in Windows CE */
175 hDLL
= LoadLibraryEx(pathname
, NULL
,
176 LOAD_WITH_ALTERED_SEARCH_PATH
);
179 unsigned int errorCode
;
181 /* Get an error string from Win32 error code */
182 char theInfo
[256]; /* Pointer to error text
184 int theLength
; /* Length of error text */
186 errorCode
= GetLastError();
188 theLength
= FormatMessage(
189 FORMAT_MESSAGE_FROM_SYSTEM
, /* flags */
190 NULL
, /* message source */
191 errorCode
, /* the message (error) ID */
192 0, /* default language environment */
193 (LPTSTR
) theInfo
, /* the buffer */
194 sizeof(theInfo
), /* the buffer size */
195 NULL
); /* no additional format args. */
197 /* Problem: could not get the error message.
198 This should not happen if called correctly. */
199 if (theLength
== 0) {
200 PyOS_snprintf(errBuf
, sizeof(errBuf
),
201 "DLL load failed with error code %d",
205 /* For some reason a \r\n
206 is appended to the text */
207 if (theLength
>= 2 &&
208 theInfo
[theLength
-2] == '\r' &&
209 theInfo
[theLength
-1] == '\n') {
211 theInfo
[theLength
] = '\0';
213 strcpy(errBuf
, "DLL load failed: ");
214 len
= strlen(errBuf
);
215 strncpy(errBuf
+len
, theInfo
,
217 errBuf
[sizeof(errBuf
)-1] = '\0';
219 PyErr_SetString(PyExc_ImportError
, errBuf
);
224 PyOS_snprintf(buffer
, sizeof(buffer
), "python%d%d.dll",
225 PY_MAJOR_VERSION
,PY_MINOR_VERSION
);
226 import_python
= GetPythonImport(hDLL
);
229 strcasecmp(buffer
,import_python
)) {
230 PyOS_snprintf(buffer
, sizeof(buffer
),
231 "Module use of %.150s conflicts "
232 "with this version of Python.",
234 PyErr_SetString(PyExc_ImportError
,buffer
);
239 p
= GetProcAddress(hDLL
, funcname
);