使用。NET(C#)DLL的Python脚本脚本、NET、DLL、Python

2023-09-03 03:52:27 作者:半世壹封

我试图端口一个简单的C#命令行实用程序到Python。在C#程序使用自定义.NET的DLL称为foobar.dll与一些实验设备通过I2C接口。在C#code DLL的用法如下:

I'm attempting to port a simple C# command-line utility to Python. The C# program uses a custom .Net dll called foobar.dll that interfaces with some lab equipment via I2C. The usage of the dll in the C# code is as follows:

fooBar.fooProvider provider = new foobar.fooProvider()
fooBar.fooBarLib fooLib = new foobar.fooBarLib(provider, 0x80)
# used below ...
numFloat = fooLib.GetSomething()
# more python ...
fooLib.SetSomething(NumberAsAFloat)

我想过简单的使用ctypes的访问DLL,但我想这不会对C#/。NET工作。由于我们实验室的限制,我需要使用香草的Python发行版+插件模块(即不是IronPython的或CPython的)。我看了一下Python中的.NET,但我不知道这会实际工作。我是一个的n00b到.NET,但有大量的与Java,C ++和放大器经验;蟒蛇。任何人都遇到过这种情况之前?一个很好的解决方案。

I thought about simply using ctypes to access the dll, but I figured this wouldn't work for C#/.Net. Due to restrictions in our lab I need to use a vanilla Python distro + addon modules (i.e. not IronPython or CPython.) I looked at Python for .NET, but I'm not sure this would actually work. I'm a n00b to .Net, but have lots of experience with Java, C++ & Python. Anyone encountered this situation and a good solution before?

编辑:

通过从下面手头的解释,我进口的蟒蛇间preTER的CLR,然后试图用下面的结果导入foobar的库:

With the explanation from below in hand, I imported the clr in the python interpreter, then attempted to import the fooBar library with the following result:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Interop.AVMCIFCLib, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'Interop.AVMCIFCLib, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null'
   at System.Signature._GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, IntPtr fieldHandle, IntPtr methodHandle, IntPtr declaringTypeHandle)
   at System.Signature.GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, RuntimeFieldHandle fieldHandle, RuntimeMethodHandle methodHandle, RuntimeTypeHandle declaringTypeHandle)
   at System.Signature..ctor(RuntimeMethodHandle methodHandle, RuntimeTypeHandledeclaringTypeHandle)
   at System.Reflection.RuntimeMethodInfo.get_Signature()
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParametersNoCopy()
   at System.Reflection.RuntimePropertyInfo.GetIndexParameters()
   at Python.Runtime.ClassManager.GetClassInfo(Type type)
   at Python.Runtime.ClassManager.CreateClass(Type type)
   at Python.Runtime.ClassManager.GetClass(Type type)
   at Python.Runtime.ModuleObject.GetAttribute(String name, Boolean guess)
   at Python.Runtime.ModuleObject.LoadNames()
   at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

然后跨preTER崩溃。这看起来像一个糟糕内置的dll,是否正确?

And then the interpreter crashes. This looks like a badly built dll, correct?

推荐答案

的的Python .NET 的作品真的很好,你可以从Python的执行.NET code。 IronPython的效果很好太,尽管周围的其他方法。你能想到的Python for .NET的作为的CPython的扩展,它可以执行.NET code,而IronPython的是CLR允许执行Python code的延伸。因为你的需求决定你使用香草的Python,Python中的.NET应该工作。而且要注意,香草Python发布的是 CPython的,在大多数情况下。

Python for .NET works really well, and you can execute .NET code from within Python. IronPython works well too, although the other way around. You can think of Python for .NET as an extension of CPython that can execute .NET code, while IronPython is an extension of the CLR allowing to execute Python code. Since your needs dictate you to use a vanilla Python, Python for .NET should work. And to note, the vanilla python distribution is CPython in most cases.