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