blob: 2fe44b68593441f515a8a8a701a641d2feff290c [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 Rossum6dbd1901996-08-21 15:03:37 +000017FILE *PyWin_FindRegisteredModule( const char *moduleName, struct filedescr **ppFileDesc, char *pathBuf, int pathLen)
18{
Guido van Rossum271f9771997-09-29 23:39:31 +000019 char *moduleKey;
Guido van Rossum271f9771997-09-29 23:39:31 +000020 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
21 const char keySuffix[] = "\\Modules\\";
Guido van Rossum1c096b71998-05-26 13:53:23 +000022#ifdef _DEBUG
23 // In debugging builds, we _must_ have the debug version registered.
24 const char debugString[] = "\\Debug";
25#else
26 const char debugString[] = "";
27#endif
Guido van Rossum6dbd1901996-08-21 15:03:37 +000028 struct filedescr *fdp = NULL;
29 FILE *fp;
Guido van Rossum4f3cc352000-04-04 22:48:55 +000030 HKEY keyBase = HKEY_LOCAL_MACHINE;
Guido van Rossum1c096b71998-05-26 13:53:23 +000031 int modNameSize;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000032 long regStat;
Guido van Rossum271f9771997-09-29 23:39:31 +000033
Guido van Rossum1c096b71998-05-26 13:53:23 +000034 // Calculate the size for the sprintf buffer.
35 // Get the size of the chars only, plus 1 NULL.
36 int bufSize = sizeof(keyPrefix)-1 + strlen(PyWin_DLLVersionString) + sizeof(keySuffix) + strlen(moduleName) + sizeof(debugString) - 1;
Guido van Rossum271f9771997-09-29 23:39:31 +000037 // alloca == no free required, but memory only local to fn, also no heap fragmentation!
Guido van Rossum1c096b71998-05-26 13:53:23 +000038 moduleKey = alloca(bufSize);
39 sprintf(moduleKey, "Software\\Python\\PythonCore\\%s\\Modules\\%s%s", PyWin_DLLVersionString, moduleName, debugString);
Guido van Rossum271f9771997-09-29 23:39:31 +000040
Guido van Rossum94555471999-01-14 22:40:30 +000041 modNameSize = pathLen;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000042 regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
43 if (regStat!=ERROR_SUCCESS)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000044 return NULL;
45 // use the file extension to locate the type entry.
Guido van Rossum22822d71997-05-08 23:43:52 +000046 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum6dbd1901996-08-21 15:03:37 +000047 int extLen=strlen(fdp->suffix);
48 if (modNameSize>extLen && strnicmp(pathBuf+(modNameSize-extLen-1),fdp->suffix,extLen)==0)
49 break;
50 }
51 if (fdp->suffix==NULL)
52 return NULL;
53 fp = fopen(pathBuf, fdp->mode);
54 if (fp != NULL)
55 *ppFileDesc = fdp;
56 return fp;
57}
Guido van Rossum1c096b71998-05-26 13:53:23 +000058