¿Cómo importaría un winDLL a python y podría usar todas sus funciones? Solo necesita dobles y cadenas.Python importar dll
Respuesta
Has etiquetado la pregunta ctypes y parece que ya sabes la respuesta.
El ctypes tutorial es excelente. Una vez que haya leído y entendido que podrá hacerlo fácilmente.
Por ejemplo:
>>> from ctypes import *
>>> windll.kernel32.GetModuleHandleW(0)
486539264
Y un ejemplo de mi propio código:
lib = ctypes.WinDLL('mylibrary.dll')
#lib = ctypes.WinDLL('full/path/to/mylibrary.dll')
func = lib['myFunc']#my func is double myFunc(double);
func.restype = ctypes.c_double
value = func(ctypes.c_double(42.0))
Bueno, sabía que necesitaba ctypes pero no sabía cómo usarlos. :) ¡También, muy buen enlace! La documentación de Python parece ser solo una buena referencia, pero no un aprendizaje real. ¡Gracias una tonelada! – pajm
¡Espera! Creo que pasé por alto tu código. Después de mirar el tutorial, parece que solo demuestra cómo cargar archivos DLL de Windows. Necesito cargar un archivo DLL personalizado. ¿Cómo haría esto? – pajm
@Patrick He agregado otro ejemplo. Pero todo está allí en el tutorial. No hay diferencia teórica entre llamar a su propia DLL y una DLL de Windows. –
Use Cython, ambos para acceder a las DLL y generar enlaces de Python para ellas.
Quiero poner mi experiencia. En primer lugar, a pesar de todo el arduo trabajo que me llevó a juntar todas las piezas, importar un dll C# es fácil. La forma en que lo hice es:
1) instala este paquete Nuget (no soy propietario, es simplemente muy útil) con el fin de construir un archivo DLL no administrado: https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
2) Su código C# DLL es igual esto:
using System;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
public class MyClassName
{
[DllExport("MyFunctionName",CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string MyFunctionName([MarshalAs(UnmanagedType.LPWStr)] string iString)
{
return "hello world i'm " + iString
}
}
3) Su código Python es así:
import ctypes
#Here you load the dll into python
MyDllObject = ctypes.cdll.LoadLibrary("C:\\My\\Path\\To\\MyDLL.dll")
#it's important to assing the function to an object
MyFunctionObject = MyDllObject.MyFunctionName
#define the types that your C# function return
MyFunctionObject.restype = ctypes.c_wchar_p
#define the types that your C# function will use as arguments
MyFunctionObject.argtypes = [ctypes.c_wchar_p]
#That's it now you can test it
print(MyFunctionObject("Python Message"))
- 1. Python importar
- 2. ¿Puede Ruby importar un dll de .NET?
- 3. Importar una DLL con C++ (Win32)
- 4. Python - importar en if
- 5. importar gtk/glib produce ImportError: DLL carga fallida
- 6. NumPy ImportError en python - Dll carga fallida
- 7. dinámicamente importar módulo de Python
- 8. Cómo importar 'GDB' en Python
- 9. Importar numpy con python 2.6
- 10. importar una matriz en python
- 11. cómo importar matplotlib en python
- 12. Error de importación de Python "Error al cargar DLL" | Python
- 13. Boost Python: Problemas para importar un módulo
- 14. Pregunta sobre exportar/importar DLL y Extern en Windows
- 15. Cómo cargar un dll de C# en python?
- 16. Python: importar el paquete que contiene
- 17. ImportError al importar Tkinter en Python
- 18. Cómo des-importar un módulo de Python?
- 19. Importar lib de Python en C#
- 20. cómo importar numpy en Python Shell
- 21. ¿Es posible sobrecargar/importar en Python?
- 22. ¿Qué es importar gv en python?
- 23. problemas al importar el módulo python
- 24. No puedo importar Numpy en Python
- 25. no puede importar SQLite con Python 2.6
- 26. ¿Es posible importar un archivo python compilado?
- 27. ¿Qué significa "importar seguro" en Python?
- 28. Importar la función C++ al programa Python
- 29. Python 2.7 no puede importar PyQt4
- 30. no se puede importar gdal en python?
¿Qué tienes hasta el momento, y cómo no funciona? –
¿Duplicar con esta pregunta? http://stackoverflow.com/questions/252417/how-can-i-use-a-dll-from-python – payne