Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 1 | /******************************************************************** |
| 2 | |
Guido van Rossum | eaf9b6c | 1996-08-22 00:06:59 +0000 | [diff] [blame] | 3 | import_nt.c |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 4 | |
| 5 | Win32 specific import code. |
| 6 | |
| 7 | */ |
| 8 | |
Guido van Rossum | 7688bba | 1997-05-05 21:45:44 +0000 | [diff] [blame] | 9 | #include "Python.h" |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 10 | #include "osdefs.h" |
| 11 | #include <windows.h> |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 12 | #include "importdl.h" |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 13 | #include "malloc.h" // for alloca |
| 14 | |
| 15 | extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup. |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 0e6ae93 | 1997-08-13 19:53:11 +0000 | [diff] [blame] | 17 | /* Return whether this is Win32s, i.e., Win32 API on Win 3.1(1). |
| 18 | This function is exported! */ |
| 19 | |
| 20 | BOOL PyWin_IsWin32s() |
| 21 | { |
| 22 | static BOOL bIsWin32s = -1; /* flag as "not yet looked" */ |
| 23 | |
| 24 | if (bIsWin32s == -1) { |
| 25 | OSVERSIONINFO ver; |
| 26 | ver.dwOSVersionInfoSize = sizeof(ver); |
| 27 | GetVersionEx(&ver); |
| 28 | bIsWin32s = ver.dwPlatformId == VER_PLATFORM_WIN32s; |
| 29 | } |
| 30 | return bIsWin32s; |
| 31 | } |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 32 | |
| 33 | FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen) |
| 34 | { |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 35 | char *moduleKey; |
| 36 | char *pos; |
| 37 | const char keyPrefix[] = "Software\\Python\\PythonCore\\"; |
| 38 | const char keySuffix[] = "\\Modules\\"; |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 39 | struct filedescr *fdp = NULL; |
| 40 | FILE *fp; |
| 41 | int modNameSize = pathLen; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 42 | int versionLen, moduleLen; |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 43 | HKEY keyBase = PyWin_IsWin32s() ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 44 | |
| 45 | // conceptually, this code is setting up: |
| 46 | // sprintf(buf, "Software\\Python\\PythonCore\\%s\\Modules\\%s", PyWin_DLLVersionString, moduleName); |
| 47 | // the sprintf would be clearer, but slower and less "length-safe" |
| 48 | versionLen = strlen(PyWin_DLLVersionString); |
| 49 | moduleLen = strlen(moduleName); |
| 50 | // alloca == no free required, but memory only local to fn, also no heap fragmentation! |
| 51 | moduleKey = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix) + moduleLen); // chars only, plus 1 NULL. |
| 52 | pos = moduleKey; |
| 53 | memcpy(pos, keyPrefix, sizeof(keyPrefix)-1); |
| 54 | pos += sizeof(keyPrefix)-1; |
| 55 | memcpy(pos, PyWin_DLLVersionString, versionLen); |
| 56 | pos +=versionLen; |
| 57 | memcpy(pos, keySuffix, sizeof(keySuffix)); |
| 58 | pos += sizeof(keySuffix)-1; |
| 59 | memcpy(pos, moduleName, moduleLen+1); // NULL comes with this one! |
| 60 | |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 61 | if (RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize)!=ERROR_SUCCESS) |
| 62 | return NULL; |
| 63 | // use the file extension to locate the type entry. |
Guido van Rossum | 22822d7 | 1997-05-08 23:43:52 +0000 | [diff] [blame] | 64 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Guido van Rossum | 6dbd190 | 1996-08-21 15:03:37 +0000 | [diff] [blame] | 65 | int extLen=strlen(fdp->suffix); |
| 66 | if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0) |
| 67 | break; |
| 68 | } |
| 69 | if (fdp->suffix==NULL) |
| 70 | return NULL; |
| 71 | fp = fopen(pathBuf, fdp->mode); |
| 72 | if (fp != NULL) |
| 73 | *ppFileDesc = fdp; |
| 74 | return fp; |
| 75 | } |