Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import dll

Tags:

python

dll

ctypes

How would I import a winDLL into python and be able to use all of its functions? It only needs doubles and strings.

like image 848
pajm Avatar asked Mar 09 '11 23:03

pajm


People also ask

Can Python use DLL files?

New in Python version 2.5 is the ctypes, a foreign function library. It provides C-compatible data types and allows calling functions in DLLs or shared libraries. Using the ctypes module in Python allows ArcObjects code written in C++ to be used in a geoprocessing script tool.

What are DLL files in Python?

A DLL is a Dynamic-Linked Library. DLLs are kind of like executables. They can contain code, data, and other resources.

Why is it showing DLL error in Python?

The Error Loading Python DLL error typically occurs when the user tries to open the desktop version of Google Drive or several seconds after starting the syncing procedure. In most cases, the error window points towards a Dynamic Link Library (DLL) named python27. dll.


2 Answers

You've tagged the question ctypes and so it sounds like you already know the answer.

The ctypes tutorial is excellent. Once you've read and understood that you'll be able to do it easily.

For example:

>>> from ctypes import *
>>> windll.kernel32.GetModuleHandleW(0)
486539264

And an example from my own code:

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))
like image 185
David Heffernan Avatar answered Sep 21 '22 15:09

David Heffernan


I'm posting my experience. First of all despite all the hard work that take me to put all pieces together, importing a C# dll is easy. The way I did it is:

1) Install this nuget package (i'm not owner, is just very useful) in order to build a unmanaged dll: https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

2) Your C# dll code is like this:

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) Your python code is like this:

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"))
like image 43
piloks_aiti Avatar answered Sep 19 '22 15:09

piloks_aiti