blob: 9e68cac35d9a3888359010faa2c9587d5b3ba2bf [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
52dl_funcptr _PyImport_GetDynLoadFunc(const char *name, const char *funcname,
53 const char *pathname, FILE *fp)
54{
55 dl_funcptr p;
56
57#ifdef MS_WIN32
58 {
59 HINSTANCE hDLL;
60 char pathbuf[260];
61 if (strchr(pathname, '\\') == NULL &&
62 strchr(pathname, '/') == NULL)
63 {
64 /* Prefix bare filename with ".\" */
65 char *p = pathbuf;
66 *p = '\0';
67 _getcwd(pathbuf, sizeof pathbuf);
68 if (*p != '\0' && p[1] == ':')
69 p += 2;
70 sprintf(p, ".\\%-.255s", pathname);
71 pathname = pathbuf;
72 }
73 /* Look for dependent DLLs in directory of pathname first */
74 /* XXX This call doesn't exist in Windows CE */
75 hDLL = LoadLibraryEx(pathname, NULL,
76 LOAD_WITH_ALTERED_SEARCH_PATH);
77 if (hDLL==NULL){
78 char errBuf[256];
79 unsigned int errorCode;
80
81 /* Get an error string from Win32 error code */
82 char theInfo[256]; /* Pointer to error text
83 from system */
84 int theLength; /* Length of error text */
85
86 errorCode = GetLastError();
87
88 theLength = FormatMessage(
89 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
90 NULL, /* message source */
91 errorCode, /* the message (error) ID */
92 0, /* default language environment */
93 (LPTSTR) theInfo, /* the buffer */
94 sizeof(theInfo), /* the buffer size */
95 NULL); /* no additional format args. */
96
97 /* Problem: could not get the error message.
98 This should not happen if called correctly. */
99 if (theLength == 0) {
100 sprintf(errBuf,
101 "DLL load failed with error code %d",
102 errorCode);
103 } else {
104 int len;
105 /* For some reason a \r\n
106 is appended to the text */
107 if (theLength >= 2 &&
108 theInfo[theLength-2] == '\r' &&
109 theInfo[theLength-1] == '\n') {
110 theLength -= 2;
111 theInfo[theLength] = '\0';
112 }
113 strcpy(errBuf, "DLL load failed: ");
114 len = strlen(errBuf);
115 strncpy(errBuf+len, theInfo,
116 sizeof(errBuf)-len);
117 errBuf[sizeof(errBuf)-1] = '\0';
118 }
119 PyErr_SetString(PyExc_ImportError, errBuf);
120 return NULL;
121 }
122 p = GetProcAddress(hDLL, funcname);
123 }
124#endif /* MS_WIN32 */
125#ifdef MS_WIN16
126 {
127 HINSTANCE hDLL;
128 char pathbuf[16];
129 if (strchr(pathname, '\\') == NULL &&
130 strchr(pathname, '/') == NULL)
131 {
132 /* Prefix bare filename with ".\" */
133 sprintf(pathbuf, ".\\%-.13s", pathname);
134 pathname = pathbuf;
135 }
136 hDLL = LoadLibrary(pathname);
137 if (hDLL < HINSTANCE_ERROR){
138 char errBuf[256];
139 sprintf(errBuf,
140 "DLL load failed with error code %d", hDLL);
141 PyErr_SetString(PyExc_ImportError, errBuf);
142 return NULL;
143 }
144 p = GetProcAddress(hDLL, funcname);
145 }
146#endif /* MS_WIN16 */
147
148 return p;
149}