Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 如何使用FlasCC将ByteArray传递给C代码_Actionscript_Flascc - Fatal编程技术网

Actionscript 如何使用FlasCC将ByteArray传递给C代码

Actionscript 如何使用FlasCC将ByteArray传递给C代码,actionscript,flascc,Actionscript,Flascc,我想把ByteArray从ActionScript传递到C函数 基本上我想做这样的事情: void init() __attribute__((used,annotate("as3sig:public function init(byteData: ByteArray):int"), annotate("as3package:example"))); void init() { //here I want to pass byteArray data to C varia

我想把ByteArray从ActionScript传递到C函数

基本上我想做这样的事情:

void init() __attribute__((used,annotate("as3sig:public function init(byteData: ByteArray):int"),
        annotate("as3package:example")));

void init()
{
   //here I want to pass byteArray data to C variable.
   //similar to AS3_GetScalarFromVar(cVar, asVar) 
}

不幸的是,我在flascc文档中找不到任何函数来帮助我解决这个问题。

您应该使用CModule.malloc和CModule.writeBytes方法以C风格的方式使用指针进行操作。看看$FLASCC/samples/06_SWIG/PassingData/PassData.as

示例:

void _init_c(void) __attribute((used,
    annotate("as3sig:public function init(byteData:ByteArray) : void"),
    annotate("as3import:flash.utils.ByteArray")));

void _init_c()
{
    char *byteArray_c;
    unsigned int len;

    inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
    byteArray_c = (char *)malloc(len);

    inline_as3("CModule.ram.position = %0;" : : "r"(byteArray_c));
    inline_as3("byteData.readBytes(CModule.ram);");

    // Now byteArray_c points to a copy of the data from byteData.
    // Note that byteData.position has changed to the end of the stream.

    // ... do stuff ...

    free(byteArray_c);
}
这里的关键是C中的堆在AS3端作为
CModule.ram
公开,它是一个
ByteArray
对象


在AS3中,C中的一个指针malloc'd被视为是对
CModule.ram

的一个偏移。这个答案与@mziwisky的答案相比有一个小的变化,应该是作为对该帖子的评论,而不是用小的修改来复制。这不是StackExchange的工作方式。另外,如果您提供解释,而不是简单地删除代码,这将有所帮助。有关如何写出好答案的指导,请参见。
void _init_c(void) __attribute((used,
    annotate("as3sig:public function init(byteData:ByteArray) : void"),
    annotate("as3import:flash.utils.ByteArray")));

void _init_c()
{
    char *byteArray_c;
    unsigned int len;

    inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
    byteArray_c = (char *) malloc(len);

    inline_as3("byteData.readBytes(CModule.ram, %0, %1);" : : "r"(byteArray_c), "r"(len));

    // Now byteArray_c points to a copy of the data from byteData.
    // Note that byteData.position has changed to the end of the stream.

    // ... do stuff ...

    free(byteArray_c);
}