use embedded python to build a dll

newbie zephyrwings at hotmail.com
Wed Dec 8 08:30:37 CET 2004


Hi all,
I got a problem using the embedded python. I'll be appreciated if some one
can show me the way.

What i want is,
1.build a dll(test.dll/test.lib) which uses the embedded python, for
example, i want to use the 're' module.
2.build a exe(usedll.exe) which uses that dll.
3.use py2exe to eliminate the dependence of the python environment.

here is the cpp file to build test.dll/test.lib

**query.py is the python script i used. so i import the module 'query'**

====
#include "stdafx.h"
#include <iostream>
#pragma comment(lib, "d:\\\\sdk\\Python23\\libs\\python23.lib")
#include "d:\sdk\python23\include\python.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}


__declspec(dllexport) void __stdcall DoIt()
{
 Py_Initialize();
 PyObject *pM=PyImport_ImportModuleEx("query", NULL, NULL, NULL);
  Py_XDECREF(pM);
 std::cout<<"ok";
 Py_Finalize();
}
====

here is the file to build usedll.exe

----
#include "stdafx.h"
#include "windows.h"

#pragma comment(lib, "..\\Release\\test.lib")

__declspec(dllimport) void __stdcall DoIt();
int main(int argc, _TCHAR* argv[])
{
 DoIt();

 return 0;
}


----

i use setup.py file like this,

from distutils.core import setup
import py2exe
setup()

and then
python setup.py py2exe --includes query --packages encodings

and i got a bunch of files.

Then I put usedll.exe test.dll test.lib query.py and all of these files
generated by py2exe in the same directory. I run the usedll.exe, then I got
a run time error 'this application has requested the runtime to terminate it
in an unusual way...'

but if i don't use those files generated by py2exe, i.e. i use the python
environment on my machine, every thing is fine.

I can not figure out where is the problem. Could anyone give me a hint?





More information about the Python-list mailing list