py2exe to generate dlls?

后端 未结 5 1381
暗喜
暗喜 2020-12-14 10:58

Is there a way using py2exe or some other method to generate dll files instead of exe files?

I would want to basically create a normal win32 dll with normal function

相关标签:
5条回答
  • 2020-12-14 11:29

    For posterity, I was able to use Elmer to successfully generate a usable DLL recently. Their site has an example of building a DLL wrapper that loads python code. It is pretty cool because you can change the python code on the fly to change the DLL behavior for debugging.

    Unfortunately, for me, I wanted a portable DLL that would work without installing python. That part didn't didn't quite work out of the box. Rather than repeating all the steps, here is a link to the answer with the steps I took: https://stackoverflow.com/a/24811840/3841168. I did have to distribute python27.dll, elmer.dll and a couple of .pyd's along with my .dll; an appropriated .net runtime was also needed since the python27.dll is not usually statically linked. There may be some way around including a boatload of dll's, but I didn't mind distributing multiple DLLs, so I didn't dig into it too much.

    0 讨论(0)
  • 2020-12-14 11:31

    I doubt that py2exe does this, as it's architectured around providing a bootstrapping .exe that rolls out the python interpreter and runs it.

    But why not just embed Python in C code, and compile that code as a DLL?

    0 讨论(0)
  • 2020-12-14 11:33

    It looks like it is possible to generate a COM DLL from py2exe:

    http://www.py2exe.org/index.cgi/Py2exeAndCtypesComDllServer

      23     my_com_server_target = Target(
      24     description = "my com server",
      25     # use module name for ctypes.com dll server
      26     modules = ["dir.my_com_server"],
      27     # the following line embeds the typelib within the dll
      28     other_resources = [("TYPELIB", 1, open(r"dir\my_com_server.tlb", "rb").read())],
      29     # we only want the inproc (dll) server
      30     create_exe = False
      31     )
    
    0 讨论(0)
  • 2020-12-14 11:39

    I think you could solve this by doing some hacking:

    • Take a look at the zipextimporter module in py2exe . It helps with importing pyd-files from a zip.
    • Using that, you might be able to load py2exe's output file in your own app/dll using raw python-api. (Use boost::python if you can and want)
    • And, since py2exe's outputfile is a zip, you could attach it at the end of your dll, making the whole thing even more integrated. (Old trick that works with jar-files too.)

    Not tested, but I think the theory is sound.

    Essentially, you reimplement py2exe's output executable's main() in your dll.

    0 讨论(0)
  • 2020-12-14 11:49

    I am not aware of py2exe being able to do that, as I believe that it does not actually make object symbols out of your Python code, but just embeds the compiled byte-code in an executable with the Python runtime).

    Creating a native library may require a bit more work (to define the C/C++ interface to things) with the Python-C API. It may be somewhat easier using Elmer for that.

    0 讨论(0)
提交回复
热议问题