Saya mencoba mengaktifkan fungsi C dari IronPython 2.6 di Ubuntu 10.10. Saya telah menggunakan contoh dari distribusi IP sebagai model saya. Namun, kode C menampilkan "StandardError: Exception telah dilemparkan oleh target pemanggilan."
Saya sudah mencoba beberapa pendekatan, tetapi tidak ada satupun yang berhasil. Ini kode saya:
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()
File objek dikompilasi oleh "gcc -c pinvoke_test.c -o pinvoke_test.o". Saya harap seseorang dapat mengarahkan saya ke arah yang benar.