blob: a08e417aced67c003b3795ba708f83cb64631968 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include <windows.h>
5#include <direct.h>
6
7#include "Python.h"
8#include "importdl.h"
9
10const struct filedescr _PyImport_DynLoadFiletab[] = {
11#ifdef _DEBUG
12 {"_d.pyd", "rb", C_EXTENSION},
13 {"_d.dll", "rb", C_EXTENSION},
14#else
15 {".pyd", "rb", C_EXTENSION},
16 {".dll", "rb", C_EXTENSION},
17#endif
18 {0, 0}
19};
20
21
Guido van Rossum96a8fb71999-12-22 14:09:35 +000022dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000023 const char *pathname, FILE *fp)
24{
25 dl_funcptr p;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000026 char funcname[258];
27
28 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000029
30#ifdef MS_WIN32
31 {
32 HINSTANCE hDLL;
33 char pathbuf[260];
34 if (strchr(pathname, '\\') == NULL &&
35 strchr(pathname, '/') == NULL)
36 {
37 /* Prefix bare filename with ".\" */
38 char *p = pathbuf;
39 *p = '\0';
40 _getcwd(pathbuf, sizeof pathbuf);
41 if (*p != '\0' && p[1] == ':')
42 p += 2;
43 sprintf(p, ".\\%-.255s", pathname);
44 pathname = pathbuf;
45 }
46 /* Look for dependent DLLs in directory of pathname first */
47 /* XXX This call doesn't exist in Windows CE */
48 hDLL = LoadLibraryEx(pathname, NULL,
49 LOAD_WITH_ALTERED_SEARCH_PATH);
50 if (hDLL==NULL){
51 char errBuf[256];
52 unsigned int errorCode;
53
54 /* Get an error string from Win32 error code */
55 char theInfo[256]; /* Pointer to error text
56 from system */
57 int theLength; /* Length of error text */
58
59 errorCode = GetLastError();
60
61 theLength = FormatMessage(
62 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
63 NULL, /* message source */
64 errorCode, /* the message (error) ID */
65 0, /* default language environment */
66 (LPTSTR) theInfo, /* the buffer */
67 sizeof(theInfo), /* the buffer size */
68 NULL); /* no additional format args. */
69
70 /* Problem: could not get the error message.
71 This should not happen if called correctly. */
72 if (theLength == 0) {
73 sprintf(errBuf,
74 "DLL load failed with error code %d",
75 errorCode);
76 } else {
Guido van Rossum582acec2000-06-28 22:07:35 +000077 size_t len;
Guido van Rossum22a1d361999-12-20 21:18:49 +000078 /* For some reason a \r\n
79 is appended to the text */
80 if (theLength >= 2 &&
81 theInfo[theLength-2] == '\r' &&
82 theInfo[theLength-1] == '\n') {
83 theLength -= 2;
84 theInfo[theLength] = '\0';
85 }
86 strcpy(errBuf, "DLL load failed: ");
87 len = strlen(errBuf);
88 strncpy(errBuf+len, theInfo,
89 sizeof(errBuf)-len);
90 errBuf[sizeof(errBuf)-1] = '\0';
91 }
92 PyErr_SetString(PyExc_ImportError, errBuf);
93 return NULL;
94 }
95 p = GetProcAddress(hDLL, funcname);
96 }
97#endif /* MS_WIN32 */
98#ifdef MS_WIN16
99 {
100 HINSTANCE hDLL;
101 char pathbuf[16];
102 if (strchr(pathname, '\\') == NULL &&
103 strchr(pathname, '/') == NULL)
104 {
105 /* Prefix bare filename with ".\" */
106 sprintf(pathbuf, ".\\%-.13s", pathname);
107 pathname = pathbuf;
108 }
109 hDLL = LoadLibrary(pathname);
110 if (hDLL < HINSTANCE_ERROR){
111 char errBuf[256];
112 sprintf(errBuf,
113 "DLL load failed with error code %d", hDLL);
114 PyErr_SetString(PyExc_ImportError, errBuf);
115 return NULL;
116 }
117 p = GetProcAddress(hDLL, funcname);
118 }
119#endif /* MS_WIN16 */
120
121 return p;
122}