blob: 08a2a895fe38c26f5f620a8bd17d8d7a470b25d2 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Support for dynamic loading of extension modules */
33
34#include <windows.h>
35#include <direct.h>
36
37#include "Python.h"
38#include "importdl.h"
39
40const struct filedescr _PyImport_DynLoadFiletab[] = {
41#ifdef _DEBUG
42 {"_d.pyd", "rb", C_EXTENSION},
43 {"_d.dll", "rb", C_EXTENSION},
44#else
45 {".pyd", "rb", C_EXTENSION},
46 {".dll", "rb", C_EXTENSION},
47#endif
48 {0, 0}
49};
50
51
Guido van Rossum96a8fb71999-12-22 14:09:35 +000052dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000053 const char *pathname, FILE *fp)
54{
55 dl_funcptr p;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000056 char funcname[258];
57
58 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000059
60#ifdef MS_WIN32
61 {
62 HINSTANCE hDLL;
63 char pathbuf[260];
64 if (strchr(pathname, '\\') == NULL &&
65 strchr(pathname, '/') == NULL)
66 {
67 /* Prefix bare filename with ".\" */
68 char *p = pathbuf;
69 *p = '\0';
70 _getcwd(pathbuf, sizeof pathbuf);
71 if (*p != '\0' && p[1] == ':')
72 p += 2;
73 sprintf(p, ".\\%-.255s", pathname);
74 pathname = pathbuf;
75 }
76 /* Look for dependent DLLs in directory of pathname first */
77 /* XXX This call doesn't exist in Windows CE */
78 hDLL = LoadLibraryEx(pathname, NULL,
79 LOAD_WITH_ALTERED_SEARCH_PATH);
80 if (hDLL==NULL){
81 char errBuf[256];
82 unsigned int errorCode;
83
84 /* Get an error string from Win32 error code */
85 char theInfo[256]; /* Pointer to error text
86 from system */
87 int theLength; /* Length of error text */
88
89 errorCode = GetLastError();
90
91 theLength = FormatMessage(
92 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
93 NULL, /* message source */
94 errorCode, /* the message (error) ID */
95 0, /* default language environment */
96 (LPTSTR) theInfo, /* the buffer */
97 sizeof(theInfo), /* the buffer size */
98 NULL); /* no additional format args. */
99
100 /* Problem: could not get the error message.
101 This should not happen if called correctly. */
102 if (theLength == 0) {
103 sprintf(errBuf,
104 "DLL load failed with error code %d",
105 errorCode);
106 } else {
107 int len;
108 /* For some reason a \r\n
109 is appended to the text */
110 if (theLength >= 2 &&
111 theInfo[theLength-2] == '\r' &&
112 theInfo[theLength-1] == '\n') {
113 theLength -= 2;
114 theInfo[theLength] = '\0';
115 }
116 strcpy(errBuf, "DLL load failed: ");
117 len = strlen(errBuf);
118 strncpy(errBuf+len, theInfo,
119 sizeof(errBuf)-len);
120 errBuf[sizeof(errBuf)-1] = '\0';
121 }
122 PyErr_SetString(PyExc_ImportError, errBuf);
123 return NULL;
124 }
125 p = GetProcAddress(hDLL, funcname);
126 }
127#endif /* MS_WIN32 */
128#ifdef MS_WIN16
129 {
130 HINSTANCE hDLL;
131 char pathbuf[16];
132 if (strchr(pathname, '\\') == NULL &&
133 strchr(pathname, '/') == NULL)
134 {
135 /* Prefix bare filename with ".\" */
136 sprintf(pathbuf, ".\\%-.13s", pathname);
137 pathname = pathbuf;
138 }
139 hDLL = LoadLibrary(pathname);
140 if (hDLL < HINSTANCE_ERROR){
141 char errBuf[256];
142 sprintf(errBuf,
143 "DLL load failed with error code %d", hDLL);
144 PyErr_SetString(PyExc_ImportError, errBuf);
145 return NULL;
146 }
147 p = GetProcAddress(hDLL, funcname);
148 }
149#endif /* MS_WIN16 */
150
151 return p;
152}