home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Python / dynload_win.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  5KB  |  153 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Support for dynamic loading of extension modules */
  33.  
  34. #include <windows.h>
  35. #include <direct.h>
  36.  
  37. #include "Python.h"
  38. #include "importdl.h"
  39.  
  40. const struct filedescr _PyImport_DynLoadFiletab[] = {
  41. #ifdef _DEBUG
  42.     {"_d.pyd", "rb", C_EXTENSION},
  43.     {"_d.dll", "rb", C_EXTENSION},
  44. #else
  45.     {".pyd", "rb", C_EXTENSION},
  46.     {".dll", "rb", C_EXTENSION},
  47. #endif
  48.     {0, 0}
  49. };
  50.  
  51.  
  52. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  53.                     const char *pathname, FILE *fp)
  54. {
  55.     dl_funcptr p;
  56.     char funcname[258];
  57.  
  58.     sprintf(funcname, "init%.200s", shortname);
  59.  
  60. #ifdef MS_WIN32
  61.     {
  62.         HINSTANCE hDLL;
  63.         char pathbuf[260];
  64.         if (strchr(pathname, '\\') == NULL &&
  65.             strchr(pathname, '/') == NULL)
  66.         {
  67.             /* Prefix bare filename with ".\" */
  68.             char *p = pathbuf;
  69.             *p = '\0';
  70.             _getcwd(pathbuf, sizeof pathbuf);
  71.             if (*p != '\0' && p[1] == ':')
  72.                 p += 2;
  73.             sprintf(p, ".\\%-.255s", pathname);
  74.             pathname = pathbuf;
  75.         }
  76.         /* Look for dependent DLLs in directory of pathname first */
  77.         /* XXX This call doesn't exist in Windows CE */
  78.         hDLL = LoadLibraryEx(pathname, NULL,
  79.                      LOAD_WITH_ALTERED_SEARCH_PATH);
  80.         if (hDLL==NULL){
  81.             char errBuf[256];
  82.             unsigned int errorCode;
  83.  
  84.             /* Get an error string from Win32 error code */
  85.             char theInfo[256]; /* Pointer to error text
  86.                           from system */
  87.             int theLength; /* Length of error text */
  88.  
  89.             errorCode = GetLastError();
  90.  
  91.             theLength = FormatMessage(
  92.                 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
  93.                 NULL, /* message source */
  94.                 errorCode, /* the message (error) ID */
  95.                 0, /* default language environment */
  96.                 (LPTSTR) theInfo, /* the buffer */
  97.                 sizeof(theInfo), /* the buffer size */
  98.                 NULL); /* no additional format args. */
  99.  
  100.             /* Problem: could not get the error message.
  101.                This should not happen if called correctly. */
  102.             if (theLength == 0) {
  103.                 sprintf(errBuf,
  104.                     "DLL load failed with error code %d",
  105.                     errorCode);
  106.             } else {
  107.                 int len;
  108.                 /* For some reason a \r\n
  109.                    is appended to the text */
  110.                 if (theLength >= 2 &&
  111.                     theInfo[theLength-2] == '\r' &&
  112.                     theInfo[theLength-1] == '\n') {
  113.                     theLength -= 2;
  114.                     theInfo[theLength] = '\0';
  115.                 }
  116.                 strcpy(errBuf, "DLL load failed: ");
  117.                 len = strlen(errBuf);
  118.                 strncpy(errBuf+len, theInfo,
  119.                     sizeof(errBuf)-len);
  120.                 errBuf[sizeof(errBuf)-1] = '\0';
  121.             }
  122.             PyErr_SetString(PyExc_ImportError, errBuf);
  123.         return NULL;
  124.         }
  125.         p = GetProcAddress(hDLL, funcname);
  126.     }
  127. #endif /* MS_WIN32 */
  128. #ifdef MS_WIN16
  129.     {
  130.         HINSTANCE hDLL;
  131.         char pathbuf[16];
  132.         if (strchr(pathname, '\\') == NULL &&
  133.             strchr(pathname, '/') == NULL)
  134.         {
  135.             /* Prefix bare filename with ".\" */
  136.             sprintf(pathbuf, ".\\%-.13s", pathname);
  137.             pathname = pathbuf;
  138.         }
  139.         hDLL = LoadLibrary(pathname);
  140.         if (hDLL < HINSTANCE_ERROR){
  141.             char errBuf[256];
  142.             sprintf(errBuf,
  143.                 "DLL load failed with error code %d", hDLL);
  144.             PyErr_SetString(PyExc_ImportError, errBuf);
  145.             return NULL;
  146.         }
  147.         p = GetProcAddress(hDLL, funcname);
  148.     }
  149. #endif /* MS_WIN16 */
  150.  
  151.     return p;
  152. }
  153.