How would I import a winDLL into python and be able to use all of its functions? It only needs doubles and strings.
-
What do you have so far, and how doesn't it work? – Ignacio Vazquez-Abrams Mar 10 '11 at 00:01
-
Duplicate with this question? http://stackoverflow.com/questions/252417/how-can-i-use-a-dll-from-python – payne Mar 10 '11 at 00:01
5 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))

- 15
- 4

- 601,492
- 42
- 1,072
- 1,490
-
Well, I knew I needed ctypes but I didn't know how to use them. :) Also, very good link! The python documentation seems to only be good for a reference, but not actual learning. Thanks a ton! – pajm Mar 10 '11 at 00:09
-
Wait! I think I overlooked your code. After looking at the tutorial, it seems to only demonstrate how to load windows DLLs. I need to load a custom DLL file. How would I do this? – pajm Mar 10 '11 at 02:50
-
@Patrick I've added another example. But it's all there in the tutorial. There's no theoretical difference between calling your own DLL and a Windows DLL. – David Heffernan Mar 10 '11 at 07:51
-
I hate to bother you again... But could you check the code I put into the original question? Thanks! – pajm Mar 10 '11 at 22:19
-
@Patrick Please could you simply ask a new question. Include the code, both the ctypes code and the DLL function signature. – David Heffernan Mar 10 '11 at 22:27
-
-
-
@DavidHeffernan offtopic: Can ctype export c variables in dll to python? – el psy Congroo May 26 '17 at 15:30
-
-
@steven It does. That was lifted from the ctypes tutorial. For the sake of a concise example I can see why they did that. – David Heffernan Jul 31 '18 at 20:29
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"))

- 9,474
- 36
- 90
- 105

- 31
- 1
c-types NOTE!
Using WinDLL
(and wintypes
, msvcrt
) is windows specific imports and does not always work, even on windows! The reason is that it depends on your python installation. Is it native Windows (or using Cygwin or WSL)?
For ctypes, the more portable and correct way is to use cdll
like this:
import sys
import ctypes
from ctypes import cdll, c_ulong
kFile = 'C:\\Windows\\System32\\kernel32.dll'
mFile = 'C:\\Windows\\System32\\msvcrt.dll'
try:
k32 = cdll.LoadLibrary(kFile)
msvcrt = cdll.LoadLibrary(mFile)
except OSError as e:
print("ERROR: %s" % e)
sys.exit(1)
# do something...
Keep dlls file in System32 or SysWOW64 folder in your 'C:\Windows' location. Use python ctypes
or cffi
library. If you use cffi library:
from cffi import FFI
ffi = FFI()
ffi.dlopen("kernel32")
ffi.dlopen("msvcrt")
ffi.dlopen("MyDLL")
ffiobj = ffi.dlopen('MyDLL2')
Or
import ctypes
kernel_32 = ctypes.WinDLL('kernel32')
It's working for me.

- 61
- 1
- 3
-
Any idea what `cffi` does differently from `ctypes`? (Why would you prefer one over the other?) – not2qubit Jun 19 '23 at 07:19