Python导入dll

发布于 2024-10-21 03:54:49 字数 53 浏览 2 评论 0原文

如何将 winDLL 导入到 python 中并能够使用它的所有功能?它只需要双打和字符串。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魂牵梦绕锁你心扉 2024-10-28 03:54:49

您已标记问题 ctypes ,所以听起来您已经知道答案。

ctypes 教程< /a> 非常好。一旦您阅读并理解了,您将能够轻松地做到这一点。

例如:

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

我自己的代码中的一个示例:

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))

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))
放低过去 2024-10-28 03:54:49

我发布我的经历。首先,尽管我付出了很多艰苦的工作才能将所有部分组合在一起,但导入 C# dll 很容易。我这样做的方法是:

1)安装这个nuget包(我不是所有者,只是非常有用)以构建非托管dll:https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagementexports

2) 您的 C# dll 代码如下:

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)你的Python代码是这样的:

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"))

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"))
抠脚大汉 2024-10-28 03:54:49

c 型 注意!

使用 WinDLL(以及 wintypesmsvcrt)是 Windows 特定的导入,并不总是有效,即使在 Windows 上也是如此!原因是它取决于你的Python安装。是本机 Windows(或使用 Cygwin 或 WSL)吗?

对于ctypes,更可移植和正确的方法是使用cdll,如下所示:

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...

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...
最冷一天 2024-10-28 03:54:49

使用 Cython 来访问 DLL 并为其生成 Python 绑定。

Use Cython, both to access the DLLs, and to generate Python bindings for them.

败给现实 2024-10-28 03:54:49

将 dll 文件保存在“C:\Windows”位置的 System32 或 SysWOW64 文件夹中。使用 python ctypescffi 库。如果您使用 cffi 库:

from cffi import FFI
ffi = FFI()
ffi.dlopen("kernel32")
ffi.dlopen("msvcrt")
ffi.dlopen("MyDLL")
ffiobj = ffi.dlopen('MyDLL2')

或者

import ctypes
kernel_32 = ctypes.WinDLL('kernel32')

它对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文