blob: 17696976ca456fa9b9954a1a5d9ab2e46a9fc8df [file] [log] [blame]
Guido van Rossum6dbd1901996-08-21 15:03:37 +00001/********************************************************************
2
Guido van Rossumeaf9b6c1996-08-22 00:06:59 +00003 import_nt.c
Guido van Rossum6dbd1901996-08-21 15:03:37 +00004
5 Win32 specific import code.
6
7*/
8
Guido van Rossum7688bba1997-05-05 21:45:44 +00009#include "Python.h"
Guido van Rossum6dbd1901996-08-21 15:03:37 +000010#include "osdefs.h"
11#include <windows.h>
Guido van Rossum6dbd1901996-08-21 15:03:37 +000012#include "importdl.h"
Guido van Rossum271f9771997-09-29 23:39:31 +000013#include "malloc.h" // for alloca
14
15extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
Guido van Rossum6dbd1901996-08-21 15:03:37 +000016
Guido van Rossum0e6ae931997-08-13 19:53:11 +000017/* Return whether this is Win32s, i.e., Win32 API on Win 3.1(1).
18 This function is exported! */
19
20BOOL 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 Rossum6dbd1901996-08-21 15:03:37 +000032
33FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen)
34{
Guido van Rossum271f9771997-09-29 23:39:31 +000035 char *moduleKey;
Guido van Rossum271f9771997-09-29 23:39:31 +000036 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
37 const char keySuffix[] = "\\Modules\\";
Guido van Rossum1c096b71998-05-26 13:53:23 +000038#ifdef _DEBUG
39 // In debugging builds, we _must_ have the debug version registered.
40 const char debugString[] = "\\Debug";
41#else
42 const char debugString[] = "";
43#endif
Guido van Rossum6dbd1901996-08-21 15:03:37 +000044 struct filedescr *fdp = NULL;
45 FILE *fp;
Guido van Rossum6dbd1901996-08-21 15:03:37 +000046 HKEY keyBase = PyWin_IsWin32s() ? HKEY_CLASSES_ROOT : HKEY_LOCAL_MACHINE;
Guido van Rossum1c096b71998-05-26 13:53:23 +000047 int modNameSize;
Guido van Rossum271f9771997-09-29 23:39:31 +000048
Guido van Rossum1c096b71998-05-26 13:53:23 +000049 // Calculate the size for the sprintf buffer.
50 // Get the size of the chars only, plus 1 NULL.
51 int bufSize = sizeof(keyPrefix)-1 + strlen(PyWin_DLLVersionString) + sizeof(keySuffix) + strlen(moduleName) + sizeof(debugString) - 1;
Guido van Rossum271f9771997-09-29 23:39:31 +000052 // alloca == no free required, but memory only local to fn, also no heap fragmentation!
Guido van Rossum1c096b71998-05-26 13:53:23 +000053 moduleKey = alloca(bufSize);
54 sprintf(moduleKey, "Software\\Python\\PythonCore\\%s\\Modules\\%s%s", PyWin_DLLVersionString, moduleName, debugString);
Guido van Rossum271f9771997-09-29 23:39:31 +000055
Guido van Rossum6dbd1901996-08-21 15:03:37 +000056 if (RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize)!=ERROR_SUCCESS)
57 return NULL;
58 // use the file extension to locate the type entry.
Guido van Rossum22822d71997-05-08 23:43:52 +000059 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum6dbd1901996-08-21 15:03:37 +000060 int extLen=strlen(fdp->suffix);
61 if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
62 break;
63 }
64 if (fdp->suffix==NULL)
65 return NULL;
66 fp = fopen(pathBuf, fdp->mode);
67 if (fp != NULL)
68 *ppFileDesc = fdp;
69 return fp;
70}
Guido van Rossum1c096b71998-05-26 13:53:23 +000071