4

My Windows application embeds Python 2.6 (old I know but that's what we have to work with). It can run basic Python commands but fails trying to execute

import ctypes
ctypes.WinDLL("msvcr90.dll")

I'm getting Error 126 "cannot find DLL". If I plant the DLL where the application can find it, then I'm getting Error 1114 "DLL initialization routine failed".

UPDATED This can be reproduced with this simplest of programs:

#include <math.h>
#include <iostream>
#undef _DEBUG
#include <Python.h>

int main(int argc, char* argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PyRun_SimpleString("import pyreadline\n");
    Py_Finalize();
    std::cout << "Press enter: " << std::endl;
    char c;
    std::cin.read(&c, 1);
    return 0;
}

This fails when compiled with either V9 or v10 toolchain, in x86 and amd64 architectures.

The traceback is as follows:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python26-x86\lib\site-packages\pyreadline\__init__.py", line 9, in <m
odule>
    import unicode_helper, logger, clipboard, lineeditor, modes, console
  File "C:\Python26-x86\lib\site-packages\pyreadline\console\__init__.py", line
14, in <module>
    from console import *
  File "C:\Python26-x86\lib\site-packages\pyreadline\console\console.py", line 6
05, in <module>
    msvcrt = cdll.LoadLibrary(ctypes.util.find_msvcrt())
  File "C:\Python26-x86\Lib\ctypes\__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "C:\Python26-x86\Lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found    
  -- or alternatively --
WindowsError: [Error 1114] A dynamic link library (DLL) initialization routine f
ailed

I know that the DLL that is being loaded is msvcr90.dll because I have inserted print self._name in ctypes.py .

The application runs most Python scripts I need, except those that load pyreadline.

These same scripts run from the installed Python executable with no problem.

What could be the reason of this?

UPDATED 2 Simple LoadLibrary("msvcr90.dll") fails too. I have added the DLL to the application manifest, as recommended in various places on the 'net. This did not help. Here's the manifest as embedded in the executable:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="amd64" publicKeyToken="1fc8b3b9a1e18e3b" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

This manifest and the manifest embedded in python.exe do match, yet python.exe can open the DLL and my application can't. I'm puzzled.

user2393229
  • 394
  • 4
  • 9
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • What version of C/C++ is your Windows application built with? – martineau Nov 24 '13 at 16:58
  • @martineau vs2010 (toolchain v10.0, msvcr100.dll) – n. m. could be an AI Nov 24 '13 at 17:07
  • I think that may be the cause of your problem -- you're effectively trying to create a program that uses two different C/C++ runtime libraries at the same time. Offhand, the only solution I can think of for that old of a version of Python would be to try to rebuild it yourself using VS2010. – martineau Nov 24 '13 at 18:41
  • @martineau There's absolutely no problem with using two runtimes, as long as they don't mix (i.e. you don't allocate memory with one runtime and free with the other). In fact my application can run almost every Python command I need. The only thing it cannot do is load pyreadline, which tries to load msvcr90.dll. – n. m. could be an AI Nov 24 '13 at 19:08
  • @martineau This fails even when compiled with the v90 toolchain, see update. – n. m. could be an AI Nov 25 '13 at 08:34
  • Is Dependency Walker happy with the DLL you "planted"? – manuell Nov 25 '13 at 11:02
  • @manuell yes it is; I hear this library must be declared in the application manifeest, I'm checking this theory right now. – n. m. could be an AI Nov 25 '13 at 11:09
  • May where be some access restrictions? And by the way http://support.microsoft.com/kb/326922/en-us states `For Msvcr80.dll and for Msvcr90.dll, you should install the CRT as Windows side-by-side assemblies`. Is it installed in your setup or just copied? – twil Nov 28 '13 at 17:43
  • @twil It is definitely installed as a side-by-side assembly. `python.exe` is somehow able to load it from the side-by-side cache. I have no idea which access restriction that could be and how to check for it. – n. m. could be an AI Nov 28 '13 at 17:47
  • @cgohlke: I have tried both x86 and amd64, same result. Do you have pyreadline installed? Which version? – n. m. could be an AI Nov 30 '13 at 08:06

2 Answers2

5

Updated!
Problem occurs because system tries to load msvcr90.dll from different sources. First when application starts, then with python script is starting.
To solve the problem you really should place in application a correct manifest file.
I created an added.manifest file in root of project with this content:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b" ></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

Than I set this file in project Properties/Manifest Tool/Input and Outputs/Additional Manifest Files
You has error in your manifest in processorArchitecture="amd64".
Project works fine after rebuilding.

Dima Kurilo
  • 2,206
  • 1
  • 21
  • 27
  • Python.exe can run pyreadline and my program cannot. The bug is most definitely not in pyreadline. – n. m. could be an AI Nov 30 '13 at 22:30
  • I build both x86 and amd64 versions. I've just checked, the x86 version references `processorArchitecture="x86"` and the amd64 version references `processorArchitecture="amd64"`. Still neither version works. – n. m. could be an AI Dec 01 '13 at 07:40
  • Can you try to embeed manifest into an application? Just as I wrote. – Dima Kurilo Dec 01 '13 at 08:01
  • Also you can try to download my test project here: http://www.kurilo.su/stackoverflow/20175129-pytest.7z – Dima Kurilo Dec 01 '13 at 09:00
  • Thanks. I have managed to get the x86 version to work by embedding your exact manifest. The debug version needs the debug CRT as an additional dependency, otherwise it won't start. But amd64 (what I'm really interested in) still doesn't work (I have changed processor architecture in the manifest to amd64). – n. m. could be an AI Dec 01 '13 at 09:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42269/discussion-between-dmitry-kurilo-and-n-m) – Dima Kurilo Dec 01 '13 at 10:42
  • Got it working. I used to add a dependent assembly in the linker tab, instead of a manifest file in the manifest tool tab. Nave no idea why that didn't work, but a separate manifest file does work, thanks. – n. m. could be an AI Dec 01 '13 at 12:44
0

I solved the same error on Windows 10 for another Python application by reinstalling python 2.7.

Pang
  • 9,564
  • 146
  • 81
  • 122
LingYan Meng
  • 699
  • 4
  • 12