Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 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 | |
| 19 | const 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 Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 31 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 32 | const char *pathname, FILE *fp) |
| 33 | { |
| 34 | dl_funcptr p; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 35 | char funcname[258]; |
| 36 | |
| 37 | sprintf(funcname, "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 38 | |
| 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 Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 86 | size_t len; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 87 | /* 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 | } |