BOOL _LoadPythonDLL(HMODULE hmod) { HRSRC hrsrc; char *pBaseAddress; int size; if (!dirname[0]) calc_dirname(hmod); // Try to locate pythonxy.dll as resource in the exe hrsrc = FindResource(hmod, MAKEINTRESOURCE(1), PYTHONDLL); if (hrsrc) { HGLOBAL hgbl = LoadResource(hmod, hrsrc); if (!_load_python(PYTHONDLL, LockResource(hgbl))) { SystemError(GetLastError(), "Could not load python dll"); return FALSE; } // dprintf("Loaded pythondll as RESOURCE\n"); return TRUE; } // try to load pythonxy.dll as bytes at the start of the zipfile pBaseAddress = MapExistingFile(libfilename, &size); if (pBaseAddress) { int res = 0; if (0 == strncmp(pBaseAddress, "<pythondll>", 11)) res = _load_python(PYTHONDLL, pBaseAddress + 11 + sizeof(int)); UnmapViewOfFile(pBaseAddress); if (res) { // dprintf("Loaded pythondll as <pythondll> from %s\n", libfilename); return TRUE; } } // try to load pythonxy.dll from the file system { char buffer[_MAX_PATH + _MAX_FNAME + _MAX_EXT]; snprintf(buffer, sizeof(buffer), "%s\\%s", dirname, PYTHONDLL); if (!_load_python(buffer, NULL)) { SystemError(GetLastError(), "LoadLibrary(pythondll) failed"); SystemError(0, buffer); return FALSE; } // dprintf("Loaded pythondll from file %s\n", buffer); } return TRUE; }
BOOL _LocateScript(HMODULE hmod) { HRSRC hrsrc = FindResource(hmod, MAKEINTRESOURCE(1), "PYTHONSCRIPT"); HGLOBAL hgbl; if (!dirname[0]) calc_dirname(hmod); // load the script resource if (!hrsrc) { SystemError(GetLastError(), "Could not locate script resource:"); return FALSE; } hgbl = LoadResource(hmod, hrsrc); if (!hgbl) { SystemError(GetLastError(), "Could not load script resource:"); return FALSE; } p_script_info = (struct scriptinfo *)pScript = LockResource(hgbl); if (!pScript) { SystemError(GetLastError(), "Could not lock script resource:"); return FALSE; } // validate script resource numScriptBytes = p_script_info->data_bytes; pScript += sizeof(struct scriptinfo); if (p_script_info->tag != 0x78563412) { SystemError (0, "Bug: Invalid script resource"); return FALSE; } // let pScript point to the start of the python script resource pScript += strlen(p_script_info->zippath) + 1; // get full pathname of the 'library.zip' file if(p_script_info->zippath[0]) { snprintf(libfilename, sizeof(libfilename), "%s\\%s", dirname, p_script_info->zippath); } else { GetModuleFileName(hmod, libfilename, sizeof(libfilename)); } return TRUE; // success }
int init_with_instance(HMODULE hmod_exe, char *frozen) { int rc = 0; HMODULE hmod_pydll; /* Py_NoSiteFlag = 1; /* Suppress 'import site' */ /* Py_InspectFlag = 1; /* Needed to determine whether to exit at SystemExit */ calc_dirname(hmod_exe); // wprintf(L"modulename %s\n", modulename); // wprintf(L"dirname %s\n", dirname); if (!locate_script(hmod_exe)) { SystemError(-1, "FATAL ERROR: Could not locate script"); // printf("FATAL ERROR locating script\n"); return -1; } hmod_pydll = load_pythondll(); if (hmod_pydll == NULL) { SystemError(-1, "FATAL ERROR: Could not load python library"); // printf("FATAL Error: could not load python library\n"); return -1; } if (PythonLoaded(hmod_pydll) < 0) { SystemError(-1, "FATAL ERROR: Failed to load some Python symbols"); // printf("FATAL Error: failed to load some Python symbols\n"); return -1; } set_vars(hmod_pydll); /* _memimporter contains the magic which allows to load dlls from memory, without unpacking them to the file-system. It is compiled into all the exe-stubs. */ PyImport_AppendInittab("_memimporter", PyInit__memimporter); /* Start the ball rolling. */ Py_SetProgramName(modulename); Py_SetPath(libfilename); Py_Initialize(); /* Set sys.frozen so apps that care can tell. If the caller did pass NULL, sys.frozen will be set to 'True'. If a string is passed this is used as the frozen attribute. run.c passes "console_exe", run_w.c passes "windows_exe", run_dll.c passes "dll" This falls apart when you consider that in some cases, a single process may end up with two py2exe generated apps - but still, we reset frozen to the correct 'current' value for the newly initializing app. */ if (frozen == NULL) PySys_SetObject("frozen", PyBool_FromLong(1)); else { PyObject *o = PyUnicode_FromString(frozen); if (o) { PySys_SetObject("frozen", o); Py_DECREF(o); } } return rc; }