2
我试图在Ubuntu 10.10上从IronPython 2.6中调用C函数。我使用了来自IP分配的示例作为我的模型。但是,C代码会抛出“StandardError:异常已被调用的目标抛出。”如何在Linux上拼音IronPython
我已经尝试了几种方法,但都没有工作。这里是我的代码:
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“。我希望有人能指出我正确的方向。
我建议使用-X:ExceptionDetail从ipy.exe运行时获取完整的堆栈跟踪。只知道例外文本并不是那么有用。使用ctypes库也可能会有更好的运气,因为它是与C库交谈的更加Pythonic的方式。 – 2011-03-18 01:39:34