Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top