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