ฉันกำลังพยายามเรียกฟังก์ชัน C จาก IronPython 2.6 บน Ubuntu 10.10 ฉันใช้ตัวอย่างจากการแจกแจง IP เป็นแบบจำลองของฉัน อย่างไรก็ตาม รหัส C แสดง "StandardError: Exception has been Thrown by the target of an invocation"
ฉันได้ลองหลายวิธีแล้ว แต่ก็ไม่ได้ผลเลย นี่คือรหัสของฉัน:
pinvoid_test.h
extern void pinvoke_this(const char*);
pinvolve_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);
}
pinvolve_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 pinvoid_test.c -o pinvoid_test.o" ฉันหวังว่าจะมีคนชี้ฉันไปในทิศทางที่ถูกต้อง