如何在Linux上拼写IronPython

我试图在Ubuntu 10.10上从IronPython 2.6中调用一个C函数。 我使用了IP分配的一个例子作为我的模型。 但是,C代码抛出“StandardError:exception已被调用的目标引发”。

我尝试了几种方法,但都没有工作。 这是我的代码:

pinvoke_test.h

extern void pinvoke_this(const char*); 

pinvoke_test.c

 #include <stdio.h> #include "pinvoke_test.h" void pinvoke_this(const char *b) { FILE *file; file = fopen("file.txt","w+"); fprintf(file,"%s", b); fclose(file); } 

pinvoke_test.py

 import clr import clrtype import System class NativeMethods(object): __metaclass__ = clrtype.ClrClass from System.Runtime.InteropServices import DllImportAttribute, PreserveSigAttribute DllImport = clrtype.attribute(DllImportAttribute) PreserveSig = clrtype.attribute(PreserveSigAttribute) @staticmethod @DllImport("pinvoke_test.o") @PreserveSig() @clrtype.accepts(System.Char) @clrtype.returns(System.Void) def pinvoke_this(c): raise RuntimeError("this should not get called") def call_pinvoke_method(): args = System.Array[object](("sample".Chars[0],)) pinvoke_this = clr.GetClrType(NativeMethods).GetMethod('pinvoke_this') pinvoke_this.Invoke(None, args) call_pinvoke_method() 

目标文件由“gcc -c pinvoke_test.c -o pinvoke_test.o”编译。 我希望有人能指引我正确的方向。

这可能不是你问题的原因,但是密码签名看起来是错误的。 你的C函数需要一个char *,而不是char。

  1. 这映射到一个pinvoke签名中的字符串。 您可能需要指定CharSet以确保正确编组,具体取决于您定位的平台。 请参阅http://www.mono-project.com/Interop_with_Native_Libraries#Strings
  2. 这个改变意味着你的'参数'变量需求也会被调整。 你想要的字符串“样品”,而不是第一个字符。
  3. 注意:C#中的char类型对应于2字节字符,而不是字节。